Search code examples
windowsbatch-filefindstr

Batch Script to delete files based on findstr regex


I'm trying to delete some files with a batch script, based on a regular expression. What I have is:

FOR /f "tokens=*" %%a in ('dir /b | findstr MY_REGEX_HERE') DO ECHO %%a 

I know my inner command works on its own, giving me the list of directories, but when I embed it in the for loop like this I get an error | was unexpected at this time. Is piping not allowed within FOR loop commands? Or do I need to escape it or something?

Any help on how I can do this would be great.


Solution

  • FOR /f "tokens=*" %%a in ('dir /b ^| findstr MY_REGEX_HERE') DO ECHO %%a 
    

    Escape the | using a ^ before it.