문제

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.

도움이 되었습니까?

해결책

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

Escape the | using a ^ before it.

다른 팁

The CMD processor is just too limited to do what you want here, pipe is not legal in this context. You could kludge it with replacing your ECHO stub with a CALL to another batch file, but even then you will still have problems. See Want to batch move all files from directories into the parent directory and delete the subdirectory for my quick comparion of scripting environments options.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top