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

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


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"


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.