12/7/2023 0 Comments Grsync for windowsIn 2021, Windows still lacks a file/folder-agnostic copy command that does recursion and update-only. And we must do the expansion because we must discriminate files vs folders, because the syntax of xcopy is inconsistent between files and folders, and so is the syntax of robocopy. No, I couldn't store the expanded list in a var and pass it to a single invocation of xcopy, because of the Windows command line length limit. In my case, throughput was no better than the calls to powershell -Command "Copy-Item -Recurse -Force" that did unnecessary copying and had the overhead of launching Powershell, because the replacement code above invokes xcopy individually on every item in the expanded file pattern. Robocopy %%D \%%~nxD /MIR /PURGE || IF !ERRORLEVEL! LSS 8 cd. REM `CD.` is the best way I know to simulate the plain old POSIX `true`. REM It's like Microsoft doesn't want its tools to be scriptable. REM `robocopy` returns exit codes 0-7 on success. REM but switching the role of folders and non-folders. REM `FOR /D` has the same unintuitive behavior described above for `FOR`, (xcopy %%F /D /R /Y /I) || EXIT /B !ERRORLEVEL! REM not a folder name is to do the weird construct below. REM The way in Windows (without calling Powershell) to check if a filename is REM As mentioned above, the non-folders-only version of `FOR` *does* let folders through, REM Stupidly, Windows requires `%%` for loop vars in. REM in the expanded list - the filtering *only* happens for wildcarded elements of the pattern. REM But even this doesn't do what you expect, because folders *will* be retained REM Stupidly, `FOR` with no params means: "expand wildcards only to files (not folders), REM although only when you use !variable! syntax. REM This line makes var evaluation instead act the way everyone would actually want and expect, To explain this, I'll break it up into lines with comments: REM Stupidly, by default, Windows evaluates %variables% in loop blocks *before* the block executes. SETLOCAL EnableDelayedExpansion & FOR %%F IN () DO (IF NOT EXIST %%F\NUL ( (xcopy %%F /D /R /Y /I) || EXIT /B !ERRORLEVEL! ) ) & FOR /D %%D IN () DO (IF EXIST %%D\NUL ( (robocopy %%D \%%~nxD /MIR /PURGE || IF !ERRORLEVEL! LSS 8 cd. OK, the one-liner equivalent, on Windows, of rsync -recursive -update -delete /, without invoking Powershell (which would have killed my throughput in my application) is:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |