Solved

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

Posted on 2012-04-01
25
910 Views
Last Modified: 2012-04-04
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
0
Comment
Question by:icecom4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 4
  • +2
25 Comments
 
LVL 7

Expert Comment

by:mbkitmgr
ID: 37794805
: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
 

Expert Comment

by:pourmonfared
ID: 37794996
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
 

Author Comment

by:icecom4
ID: 37796832
@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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

Expert Comment

by:Bill Prew
ID: 37797044
Will the BQUEST folder always be at the root, or could it be anywhere?

~bp
0
 

Author Comment

by:icecom4
ID: 37797678
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
 

Expert Comment

by:pourmonfared
ID: 37798014
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
 
LVL 56

Accepted Solution

by:
Bill Prew earned 450 total points
ID: 37798372
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37799014
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37799068
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37799123
That didn't find the folder for me Paul.

~bp
0
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 50 total points
ID: 37799141
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37800557
That didn't find the folder for me Paul.

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37800645
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37800667
@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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37800960
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37800979
I'm running on Win7.

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37801001
bill

I'm running XP. Here's the results for a simulation of the last test you performed. Please confirm...
simulated test result
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37801376
@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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37802057
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
 

Author Comment

by:icecom4
ID: 37804146
@ 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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37804153
Just add your logic to the :NotFound routine.

~bp
0
 

Author Comment

by:icecom4
ID: 37804179
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37804186
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37804799
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37805023
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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Part One of the two-part Q&A series with MalwareTech.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question