Take the 2-minute tour ×

I have a filesystem that uses a hash algorithm to organize files. I have used xcopy in the past to copy files to a different location by passing in a file that has a list of all the files and having it iterate through it. The script looks similar to the following:

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

However, now I've run into a situation where in addition to copying the files that are provided in the foo.csv file, I want them to be deleted as well. I looked at the xcopy documentation and couldn't find anything. Is there someway I can accomplish this, even if I have to run another script to go through the same list of files and delete them after using xcopy?


share|improve this question

3 Answers 3

up vote 2 down vote accepted

You can use parenthesis to indicate multiple commands to be excecuted by the for operand:

for /f "delims=, tokens=1,2,3" %%i in (D:\foo.csv) do (
    echo F | xcopy /i /d "Z:\%%i\%%j\%%k" "Y:\%%i\%%j\%%k" >> "D:\xcopy\Log.txt"
    del /F "Z:\%%i\%%j\%%k"
share|improve this answer
Whenever I try to run this it just keeps outputting "More?" over and over regardless of what I type. Am I doing something wrong? –  Saggio Oct 27 '11 at 19:51
@user564636 I would need to see the actual input file you are using to dignose any further. Also take into account that you need to use %%i instead of %i if you are using this inside a .bat script –  yms Oct 27 '11 at 19:58
Nevermind, I was just missing a Quote. So now when I run it like that it gives me invalid # of parameters errors, but if I use a pipe (like in between echo F and xcopy commands) it's actually deleting the source files before they finish copying. Any ideas? (EDIT): I'm running it directly in the command prompt, I'm not using a batch file –  Saggio Oct 27 '11 at 20:03
@user564636 For the parenthesis thing to work you need to put it in a script, since you are supposed to put each command in a new line –  yms Oct 27 '11 at 20:11
As soon as I put it into a .bat file (and I had to move "do (" up to the same line as the for command because it was giving me incorrect syntax errors when ran), it worked like a charm. Thanks!! –  Saggio Oct 27 '11 at 20:36

I'm not familiar with Windows (I'm happily using Gnu/Linux since 1993), but perhaps you could add some command with variables like del %n somewhere (or replace xcopy with your own .bat file doing what you want)

share|improve this answer

From the look of it you can use move command instead of xcopy, since you're not using any extended features from xcopy. The '/d' is supposed to be used to only copy the files if they are newer, not sure how useful that is for your purpose since you want to delete them. Otherwise, move doesn't have many more options to speak of.

Another possible, and slightly more sophisticated, method is robocopy.
robocopy /MOVE /XO "Z:\%i\%j\%k" "Y:\%i\%j\%k"
The /MOVE would delete both folders and files after copying, and /XO flag excludes older files from being copied. robocopy is primarily available in newer operating systems (i.e. Not XP). You can check the above mentioned reference for more details.

Hope this helps. Although, just using del as previously mentioned should work fine also.

share|improve this answer
Does this work for lists of files? The '%k' variable is a file name, not a directory. I was under the impression robocopy could only copy/move whole directories and their contents, not specific files within them. (EDIT): Nevermind, I was missing a quotation mark –  Saggio Oct 27 '11 at 19:53
Disregard the edit, that was supposed to be for my comment on the other answer. I missed the window to edit it back out :p –  Saggio Oct 27 '11 at 20:08

Your Answer


