递归查找和替换文件 [英] Recursively find and replace files

查看:244
本文介绍了递归查找和替换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是什么之后。
我想创造一些bat文件,这将递归搜索从当前目录开始的文件,并与我公司提供的文件替换。外汇。如果我想搜索和替换test1.txt的,我打开这个小应用程序和编写text1.txt,置我要被替换的文件。

What I want to do is following. I want to create some bat file, that will recursively search for files starting from current directory and replace with the file that I provided. For ex. if I want to search and replace test1.txt, I'm opening this mini app and writing text1.txt, and placing the file that I want to be replaced with.


  • 目录

    • app.bat

    • test1.txt的//应用程序将文件夹1和文件夹2内递归搜索并替换test1.txt的
    • 找到的所有结果
    • 文件夹1

    • 文件夹2

    • Dir
      • app.bat
      • test1.txt // app will recursively search inside folder 1 and folder 2 and will replace all found results with test1.txt
      • folder 1
      • folder 2

      我在想,如果有准备去应用程序或批处理文件这个原因?

      I wonder, if there is ready to go app or bat file for this reason?

      推荐答案

      下面开始批处理文件从当前目录,递归搜索中的第一个参数给出的文件,并复制它(同名)的第二个参数给出的文件

      The Batch file below start from current directory, recursively search the file given in the first parameter and copy over it (with same name) the file given in second parameter:

      @echo off
      set targetName=%~NX1
      set replacementFile=%~F2
      call :processFolder
      goto :EOF
      
      :processFolder
      rem For each folder in this level
      for /D %%a in (*) do (
         rem Enter into it, process it and go back to original
         cd %%a
         if exist "%targetName%" (
            copy "%replacementFile%" "%targetName%" /Y
         )
         call :processFolder
         cd ..
      )
      exit /B
      

      例如:

      app test1.txt c:\data\replacementfile.txt
      

      这篇关于递归查找和替换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