Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested forfiles: path and extension filter

is there any possibility to nest two forfile commands so that I can filter by pathname and by extension and then run a command only on those double filtered files?

By example I'd like to get all Outlook HTML-signatures of all users. I can do this by

forfiles /s /p c:\Users /m *Signatures* /c "cmd /c forfiles /s /p @path /m *.htm"

But this will only display the filenames because it's the default behavior of forfiles to call cmd /c echo @file.

Changing this doesn't work because then I'd need to set the /c-option in the inner forfiles command which requires to set the command in quotes resulting in double quotes:

forfiles /s /p c:\Users /m *Signatures* /c "cmd /c forfiles /s /p @path /m *.htm /c "cmd /c echo @path""

How can I escape the inner quotes or use some different approach to run any command on all files filtered by a substring of path and the file extension?

Kind regards
sc911

[edit] forgot /s for recursive searching [/edit]

like image 254
sc911 Avatar asked Jan 20 '23 02:01

sc911


1 Answers

Instead of forfiles you can use two nested FOR commands.

see the following one-liner example to test in a cmd prompt

@for /d %d in (c:\Users\*signature*) do @for %f in (%d\*.htm) do @echo %f

and use this code as a skeleton to include in a BAT file

for /d %%d in (c:\Users\*signatures*) do (
  for %%f in (%%d\*.htm) do (
    echo %%f
  )
)  
like image 105
PA. Avatar answered Jan 31 '23 03:01

PA.