2

When the FOR loop in my backup script uses a double-quoted input parameter ("Default User"), I get an "invalid number of parameters" error at the xcopy command line unless I enclose it with an extra pair of double-quotes. The other commands work OK without the extra quotes. Can someone explain why xcopy behaves this way, and if there is a workaround that avoids the extra quotes? Thanks.

@echo off
setlocal
set drive=O:\Backups\test
set backupcmd=xcopy /s /c /d /e /i /r /y

if exist "c:\users" goto startvis7
if exist "c:\documents and settings\" goto startxp

:startxp
for %%a in ("Default User" Owner) do (
if exist "c:\documents and settings\%%a" (
echo ### Backing up desktop for %%a...
%backupcmd% ""c:\documents and settings\%%a\desktop"" ""%drive%\%%a\desktop""
) else (echo ### Account %%a not found.)
)
goto eof

:startvis7
for %%a in ("Default User" Owner) do (
if exist "c:\users\%%a" (
echo ### Backing up desktop for %%a...
%backupcmd% ""c:\users\%%a\desktop"" ""%drive%\%%a\desktop""
) else (echo ### Account %%a not found.)
)
:eof
echo Backup Complete!
@pause
endlocal
user892267
  • 21
  • 2

1 Answers1

2

You got a problem with the %%a in
%backupcmd% ""c:\users\%%a\desktop"" ""%drive%\%%a\desktop""
It can contain also quotes, so your string could look like
""c:\users\"Default User"\desktop""

You could use the %%~a syntax, it strips surrounding quotes, then you should be able to use
%backupcmd% "c:\users\%%~a\desktop" "%drive%\%%~a\desktop"

jeb
  • 78,592
  • 17
  • 171
  • 225