nested forfiles: path and extension filter

2019-05-06 06:15发布

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]

2条回答
做自己的国王
2楼-- · 2019-05-06 06:35

FORFILES seems to be able to recognise \" as the way of escaping inner ". So, the following should work:

forfiles /p c:\Users /m *Signatures* /c "cmd /c forfiles /p @path /m *.htm /c \"cmd /c echo @path\""
查看更多
Ridiculous、
3楼-- · 2019-05-06 06:41

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
  )
)  
查看更多
登录 后发表回答