Search code examples
windowscommand-linedosxcopy

Command prompt logging with xcopy


I have an xcopy script that I'm running that reads in a .csv file with directories and file names and parses it to copy the files.

Here's the script:

echo F | for /f "delims=, tokens=1,2,3" %i in (D:\foo.csv) 
do xcopy /i /d "Z:\%i\%j\%k" "Y:\%i\%j\%k" >> "D:\xcopy\Log.txt"

The output to the command prompt are the commands being executed:

 echo F   | xcopy /i /d "Z:\hcri001\a1\
ffce5a14-33ca-43cf-b366-af266c450979" "Y:\hcri001\a1\ffce5a14-33ca-43cf-b366
-af266c450979"  1>>"D:\xopy\Log.txt

However, the log just has the output of the commands:

0 File(s) copied
0 File(s) copied
0 File(s) copied
Does Y:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3 specify a file name
or directory name on the target
(F = file, D = directory)? F
Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3
1 File(s) copied

Is there someway to have both the commands being executed, as well as the output? Also, is there anyway to have some sort of counter that I can put in the beginning of the line?

My Ideal log file would be in this format:

    1) C:\Desktop>echo F   | xcopy /i /d "Z:\hcri001\a7\
    00a62a73-d7a7-4cfb-b55c-457bc67b3647" "Y:\hcri001\a7\00a62a73-d7a7-4cfb-b55c
    -457bc67b3647"  1>>"D:\Robocopy\AtlusPatient131172CopyLog.txt 
    0 File(s) copied
    2)    C:\Desktop>echo F   | xcopy /i /d "Z:\hcri001\d5\
    003452354-d7a7-4cfb-452c-457bc67b3647" "Y:\hcri001\d5\003452354-d7a7-4cfb-452c-457bc67b3647"  1>>"D:\Robocopy\AtlusPatient131172CopyLog.txt 
    0 File(s) copied
    3)   C:\Desktop>echo F   | xcopy /i /d "Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3" "Y:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3"  1>>"D:\Robocopy\AtlusPatient131172CopyLog.txt 
Does Y:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3 specify a file name
        or directory name on the target
        (F = file, D = directory)? F
        Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3
        1 File(s) copied

and bonus points if the command can be trimmed to just include the file name instead of the whole command i.e. 1) "Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3" 0 File(s) copied


Solution

  • for /f "delims=, tokens=1,2,3" %i in (D:\foo.csv) do (
      echo xcopy /i /d "Z:\%i\%j\%k" "Y:\%i\%j\%k" >> "D:\xcopy\Log.txt"
      echo f | xcopy /i /d "Z:\%i\%j\%k" "Y:\%i\%j\%k" >> "D:\xcopy\Log.txt"
    )
    

    For the counter you need SETLOCAL ENABLEEXTENSIONS then set counter=1 and inside the loop set /a counter=!counter! + 1