batch script to search multiple drives for folder, if not exist...

please help me with the following...

a batch script to search multiple drives (C D E) for folder named "bquest", if not exist (at least 1 folder) then goto :notexist

Thanks in advance!

ps
batch only for this
icecom4Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbkitmgrCommented:
:CDRV
if not exist c:\bquest goto DDRV
Echo Exists
Goto End

:DDRV
if not exist c:\bquest goto EDRV
Echo Exists
Goto End

:EDRV
if not exist c:\bquest goto END
Echo Exists
Goto End

:END
pourmonfaredCommented:
Hi,
One line solution ! ;)
@echo off
FOR %%A IN (C D E F G H I J) DO IF EXIST %%A:\bquest Echo Exists in %%A

Open in new window

icecom4Author Commented:
@mbk
I don't have known directories for this, so it has to search

@pour
I tried this, and it seems to move on to next line no matter what.  I tried your line as it was, and I also tried changing it.  If I change to "if not exist" then it will still goto next line no mater what

FOR %%A IN (C D E F G) DO IF not EXIST %%A:\bquest Echo not Exists in %%A
goto notexist <--no mater what it goes to this line
) else ( 
goto exist

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Bill PrewIT / Software Engineering ConsultantCommented:
Will the BQUEST folder always be at the root, or could it be anywhere?

~bp
icecom4Author Commented:
it would be anywhere on the drives

This task is only designed to find at least one bquest folder.  If it cannot find any, it will terminate the batch.  However if it finds at least one folder, it will proceed.  

So I want it to search the drives, as soon as it finds one, goto exists, however if it cannot find any goto notexists
pourmonfaredCommented:
hi @ice complete solution, search all drives and folder for finding sub-folder with appropriate name :
@echo off

FOR %%A IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
cd %%A:\
FOR /D /r %%G in (bquest) DO (
Echo *** %%G is exist **
goto END
))
echo not exist
:END

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Okay, this is a little trickier than I thought it might be.  The deal with the FOR /D /R is that you need to match against a wildcard, not just a directory name.  So I adjusted for that below.  Also, this is how I would structure the code, you may want to remove some of the ECHO's once you know it works right.  In addition, it will stop searching drives once it finds the folder, but it will have to search the whole drive it is processing, even once it finds the first match there.
@echo off
setlocal EnableDelayedExpansion

set Found=N
for %%A in (C D E) do (
  echo Searching drive [%%A]
  call :LookForDir "%%A:\"
  if "!Found!" == "Y" goto :FoundIt
)

:NotFound
  echo Directory not found
  goto :EOF

:FoundIt
  echo Directory found
  goto :EOF

:LookForDir [base-dir]
  for /D /R "%~1" %%B in (bquest.*) do (
    echo Found file [%%B]
    set Found=Y
    exit /b
  )
  exit /b

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul TomasiCommented:
Hello chaps

This will do the job quite nicely !!
for %%a in (c d e) do dir %%a:\ /ad /b /s | findstr "\<bquest\>" >nul && goto exists || goto notexist
exit /b

:exists
exit /b

:notexist
exit /b

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Paul,

That would also match any folder that had bquest in their name, even if the name was longer than that under circumstances like:

"xxx bquest xxx"
"xxx-bquest-xxx"
"xxx.bquest.xxx"
"xxx(bquest)xxx"

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
That didn't find the folder for me Paul.

~bp
Paul TomasiCommented:
Apologies... I meant:
for %%a in (c d e) do dir %%a:\ /ad /b /s | findstr /b /e "bquest" >nul && goto exists || goto notexist
exit /b

:exists
exit /b

:notexist
exit /b

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
That didn't find the folder for me Paul.

~bp
Paul TomasiCommented:
bill

I checked the code against 'bquest' as well as the variants you posted in http:#37799068. It worked fine for me.

However, because your results differ from mine I will doublecheck my results.
Bill PrewIT / Software Engineering ConsultantCommented:
@Paul,

