为什么 xcopy 需要多个引号?
当我的备份脚本中的 FOR 循环使用双引号输入参数(“默认用户”)时,我在 xcopy 命令行中收到“参数数量无效”错误,除非我用一对额外的双引号将其括起来。其他命令无需额外引号即可正常工作。有人可以解释为什么 xcopy 会这样做,以及是否有解决方法可以避免额外的引号?谢谢。
@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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
中的
%%a
遇到问题%backupcmd% ""c:\users\%%a\desktop"" ""%drive%\%%a\desktop""
它也可以包含引号,所以你的字符串可能看起来像
""c:\users\"Default User"\desktop""
您可以使用
%%~a
语法,它会去除周围的引号,然后您应该能够使用%backupcmd%“c:\users\%%~a\desktop”“%drive%\%%~a\desktop”
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"