Jeremy L.
Jeremy L.

Reputation: 138

Cannot Copy a File Using Batch

I am trying to copy some files using a batch file.

This doesn't work:

@echo off
for /R "C:\Users\User\Documents\Folder FolderA FolderB\Folder\Folder FolderC FolderD\Folder\Folder" %%f in (*.inc) do copy %%f /R "\\10.1.10.156\c\Folder1\Folder 2\Folder3"

This works:

@echo off
for /R "C:\Users\User\Documents" %%f in (*.inc) do copy %%f "\\10.1.10.156\c\Folder1\Folder 2\Folder3"

I also replaced "C:\Users\User\Documents\Folder FolderA FolderB\Folder\Folder FolderC FolderD\Folder\Folder" with %cd% and I got an error saying FolderA was unexpected at this time so I feel like this has to do with the spaces in the folder named Folder FolderA FolderB but I cannot get it to work.

Upvotes: 1

Views: 118

Answers (1)

Wes Larson
Wes Larson

Reputation: 1062

First, you have a /R in your copy statement that shouldn't be there.

Second, as @Dennis van Gils suggests, you need to put quotes around your %%f variable in the copy statement. And here's why:

In this command:

for /R "C:\a folder with\spaces\" %%f in (*.inc) do copy %%f "\\destination\folder"

the variable %%f expands to C:\a folder with\spaces\filename.inc. So when copy tries to use this it sees:

copy C:\a folder

--and then it chokes because C:\a isn't a valid source and folder isn't a valid destination. So, if you put quotes around the variable like this:

rem ---------------------quotes around this variable-----VVVVV
for /R "C:\a folder with\spaces\" %%f in (*.inc) do copy "%%f"  "\\destination\folder"

it will expand to:

copy "C:\a folder with\spaces\filename.inc" "\\destination\folder"

and should work as you expect it to.

Upvotes: 1

Related Questions