Search code examples
batch-filecsvwindows-7xcopy

Windows 7 Batch file to xcopy from "filepath_from.csv" to "filepath_to.csv"


I'm sure it's been done before but from source destinations listed in a file "filepath_from.csv" with contents like:

.\ARCHIVE\FOO.PDF,  
.\ARCHIVE\BAR.PDF  

To a destination on the corresponding ith line of the file "filepath_to.csv":

.\VARYING\PATH\IN\WORKING\DIR\FOO.PDF,  
.\VARYING\PATH\IN\WORKING\DIR\BAR.PDF,  

How can a Windows 7 batch file copy these files where the destination directory is to be created in the copy command?

Edit: If it simplifies things, I could make the program which generates these two .csv files simply make one .csv file with two columns.


Solution

  • Put this code in a bat file and run it. It reads paths to copy to from filepath_from.csv and copyes each one to path specified in filepath_to.csv. filepath_to.csv is read from application stream3. It avoids interfering with STDIN. filepath_from.csv is read in standard for loop. I assume both files have equal number of rows.

    @echo off
    setlocal EnableDelayedExpansion
    
    3<filepath_from.csv (
       for /F "delims=" %%a in (filepath_to.csv) do (
          set line=
          set /P line=<&3
          copy /Y /Z "!line!" "%%a" 
          rem Or use      xcopy "!line!" "%%~dpa" /Y /Z
       )
    )
    

    Hope it will help.