Bat to find the file in the folder and subfolders and do something with it.

I need to find all files with a specific file name (e.g. main.css) in a folder and in all subfolders, and then do something with it (e.g. rename, move, delete, add a text line, etc.)

+4
source share
2 answers

This is what you need:

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

Naturally, you would replace echo with what you want to do with the file. You can use wildcards if you need:

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

For now, it traverses the directory tree:

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

Actually this does not match the file names. That is, if you have a tree:

  DirectoryA A1 A2 

the operation for / R will give% f from DirectoryA / main.css, then DirectoryA / A1 / main.css, etc., even if main.css is not in any of these directories. Therefore, to be sure that there really is a file (or directory), you must do this:

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

Also, keep in mind that you need to specify a file name, because if the path or file contains spaces, the directory in which walking can go may explode.

This is at least how it works on Windows 8.

+6
source

Source: https://habr.com/ru/post/1394790/


All Articles