I used "temp" as a folder name, but here's the test I did.  I created a few folders on a small drive called G: as follows.

[C:\]dir /b /ad g:\*temp*
temp
xxx temp xxx
xxx(temp)xxx
xxx-temp-xxx
xxx.temp.xxx
xxx_temp_xxx

I then did the following command, sort of the core part of your last solution, but no folders are returned, not even the g:\temp folder.

[C:\]dir g:\ /ad /b /s | findstr /b /e "temp"

~bp
Paul TomasiCommented:
bill

Please run the following testbed code (no need to edit it). You will notice the test you performed in your last comment does not match the results given by this testbed code.
@echo off

pushd g:\ 2>nul
if errorlevel 1 (
  md "%temp%\%~nx0.tmp" 2>nul
  subst g: "%temp%\%~nx0.tmp"
  pushd g:\
)

md bquest "x bquest x" x(bquest)x x-bquest-x x.bquest.x x_bquest_x
popd

echo on
dir /b /ad g:\*bquest*

@pause
dir /b /ad g:\ | findstr /b /e "bquest"

@subst g: /d 2>nul
@rd "%temp%\%~nx0.tmp" /q /s 2>nul

Open in new window

What O/S did you perform your previous test on? Is it a problem with spaces around the pipe symbol?
Bill PrewIT / Software Engineering ConsultantCommented:
I'm running on Win7.

~bp
Paul TomasiCommented:
bill

I'm running XP. Here's the results for a simulation of the last test you performed. Please confirm...
simulated test result
Bill PrewIT / Software Engineering ConsultantCommented:
@Paul,

The reason that last test works, while the prior doesn't is the removal of the /S from the DIR command.  But that is needed to search ALL the folders on the drive for the bquest folder.  Add the /S for your latest test...

~bp
Paul TomasiCommented:
bill

Ah! Of course... I changed my code from my original draft which did include the /S options. I started cutting corners when I switched to using FINDSTR...

(Never liked the DIR /S... options anyway. Can't break out of it once the target is found)
icecom4Author Commented:
@ pour
no matter what it says the folder exists.  I replaced "bquest" with "gtr5wvxCvStyV" and it says it still exists :(

@ bill
Your code works.  However, it seems that it requires the "exit /b" to recycle from C to D to E...etc. Thats fine, but it seems that this stops the script after the last cycle.  but how do I get it to proceed if it does not find the folder?  The reason I need it to proceed is so that I can echo a few statements about the file not being there.
Bill PrewIT / Software Engineering ConsultantCommented:
Just add your logic to the :NotFound routine.

~bp
icecom4Author Commented:
my issue was that it never made it as far as the :EOF where I had all my closing remarks and last few commands.  But I did what you said, and added right under :notfound and it works like a charm.

Thanks again Bill
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome.

FYI, the :EOF label is special, it actually means go to the end of the subroutine, or main script, depending on the context.  So even if you have a :EOF label in the script, it will never actually get control via a "goto :EOF".

~bp
Paul TomasiCommented:
Points for my efforts... Thank you. However, I still have plenty of steam left in me but sadly pushed for time and opportunity....

The solution is similar to what I would have proposed if all else failed... and that option was already taken by billprew :)

(I thnk I'll revisit this one on a rainy day)
Paul TomasiCommented:
Apologies for revisiting this question again so soon.

Just had a little play with DOS and came up with the following solution (that DOES actually work this time).
(dir /a-d /b /s c:\ d:\ e:\) | findstr /e /c:"\bquest" && goto exists || goto notexist
exit /b

:exists
exit /b

:notexist
exit /b

Open in new window

The problem was the missing brackets. I knew it had to be there somewhere...


NOTE: To suppress output, add '>nul' to line 1 as follows:
(dir /a-d /b /s c:\ d:\ e:\) | findstr /e /c:"\bquest" >nul && goto exists || goto notexist

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.