【问题标题】:Copy a file to all folders batch file?将文件复制到所有文件夹批处理文件?
【发布时间】:2010-09-02 18:01:02
【问题描述】:

我需要将 credits.jpgC:\Users\meotimdihia\Desktop\credits.jpg 复制到 D:\Software\destinationfolder and all subfolders 我读了很多,我写了

/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"

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

【问题讨论】:

  • PowerShell 可以在其中执行吗?它是从 Windows 7 集成的(适用于 XP+)
  • 我用来复制的功能是什么?

标签: windows windows-7 batch-file


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 工作起来就像一个魅力和超级简单。
【解决方案2】:

这是我在 google 上找到的第一个 batch file copy file to all subfolders 搜索。

这是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 循环 - See help here。或者在命令提示符下输入for /?
  • /r - 循环文件(递归子文件夹)
  • /d - 循环浏览多个文件夹
  • %%I - %%parameter:可替换参数
  • xcopy - 在命令行中输入 xcopy /? 以获得大量帮助。您可能需要按 Enter 才能阅读有关此内容的完整帮助。
  • C:\temp\file.ext - 你要复制的文件
  • "%%~fsI" - 将 %%I 扩展为仅包含短名称的完整路径名
  • /H - 复制具有隐藏和系统文件属性的文件。默认情况下,xcopy 不会复制隐藏文件或系统文件
  • /K - 复制文件并保留目标文件的只读属性(如果源文件中存在)。默认情况下,xcopy 会移除只读属性。

如果您在处理任何只读文件时遇到问题,最后两个参数只是示例,并且将保留最重要的文件属性。

Lots more xcopy parameters here

xcopy examples here


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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-04-12
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2011-06-03
      • 2015-05-10
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多