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

发布于 2024-09-17 11:22:41 字数 350 浏览 6 评论 0原文

我需要将 credits.jpgC:\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 技术交流群。

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

发布评论

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

评论(3

明天过后 2024-09-24 11:22:41

尝试一下:

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

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

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 '%'.

笨笨の傻瓜 2024-09-24 11:22:41

这是我在 Google 上找到的第一个搜索将文件批量复制到所有子文件夹

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


但是,让我们重点关注 xcopy

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

cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
  • cd "D:\Software\destinationfolder" 将目录更改为要将文件复制到的文件夹。如果路径有空格,请将其括在引号中。
  • for 循环 - 在此处查看帮助。或者在命令提示符下键入 for /?
  • /r - 循环文件(递归子文件夹)
  • /d - 循环多个文件夹
  • %%I - %%parameter:可替换参数
  • xcopy - 在命令行中键入 xcopy /? 以获得大量帮助。您可能需要按 Enter 才能阅读有关此内容的完整帮助。
  • 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 选项即可使其表现得像这样。

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

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"
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 for loop - 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 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.

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

童话 2024-09-24 11:22:41

如果您可以使用它:这是一个 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

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

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

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

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