windows 将文件复制到所有文件夹的批处理文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3629831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:10:35  来源:igfitidea点击:

Copy a file to all folders batch file?

windowswindows-7batch-file

提问by meotimdihia

I need copy credits.jpgfrom C:\Users\meotimdihia\Desktop\credits.jpgto D:\Software\destinationfolder and all subfoldersI read many and i write

我需要credits.jpg从复制C:\Users\meotimdihia\Desktop\credits.jpgD:\Software\destinationfolder and all subfolders我读了很多我写

/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

然后我保存文件 saveall.bat 但我运行它,它根本不起作用。帮我写1个蝙蝠

回答by BillP3rd

Give this a try:

试试这个:

for /r "D:\Software\destinationfolder" %i in (.) do @copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"

Of course, if it's to go into a batch file, double the '%'.

当然,如果要进入批处理文件,请将“%”加倍。

回答by Lasse Espeholt

If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):

如果您可以使用它:这是一个 PowerShell 解决方案(PowerShell 集成在 Windows 7 中,可从 XP 及更高版本使用):

$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"

#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir

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).

我很确定最后一行可以集成,dir ...但我不确定如何(我不经常使用 PowerShell)。

回答by Ste

This was the first search I found on google for batch file copy file to all subfolders.

这是我在谷歌上找到的第一次搜索batch file copy file to all subfolders

Here's a way with xcopy. There's also robocopybut 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)

这是xcopy的一种方法。还有robocopy ,但这对于像这样的简单任务来说太强大了。(我已将其用于整个驱动器备份,因为它可以使用多线程



But, let us focus on xcopy.

但是,让我们专注于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.

此示例用于保存到扩展名为.bat. %如果直接在命令行上运行,只需删除额外的两个。

cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
  • 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.
  • the forloop - See help here. Or type for /?in a command prompt.
  • /r- Loop through files (recurse subfolders)
  • /d- Loop through several folders
  • %%I- %%parameter: A replaceable parameter
  • xcopy- Type xcopy /?in the command line for lots of help. You may need to press Enterto read the entire help on this.
  • C:\temp\file.ext- The file you want to copy
  • "%%~fsI"- Expands %%Ito 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.
  • cd "D:\Software\destinationfolder"将目录更改为要将文件复制到的文件夹。如果路径有空格,请将其用引号括起来。
  • for环-在这里看到的帮助。或者输入for /?命令提示符。
  • /r- 循环文件(递归子文件夹)
  • /d- 循环浏览几个文件夹
  • %%I- %%parameter: 一个可替换的参数
  • xcopy-xcopy /?在命令行中输入大量帮助。您可能需要按Enter阅读有关此的完整帮助。
  • C:\temp\file.ext- 要复制的文件
  • "%%~fsI"- 扩展%%I为仅包含短名称的完整路径名
  • /H- 复制具有隐藏和系统文件属性的文件。默认情况下,xcopy 不复制隐藏或系统文件
  • /K- 复制文件并保留目标文件的只读属性(如果源文件中存在)。默认情况下,xcopy 删除只读属性。


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 参数

xcopy examples here

xcopy 例子在这里



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 /roption is removed for it to behave like this.

只是为了完整性。下面的这个例子将复制当前目录的每个文件夹中的相同文件,而不是任何子文件夹。只是/r删除了这个选项,它的行为就像这样。

for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K