批处理文件 XCopy 命令

发布于 2024-08-13 11:02:43 字数 623 浏览 4 评论 0原文

我有一个批处理文件,它循环遍历文本文件的内容并使用 xcopy 命令复制特定文件。

这是片段。

for /f %%a in (FilesToCopy.txt) do (
xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f 
xcopy Common\%%a ..\..\Common\%%a /i /d /C /v /s /y /f 
)

%%a 包含类似的值 图片\image1.jpg Images\image2.jpg

因此,当执行 xcopy 时,它看起来会

xcopy ..\..\Common\Images\image1.jpg Common\Images\image1.jpg /i /d /c /v /s /y

在执行时提示此消息

Does Common\Images\image1.png specify a file name
or directory name on the target
(F = file, D = directory)?

,似乎 /i 命令不起作用,或者我在这里缺少一些内容来抑制上面的消息。

I have a batch file which loops through a content of a text file and copies a specific file using xcopy command.

here's the snippet.

for /f %%a in (FilesToCopy.txt) do (
xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f 
xcopy Common\%%a ..\..\Common\%%a /i /d /C /v /s /y /f 
)

%%a contains values like
Images\image1.jpg
Images\image2.jpg

so when xcopy is executed it would look like

xcopy ..\..\Common\Images\image1.jpg Common\Images\image1.jpg /i /d /c /v /s /y

upon execute it would then prompt this message

Does Common\Images\image1.png specify a file name
or directory name on the target
(F = file, D = directory)?

it seems that the /i command doesn' work or i am missing something here to suppress the message above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

倚栏听风 2024-08-20 11:02:43

好吧,您遗漏了帮助中关于 /I第二语句:

/I           If destination does not exist and copying more than one file,
             assumes that destination must be a directory.

您一次只能复制一个文件,因此 /I不适用。

您可以通过将 F 管道输入命令并抑制输出来解决此问题:(

echo F|xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f >nul

在非英语版本的 Windows 上不起作用;但这可能是您遇到的最不重要的问题,因为批处理对于带有空格的文件名已经失败:-))

您可以尝试构建一个要复制的长文件名列表:

setlocal enabledelayedexpansion enableextensions
set LIST=
for /f %%a in (FilesToCopy.txt) do set LIST=!LIST! "..\..\Common\%%a"
xcopy %LIST% Common /i /d /c /v /s /y /f

不过,这需要对初始文件进行两次传递。当文件名列表超过 8190 个字符时,它会失败。

Well, you left out the second statement the help gives about /I:

/I           If destination does not exist and copying more than one file,
             assumes that destination must be a directory.

You are only ever copying one file at a time, so /I doesn't apply.

You can probably hack-solving this by piping F into the command and suppressing output:

echo F|xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f >nul

(Won't work on non-English versions of Windows; but probably that's the least of your problems, given that the batch already fails for file names with spaces :-))

You could try building a single long list of file names to copy:

setlocal enabledelayedexpansion enableextensions
set LIST=
for /f %%a in (FilesToCopy.txt) do set LIST=!LIST! "..\..\Common\%%a"
xcopy %LIST% Common /i /d /c /v /s /y /f

This requires two passes over the initial file, though. And it fails when the list of file names gets longer than 8190 characters.

呆橘 2024-08-20 11:02:43

目的地应该是路径,然后它不会询问:

xcopy ..\..\Common\Images\image1.jpg Common\Images\ /i /d /c /v /s /y

在您的情况下,您可以在目的地上使用带有 %~p 的路径提取,因为您可能想保留它:

xcopy ..\..\Common\%%a Common\%%~pa /i /d /c /v /s /y

The destination should be a path, then it won't ask:

xcopy ..\..\Common\Images\image1.jpg Common\Images\ /i /d /c /v /s /y

In your case, you can use path extraction with %~p on the destination since you may want to preserve that:

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