Jump to content

{Batch] Find file on the CD-ROMs


Recommended Posts

HI :)

My little script to finde a file on the CD-ROMs

IF EXIST "D:\OEM\RunOnce\Auto Setup.bat" set CDROM=D:
IF EXIST "E:\OEM\RunOnce\Auto Setup.bat" set CDROM=E:
IF EXIST "F:\OEM\RunOnce\Auto Setup.bat" set CDROM=F:
IF EXIST "G:\OEM\RunOnce\Auto Setup.bat" set CDROM=G:
...
IF EXIST "Z:\OEM\RunOnce\Auto Setup.bat" set CDROM=Z:
START "" /WAIT "%CDROM%\OEM\RunOnce\Auto Setup.bat"

But how can i do if the file gets not found that it will write a ECHO message like put the CD in and press enter to try agan.

Edited by Outbreaker
Link to comment
Share on other sites


You just need to to do something like this:


SET CDROM=NONE
IF EXIST "D:\OEM\RunOnce\Auto Setup.bat" set CDROM=D:
IF EXIST "E:\OEM\RunOnce\Auto Setup.bat" set CDROM=E:
IF EXIST "F:\OEM\RunOnce\Auto Setup.bat" set CDROM=F:
IF EXIST "G:\OEM\RunOnce\Auto Setup.bat" set CDROM=G:
...
IF EXIST "Z:\OEM\RunOnce\Auto Setup.bat" set CDROM=Z:
IF "%CDROM%"=="NONE" goto NOCD
START "" /WAIT "%CDROM%\OEM\RunOnce\Auto Setup.bat"
goto end
:NOCD
Echo No CDROM unit found
:END

Link to comment
Share on other sites

The "usual way" is something like:

FOR %%A IN ( Z Y X W V U T S R Q P O N M L K J I H G F E D ) DO (
IF EXIST "%%A:\OEM\RunOnce\Auto Setup.bat" set CDROM=%%A:&GOTO :Out_of_loop
)
ECHO Echo No CDROM unit found
PAUSE
GOTO :EOF
:Out_of_loop
ECHO HERE are the other commands

Problem is that in some PC's a Sd card slot or a stoopid multi-card readers provide a drive letter even if no media is present and this will cause a popup window when you try to access the device.

Something like this should work, however:


For /F "tokens=3,5 delims=\ " %%A IN ('REG QUERY HKLM\SYSTEM\MountedDevices /s ^|FIND "\DosDevices\" ^| FIND "4300640052006F006D"') DO IF EXIST "%%A\OEM\RunOnce\Auto Setup.bat" set CDROM=%%A:&GOTO :Out_of_loop
ECHO Echo No CDROM unit found
PAUSE
GOTO :EOF
:Out_of_loop

"4300640052006F006D" is the binary for "CdRom" which should be present in any dosdevice that represent a CD or DVD drive.

jaclaz

Link to comment
Share on other sites

This would be my suggestion, (although the cdrom variable isn't guaranteed to be a CD-ROM).

bat_file.cmd

@ECHO OFF
SETLOCAL

(SET CDROM=)
(SET MYBAT=OEM\RunOnce\Auto Setup.bat)

FOR /F %%# IN ('MOUNTVOL^|FINDSTR [C-Z]:\\') DO CALL :SUB %%#
IF NOT DEFINED CDROM ECHO=Correct CD-ROM Drive not found!
GOTO :EOF

:SUB
(SET CDROM=%1)
IF EXIST "%CDROM%%MYBAT%" CALL "%CDROM%%MYBAT%"

<Edit />

Fixed

Edited by Yzöwl
edited because I didn't check my own code...
Link to comment
Share on other sites

@Yzöwl

I am not sure to get it. :unsure:

The CDROM variable is set to C:\ on first run, unless you add a

SET CDROM=

after the IF EXIST:

@ECHO OFF
SETLOCAL

(SET CDROM=)
(SET MYBAT=OEM\RunOnce\Auto Setup.bat)

FOR /F %%# IN ('MOUNTVOL^|FINDSTR [C-Z]:\\') DO CALL :SUB %%#
IF NOT DEFINED CDROM ECHO=Correct CD-ROM Drive not found!
GOTO :EOF

:SUB
IF DEFINED CDROM GOTO :EOF
(SET CDROM=%1)
IF EXIST "%CDROM%%MYBAT%" CALL "%CDROM%%MYBAT%"
(SET CDROM=)

the CDROM is always defined as C:\ and the IF EXIST is never executed for any other drive but C:, if it is, then you have the same problem I was talking about the accessing of devices with letters but no actual media.

jaclaz

Link to comment
Share on other sites

One question would be, where are we running this batch file? It might be worthwhile to see if using vbscript or powershell for this might be feasible depending on where this script is running, for what it's worth.

Link to comment
Share on other sites

One question would be, where are we running this batch file? It might be worthwhile to see if using vbscript or powershell for this might be feasible depending on where this script is running, for what it's worth.

With all due respect :), why everytime someone is having some fun with good ol' BATCH, someone else comes around saying that VBS or POWERSHELL would be better? :unsure:

