bat 函数在文件夹和子文件夹中查找文件并对其执行某些操作。

发布于 01-02 07:15 字数 80 浏览 5 评论 0原文

我需要在文件夹和所有子文件夹中查找具有特定文件名的所有文件(例如 main.css),然后对其执行某些操作(例如重命名、移动、删除、添加文本行等)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放血2025-01-09 07:15:33

这就是您所需要的:

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

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

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

This is what you need:

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

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

for /R %f in (*.css) do @echo "%f"
吻风2025-01-09 07:15:33

虽然这将遍历目录树:

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

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

    DirectoryA
        A1
        A2

for /R 操作将给出 DirectoryA/main.css 的 %f,然后 DirectoryA/A1/main.css 等等,即使 main.css 不在任何这些目录中。因此,为了确保确实存在一个文件(或目录),您应该这样做:

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

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

以上至少是它在 Windows 8 中的工作方式。

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 /R operation will give %f of DirectoryA/main.css, then DirectoryA/A1/main.css and 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文