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?

Improve company productivity with a Business Account.Sign Up

x
 
Bill PrewConnect With a Mentor Commented:
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
0
 
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
0
 
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

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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

0
 
Bill PrewCommented:
Will the BQUEST folder always be at the root, or could it be anywhere?

~bp
0
 
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
0
 
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

0
 
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

0
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
That didn't find the folder for me Paul.

~bp
0
 
Paul TomasiConnect With a Mentor Commented:
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

0
 
Bill PrewCommented:
That didn't find the folder for me Paul.

~bp
0
 
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.
0
 
Bill PrewCommented:
@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
0
 
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?
0
 
Bill PrewCommented:
I'm running on Win7.

~bp
0
 
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
0
 
Bill PrewCommented:
@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
0
 
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)
0
 
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.
0
 
Bill PrewCommented:
Just add your logic to the :NotFound routine.

~bp
0
 
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
0
 
Bill PrewCommented:
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
0
 
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)
0
 
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.