views:

528

answers:

2

I have a batch file which loops through a content of a text file and copies a specific file using xcopy command.

here's the snippet.

for /f %%a in (FilesToCopy.txt) do (
xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f 
xcopy Common\%%a ..\..\Common\%%a /i /d /C /v /s /y /f 
)

%%a contains values like Images\image1.jpg Images\image2.jpg

so when xcopy is executed it would look like

xcopy ..\..\Common\Images\image1.jpg Common\Images\image1.jpg /i /d /c /v /s /y

upon execute it would then prompt this message

Does Common\Images\image1.png specify a file name
or directory name on the target
(F = file, D = directory)?

it seems that the /i command doesn' work or i am missing something here to suppress the message above.

+3  A: 

Well, you left out the second statement the help gives about /I:

/I           If destination does not exist and copying more than one file,
             assumes that destination must be a directory.

You are only ever copying one file at a time, so /I doesn't apply.

You can probably hack-solving this by piping F into the command and suppressing output:

echo F|xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f >nul

(Won't work on non-English versions of Windows; but probably that's the least of your problems, given that the batch already fails for file names with spaces :-))

You could try building a single long list of file names to copy:

setlocal enabledelayedexpansion enableextensions
set LIST=
for /f %%a in (FilesToCopy.txt) do set LIST=!LIST! "..\..\Common\%%a"
xcopy %LIST% Common /i /d /c /v /s /y /f

This requires two passes over the initial file, though. And it fails when the list of file names gets longer than 8190 characters.

Joey
good point with the hack thing, i'll try your solution tho...as you can see there is a timestamp checking /d there so /d means copy only if the destination is older.so if commandA's source has new version than the destination then it would replace it and vice versa for commandB.
Juvil John Soriano
Ah, didn't bother to dissect the switches there in detail, sorry :-)
Joey
+1  A: 

The destination should be a path, then it won't ask:

xcopy ..\..\Common\Images\image1.jpg Common\Images\ /i /d /c /v /s /y

In your case, you can use path extraction with %~p on the destination since you may want to preserve that:

xcopy ..\..\Common\%%a Common\%%~pa /i /d /c /v /s /y
cjrh
yes, that would work but then as you can see there is a reason why its i'm using the text file and store it to %%a, that way i would have a dynamic destination path. Images aren't the only expected path there.
Juvil John Soriano
Good point. I updated my answer to show that you can extract the path from your items in the text file.
cjrh