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)
Asked
Active
Viewed 3.2k times
9
-
Possible duplicate of [Search for files in a batch script and process those files?](https://stackoverflow.com/questions/1447703/search-for-files-in-a-batch-script-and-process-those-files) – phuclv Mar 01 '18 at 02:42
2 Answers
18
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"

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
11
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.

Steven Christenson
- 261
- 4
- 3