Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top