批处理文件复制文件到所有文件夹?
我需要将 credits.jpg
从 C:\Users\meotimdihia\Desktop\credits.jpg
复制到 D:\Software\destinationfolder 和所有子文件夹
我读了很多,我写了
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
然后保存文件 saveall.bat 但我运行它,它根本不起作用。 帮我写1个bat
I need copy credits.jpg
from C:\Users\meotimdihia\Desktop\credits.jpg
to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下:
当然,如果要进入批处理文件,请将“%”加倍。
Give this a try:
Of course, if it's to go into a batch file, double the '%'.
这是我在 Google 上找到的第一个搜索
将文件批量复制到所有子文件夹
。这是 xcopy 的方法。
还有 robocopy 但这也太对于像这样的简单任务来说功能强大。 (我已将其用于整个驱动器备份,因为它可以使用多线程)
但是,让我们重点关注
xcopy
。此示例用于保存到扩展名为
.bat
的文件。如果直接在命令行上运行,只需删除额外的%
(其中有两个)。cd "D:\Software\destinationfolder"
将目录更改为要将文件复制到的文件夹。如果路径有空格,请将其括在引号中。for
循环 - 在此处查看帮助。或者在命令提示符下键入for /?
。/r
- 循环文件(递归子文件夹)/d
- 循环多个文件夹%%I
-%%parameter:可替换参数
C:\temp\file.ext
- 要复制的文件"%%~fsI"
- 将%%I
扩展为完整文件仅包含短名称的路径名/H
- 复制具有隐藏和系统文件属性的文件。默认情况下,xcopy 不复制隐藏文件或系统文件/K
- 复制文件并保留目标文件的只读属性(如果源文件中存在)。默认情况下,xcopy 删除只读属性。如果您在处理任何只读文件时遇到问题,最后两个参数只是示例,并且将保留最重要的文件属性。
此处有更多 xcopy 参数
< a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy#examples" rel="nofollow noreferrer">xcopy 示例此处
只是为了完整性。下面的示例将在当前目录的每个文件夹中复制相同的文件,而不是任何子文件夹中。只需删除
/r
选项即可使其表现得像这样。This was the first search I found on google for
batch file copy file to all subfolders
.Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on
xcopy
.This example is for saving to a file with the extension
.bat
. Just drop the additional%
where there is two if running directly on the command line.cd "D:\Software\destinationfolder"
change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.for
loop - See help here. Or typefor /?
in a command prompt./r
- Loop through files (recurse subfolders)/d
- Loop through several folders%%I
-%%parameter
: A replaceable parameterxcopy
- Typexcopy /?
in the command line for lots of help. You may need to press Enter to read the entire help on this.C:\temp\file.ext
- The file you want to copy"%%~fsI"
- Expands%%I
to a full pathname with short names only/H
- Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files/K
- Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the
/r
option is removed for it to behave like this.如果您可以使用它:这是一个 PowerShell 解决方案(PowerShell 集成在 Windows 7 中,可从 XP 及更高版本使用):
我很确定最后一行可以集成在
dir ...
中但我不知道如何(我不经常使用 PowerShell)。If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
I'm pretty sure that the last line can be integrated in
dir ...
but I'm not sure how (I do not use PowerShell very often).