I was actually expecting gunsmokingman to post one of his nice .vbs's .... :angel

jaclaz

Link to comment
Share on other sites

Here is a VBS script to see if the optical drive is empty.

Save as OpticalDriveChk.vbs


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Chk, Drv, Obj
Chk = False
'-> Loop Threw The Drives
For Each Obj In Fso.Drives
'-> Check To See If The Drive Ready And It A Optical Type
If Obj.IsReady And Obj.DriveType = 4 Then
Drv = Obj.DriveLetter
Chk = True
End If
Next
'-> Check The Results
If Chk = True Then
WScript.Echo "Confirm " & Drv
Else
WScript.Echo "Empty Optical Drive"
End If

Link to comment
Share on other sites

You think that's being clever…

Nobody mentioned WMIC!

@ECHO OFF&SETLOCAL ENABLEEXTENSIONS
(SET CDROM=)
FOR /F "USEBACKQ TOKENS=2 DELIMS==" %%# IN (`WMIC DATAFILE WHERE^
"FSNAME='CDFS' AND PATH='\\OEM\\RUNONCE\\' AND FILENAME='AUTO SETUP' AND EXTENSION='BAT'"^
GET Name /VALUE^|FIND "="`) DO SET "CDROM=%%~d#"&SET "FPATH=%%#"
IF NOT DEFINED CDROM GOTO :EOF
REM PUT YOUR COMMANDS BENEATH HERE
ECHO=%%CDROM%%=%CDROM%
CALL "%FPATH%"

Link to comment
Share on other sites

why everytime someone is having some fun with good ol' BATCH, someone else comes around saying that VBS or POWERSHELL would be better? :unsure:

Not every time. I tend to say that a lot myself (because I think it's often very much the case and in many ways -- jscript is nice too), but for ridiculously simple things (e.g. starting an installer with a couple switches) it's just overkill.

Link to comment
Share on other sites

You think that's being clever…

Nobody mentioned WMIC!

And nobody mentioned fsutil...! ;):

http://www.msfn.org/board/index.php?showtopic=143446

@ECHO OFF
FOR /F "tokens=1,2 delims=\" %%A IN ('MOUNTVOL^|FINDSTR [C-Z]:\\') DO (
FOR /F "tokens=1,* delims=-" %%B IN ('fsutil fsinfo drivetype %%A^|FIND /I "CD-ROM"') do (
ECHO Checking drive %%B ...
IF EXIST "%%B\OEM\RunOnce\Auto Setup.bat" CALL "%%B\OEM\RunOnce\Auto Setup.bat"&GOTO :EOF
)
)
ECHO \OEM\RunOnce\Auto Setup.bat NOT FOUND
PAUSE

jaclaz

Link to comment
Share on other sites

why everytime someone is having some fun with good ol' BATCH, someone else comes around saying that VBS or POWERSHELL would be better? :unsure:

Not every time. I tend to say that a lot myself (because I think it's often very much the case and in many ways -- jscript is nice too), but for ridiculously simple things (e.g. starting an installer with a couple switches) it's just overkill.

That depends on your view of simple. I prefer using something other than cmd simply because nothing ever stays "ridiculously simple" in my world. Give it time, it'll snowball into something that has to be migrated into a compiled app at some point, so I might as well use VBS or a Powershell cmdlet now, to try and stave off the inevitable longer.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...