Solved

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

Posted on 2012-04-01
25
849 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
  • 10
  • 8
  • 4
  • +2
25 Comments
 
LVL 5

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
 
LVL 51

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 51

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 51

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 51

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 51

Expert Comment

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

~bp
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 51

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 51

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 51

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 51

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 51

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now