user2989849
user2989849

Reputation: 11

Batch File - Using For & Xcopy Getting Invalid Number of Parameters (Not Quotes)?

Basically what I'm trying to do is make a batch file that will run through the computer and grab everything from the Desktops and My Documents of ever user on the computer. Thing is, I won't know every users name because it will be used on unknown computers to back up. Trying to find a way to copy these things but so far I can not. I've been working on My Documents and I keep getting a "Invalid number of parameters".

@echo off
echo This script will copy all of the files from my documents onto a C drive.
pause
md "C:\TestForWork"
pause
for /D /r %%G in ("C:\Users") DO for /D /r "%%H" in ("%%G\My Documents\") do xcopy %%H /e /y "C:\TestFor Work"
pause

Upvotes: 1

Views: 3260

Answers (2)

MC ND
MC ND

Reputation: 70923

for /d %%u in ("c:\users\*") do for %%f in ("Desktop" "Documents") do (
    robocopy "%%~u\%%~f" "c:\test for work\%%~u\%%~f" /s
)

Or, for xcopy

for /d %%u in ("c:\users\*") do for %%f in ("Desktop" "Documents") do (
    xcopy "%%~u\%%~f" "c:\test for work\%%~u\%%~f" /e /y /i
)

Upvotes: 1

paddy
paddy

Reputation: 63471

You need to enclose the first parameter to xcopy in double-quotes because it is a directory name containing spaces:

for /D /r %%G in ("C:\Users") DO (
    for /D /r %%H in ("%%~G\My Documents\") do (
        xcopy "%%~H" /e /y "C:\TestFor Work"
    )
)

You also need to use the ~ to prevent double-quotes from being included in the variable expansion.

Upvotes: 1

Related Questions