Search code examples
batch-filexcopy

Copy files and output the target path of the overriden files


I would like to write a batch that will copy all of the files in a given source path to a given output path, and plot the target path of the files that were actually changed. XCOPY is doing almost that. It plots the source paths.

I guess it would be easier with PowerShell, but I would rather keeping it in the bat file.


Solution

  • Extending Sumit's suggestion of the /F option - you could use FOR /F to parse out only the destination files. There is no need for a batch script. It can be a one liner on the interactive command line.

    for /f "delims=> tokens=2" %A in ('xcopy /f "someSourcePath\*.txt" "someDestinationPath"') do @echo %A
    

    The above will include an unwanted leading space on each line of output. If needed, you can use an extra FOR /F to remove the leading space.

    for /f "delims=> tokens=2" %A in ('xcopy /f "someSourcePath\*.txt" "someDestinationPath"') do @for /f "tokens=* delims= " %B in ("%A") do @echo %B
    

    Either way, the file count at the end is also removed.

    Double all % as %% if you put the command in a batch script.

    The solution is even simpler if you have my JREPL.BAT regular expression text processing utility - a purely script based utility (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.

    xcopy /F "someSourcePath\*.txt" "someDestinationPath" | jrepl "> (.*)" $1 /jmatch
    

    If you want to preserve the file count at the end, then you could use an even simpler variation

    xcopy /F "someSourcePath\*.txt" "someDestinationPath" | jrepl "^.*> " ""