Page 1 of 1

Find and open a subfolder (using 'for' and 'start')

Posted: 21 May 2020 15:44
by stephjo
Hello,

I'd like to find and open subfolder by name.

I have the simple command line

Code: Select all

dir /a:d /s /b |find "amazon" |find /v "bak"
That is, find (all) folders which have the word "amazon", but not with "bak" (e.g. I want "c:\mine\amazon" but not "c:\mine\amazon\bak" )

Then, I'd like to pipe this to start command and open folders.

How can I create a batch file that takes "amazon" as argument and opens the folders?

Something like findfolder.bat:

Code: Select all

for %%a in (dir /a:d /s /b |find "%~1" |find /v "bak") do start "" "%%~a" 
Thank you,
-Steph

Re: Find and open a subfolder (using 'for' and 'start')

Posted: 21 May 2020 16:08
by ShadowThief
If you're going to pipe commands together in a for loop, you need to use the /f option, put the command in single quotes, remove delimiters to handle paths with spaces, and escape the pipes.

Code: Select all

for /f "delims=" %%a in ('dir /a:d /s /b ^|find "%~1" ^|find /v "bak"') do start "" "%%~a"