递归查找和替换文件(Recursively find and replace files)

编程入门 行业动态 更新时间:2024-10-20 16:07:57
递归查找和替换文件(Recursively find and replace files)

我想做的是跟随。 我想创建一些bat文件,它将递归搜索从当前目录开始的文件,并替换为我提供的文件。 对于前者 如果我想搜索并替换test1.txt,我打开这个迷你应用程序并编写text1.txt,并放置我想要替换的文件。

迪尔 app.bat test1.txt // app将递归搜索文件夹1和文件夹2,并将用test1.txt替换所有找到的结果 文件夹1 文件夹2

我想知道,如果因为这个原因准备好去app或bat文件?

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.

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?

最满意答案

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

@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

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

For example:

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

更多推荐

本文发布于:2023-08-07 01:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   文件   Recursively   files   replace

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!