Solved

Batch File to Search all drives

Posted on 2011-02-21
8
806 Views
Last Modified: 2012-05-11
I'm trying to right a batch file to search all drives for the ghost32.exe file and also to search all drives for the actual ghost file and once it finds both to actually run ghost to clone the machine and use the image file found. I need to replace the "d:\image.gho" with the path where the image file is actually found through the search because the drive letter may not always be "D". This will all be done from PE so I'm editing the startnet.cmd file in PE.  Your help is greatly appreciated. This is what I have so far:

wpeinit

@echo off & setLocal EnableDELAYedeXpansion

for %%d in (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 (
  if exist %%d: (
      for /f "tokens=* delims= " %%a in ('dir/b %%d:\ghost32.exe 2^>nul') do (
        "%%a" -clone,mode=load,src=d:\image.gho,dst=1
      )
  )
)

wpeutil reboot


Thanks again
0
Comment
Question by:Tlavette
  • 4
  • 4
8 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 34946533
This will only search the root of the drives found; is that intentionally?
Anyway, try this:

@echo off & setLocal EnableDELAYedeXpansion
for %%d in (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 (
  if exist %%d: (
      for /f "tokens=* delims= " %%a in ('dir /b %%d:\ghost32.exe 2^>nul') do (
        "%%d:\%%a" -clone,mode=load,src=%%d:\image.gho,dst=1
      )
  )
)
wpeutil reboot

Open in new window

0
 

Author Comment

by:Tlavette
ID: 34946691
oBda,

I had a /s in as well to search the subfolders but don't think I need it. the only question that i have in your solution is will this also search for the image.gho file? for instance if I boot from a thumb stick the and have the image.gho file on a dvd then ghost32, which is on the thumb stick might be on drive D if that what the thumbstick gets mounted to and the the image.gho file would be on say drive E if that what the CD drive was mounted too?
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 34946823
Sorry, misunderstood you.
Here's an enhanced version:

@echo off
setlocal EnableDelayedExpansion
set DriveList=C D E F G H I J K L M N O P Q R S T U V W X Y Z

set Ghost=
echo Searching for ghost.exe:
for %%a in (%DriveList%) do (
  echo Drive %%a ...
  if exist "%%a:\ghost.exe" (
    echo ... found.
    set Ghost=%%a\ghost.exe
    goto :FindImage
  )
)
:FindImage
if "%Ghost%"=="" (
  echo ghost.exe not found in any of the following drives:
  echo %DriveList%
  pause
  goto :eof
)
set Image=
echo Searching for image.gho:
for %%a in (%DriveList%) do (
  echo Drive %%a ...
  if exist "%%a:\image.gho" (
    echo ... found.
    set Image=%%a\image.gho
    goto :Clone
  )
)
:Clone
if "%Image%"=="" (
  echo image.gho not found in any of the following drives:
  echo %DriveList%
  pause
  goto :eof
)
echo Starting cloning ...
ECHO "%Ghost%" -clone,mode=load,src=%Image%,dst=1
ECHO wpeutil reboot

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:Tlavette
ID: 34946833
oBda,

I tried your scripted and it did find the ghost32.exe file and it ran it, but was looking for the image.gho file on the same drive as the ghost32 file and it didn't find the image file. Normally I might have a small image file on the thumb stick so it would be on the same drive letter, but sometimes and have larger images that I put on DVD and it would be on a different drive letter.

Thanks again for the help
0
 
LVL 84

Expert Comment

by:oBdA
ID: 34946888
Just in case: I just noticed that I forgot to mention in the updated script above that it's in test mode; it will only display the "ghost" and "wpeutil" commands, instead of executing them.
Simply remove the capitalized ECHOs to run it for real.
0
 

Author Comment

by:Tlavette
ID: 34947380
oBda,

This worked beautifully. The only thing that I had to tweak was changed the file name from ghost.exe to ghost32.exe and I had to add the colon (:) after the "%%a" for each "set" command. After doing that it worked like a charm. Thanks for the quick response and quick solution. Any suggested reading in this area would be greatly appreciated as well.

Thanks
0
 

Author Closing Comment

by:Tlavette
ID: 34947388
Great solution...quick and precise.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 34947461
A very nice scripting resource is http://www.robvanderwoude.com/
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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