'Copying and renaming multiple files in a .bat file with FOR ... DO xcopy

I want to move several thousand files (.jpg and .pdf) from one location to another and then rename each file.

My 'copy_rename_docs.csv' input file contains two columns:

  • the first column containing the original path AND file names for each file that I want to move and rename
  • and the second column containing the new paths AND file names that I want to move and rename each file to

like this:

\\myapp\data\e3\9b\e39bf5e8d745833418d3edcd97c6c7589716970b,\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\data\09\f5\09f58d1a1345f67a36154e99ffedb27308fa4292,\\myapp\test_migration\09\f5\nxf7.pdf

so "e39bf5e8d745833418d3edcd97c6c7589716970b" is a SHA1 hash of "Test File-xxx - SS1.pdf"

To copy and rename the files I tried this batch file:

@echo off
FOR /F "tokens=1,2 delims=," %%a IN (copy_rename_docs.csv) DO (xcopy /e /i "%%a" "%%b\*")
pause
exit

But instead of copying and renaming the files to this:

\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf

It is creating a FOLDER for each file with the FILE names as the FOLDER names and copying the files with their original file names into each FOLDER, like this:

\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf\e39bf5e8d745833418d3edcd97c6c7589716970b
\\myapp\test_migration\09\f5\nxf7.pdf\09f58d1a1345f67a36154e99ffedb27308fa4292

What I want is:

\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf

What's wrong with my batch file?

And is xcopy the best tool for this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source