为什么 xcopy 需要多个引号?

发布于 2024-12-19 01:37:51 字数 869 浏览 3 评论 0原文

当我的备份脚本中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-12-26 01:37:51

您在
中的 %%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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文