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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stephjo
Posts: 9
Joined: 03 Feb 2014 02:39

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

#1 Post by stephjo » 21 May 2020 15:44

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

ShadowThief
Expert
Posts: 954
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#2 Post by ShadowThief » 21 May 2020 16:08

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" 

Post Reply