【问题标题】:Copy same file to different directories将相同的文件复制到不同的目录
【发布时间】:2013-03-11 03:47:16
【问题描述】:

是否有一个程序(或 ReNamer 或 .cmd 的代码)将文件 readme.txt 复制到与 readme.txt 相同的目录中的每个目录?

ReNamer:http://www.den4b.com/?x=products&product=renamer(有一个 PascalScript 规则,让用户可以编写自己的重命名规则。)

我每天从工作中获得超过 50 个新目录,它们都需要在其中的这个文件。我手动做了1000多个,希望有解决办法。

谢谢!

【问题讨论】:

    标签: copy cmd rename copy-paste pascalscript


    【解决方案1】:

    在批处理脚本中试试这个:

    @echo off &setlocal
    set "startdir=X:\start\folder"
    for /d /r "%startdir%" %%i in (*.*) do copy readme.txt "%%~i"
    endlocal
    

    【讨论】:

    • 我用您提供的内容制作了一个 copy.bat,但没有用:code @echo off &setlocal set "startdir=C:\Work_VideoClasses" for /d /r "%startdir%" %%i in (.) do copy readme.txt "%%~i" endlocalcode
    • 检查此测试代码的输出:@echo off &setlocalset "startdir=C:\Work_VideoClasses"for /d /r "%startdir%" %%i in (*.*) do echo copy readme.txt "%%~i"endlocal
    【解决方案2】:
    @echo off
    FOR /R %%f in (.) DO xcopy readme.txt %%f
    

    您将 .bat 文件保存在与 readme.txt 相同的位置,并且子目录必须与 readme.txt 位于同一文件夹中: 即

    Desktop
        readme.txt 
        name.bat
        Sub1
        Sub2
        Sub3
    

    等等……

    【讨论】:

    • 我用您提供的内容制作了一个 copy.bat,并完全按照您说的做,但它没有工作:code @echo off FOR /R %%f in (.) DO xcopy readme .txt %%fcode
    【解决方案3】:

    下面是ReNamer 的 PascalScript 代码,它将在每个已处理的文件夹中创建一个 Readme.txt 文件。

    该脚本在预览期间执行,因此为了安全起见,每次预览时它都会询问您是否创建Readme.txt。该脚本使用ReadmeText 常量作为内容,但也可以从系统上的文件复制Readme.txt 文件的内容。如果一个文件尚不存在,它只会创建一个 Readme.txt 文件。将所有文件夹放入 ReNamer 并使用此脚本。

    const
      ReadmeName = 'Readme.txt';
      ReadmeText = 'Content of Readme file goes here!';
    var
      Initialized: Boolean;
      ReadmePath: WideString;
      DoCreateReadmeFile: Boolean;
    begin
      if not Initialized then
      begin
        Initialized := True;
        DoCreateReadmeFile := DialogYesNo('Create Readme file this time?');
      end;
      if WideDirectoryExists(FilePath) then
        ReadmePath := WideIncludeTrailingPathDelimiter(FilePath)
      else
        ReadmePath := WideExtractFilePath(FilePath);
      ReadmePath := ReadmePath + ReadmeName;
      if DoCreateReadmeFile and not WideFileExists(ReadmePath) then
        FileWriteContent(ReadmePath, ReadmeText);
    end.
    

    编辑

    下面的脚本将获取SourceFilePath 定义的源文件并将其复制到目标文件夹。如果源文件不存在,它也会警告你。

    const
      TargetFileName = 'Readme.txt';
      SourceFilePath = 'C:\Temp\Readme.txt';
    var
      Initialized: Boolean;
      TargetFilePath: WideString;
      DoCreateFiles: Boolean;
    begin
      if not Initialized then
      begin
        Initialized := True;
        if WideFileExists(SourceFilePath) then
          DoCreateFiles := WideDialogYesNo('Create "'+TargetFileName+'" files this time?')
        else
          WideShowMessage('Source file does not exist!'+#13#13+SourceFilePath);
      end;
      if WideDirectoryExists(FilePath) then
        TargetFilePath := WideIncludeTrailingPathDelimiter(FilePath)
      else
        TargetFilePath := WideExtractFilePath(FilePath);
      TargetFilePath := TargetFilePath + TargetFileName;
      if DoCreateFiles and not WideFileExists(TargetFilePath) then
        WideCopyFile(SourceFilePath, TargetFilePath, False);
    end.
    

    【讨论】:

    • 非常感谢,但是其他文件呢? f.e.一个 JPG - 如果你能给出一个文件的路径,并将这个文件复制到你在 ReNamer 中加载的每个目录,那就太棒了。
    • @WHOMEZz 我已经编辑了帖子并添加了一个脚本,该脚本将获取SourceFilePath 定义的源文件并将其复制到目标文件夹。
    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2018-03-13
    • 2019-08-06
    • 2017-08-14
    • 2019-11-18
    相关资源
    最近更新 更多