windows bat 函数在文件夹和子文件夹中查找文件并对其进行处理。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9143628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:11:25  来源:igfitidea点击:

bat function to find a file in folder and subfolders and do something with it.

windowsbatch-filefindbatch-processing

提问by Davinel

I need to find all files with specific filename(for example main.css) in folder and all subfolders and then do something with it(eg. rename, move, delete, add text line, etc)

我需要在文件夹和所有子文件夹中找到具有特定文件名的所有文件(例如 main.css),然后用它做一些事情(例如。重命名、移动、删除、添加文本行等)

回答by David Heffernan

This is what you need:

这是你需要的:

for /R %f in (main.css) do @echo "%f"

Naturally you would replace echowith whatever it is you wish to do to the file. You can use wildcards if you need to:

自然地,您将替换echo为您希望对文件执行的任何操作。如果需要,可以使用通配符:

for /R %f in (*.css) do @echo "%f"

回答by Steven Christenson

While this will traverse the directory tree:

虽然这将遍历目录树:

for /R %f in (main.css) do @echo "%f"

It doesn't actually match file names. That is, if you have a tree:

它实际上并不匹配文件名。也就是说,如果你有一棵树:

    DirectoryA
        A1
        A2

the for /Roperation will give %f of DirectoryA/main.css, then DirectoryA/A1/main.cssand so on even if main.css is not in any of those directories. So to be sure that there really is a file (or directory) you should do this:

用于/ R操作将给予的%F DirectoryA / main.css的,然后DirectoryA / A1 /的main.css等即使main.css的是不是在任何这些目录的。因此,要确保确实存在文件(或目录),您应该这样做:

for /R %f in (main.css) do @IF EXIST %f @echo "%f"

Also, be aware that you do need to quote the file name because if the path or file contains spaces the directory walking may blow up.

另外,请注意您确实需要引用文件名,因为如果路径或文件包含空格,则目录遍历可能会爆炸。

The above is, at least, how it is working in Windows 8.

以上是,至少,它是如何在 Windows 8 中工作的。