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

25,744

Solution 1

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"

Solution 2

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.

Share:
25,744
Davinel
Author by

Davinel

Updated on July 09, 2022

Comments

  • Davinel
    Davinel almost 2 years

    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)