Sign in to follow this  
Followers 0
Outbreaker

{Batch] Find file on the CD-ROMs

38 posts in this topic

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
0

Share this post


Link to post
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

0

Share this post


Link to post
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

0

Share this post


Link to post
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...
0

Share this post


Link to post
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

0

Share this post


Link to post
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.

0

Share this post


Link to post
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

0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

hahahahahahaha - awesome! :)

0

Share this post


Link to post
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%"

0

Share this post


Link to post
Share on other sites
:P
0

Share this post


Link to post
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.

0

Share this post


Link to post
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

0

Share this post


Link to post
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.

0

Share this post


Link to post
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

This script looks better then my noob script ^_^ but i have a question to understand this script better how it works.

I played little bit around and change [C-Z] to [A-Z] i didn't get this "No Disk" error on the Floppy driver but if i change it to [A-B] i get the error that's what i don't get [A-Z] and [A-B] should have the same effect or not O_o ?

Edited by Outbreaker
0

Share this post


Link to post
Share on other sites

No it is the normal behaviour of the script: it test if the CDROM variable was set before echoing an error.

And the CDROM variable will took the last letter of the mounted volumes even if the file doesn't exist.

0

Share this post


Link to post
Share on other sites

Ok after testing all this script i have some questions :)

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

How can i do it so the script will also search in the Harddisk drives and USB flash drives ?

Is there also a batch script that can check like the VBScript if it can read on the driver and then searches for the file so i will get no popup errors on Card Readers and ohter drives.

Edited by Outbreaker
0

Share this post


Link to post
Share on other sites

1:\ Check to see if drive ready

2:\ Checks for file on drive

Save as a VBS file


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Chk, File, Obj
'-> Path To File Without Drive Letter
File = ":\SOME_FOLDER\FILE.NAME"
Chk = False
'-> Loop Threw The Drives
For Each Obj In Fso.Drives
'-> Drive Is Ready And Check To See File Exists On Drive
If Obj.IsReady And Fso.FileExists(Obj.DriveLetter & File) Then
File = Obj.DriveLetter & File
Chk = True
End If
Next
'-> Check The Results
If Chk = True Then
WScript.Echo "Confirm " & File
Else
WScript.Echo "Can Not Find File"
End If

0

Share this post


Link to post
Share on other sites

I tested it but i get always a drive A: error is there no workaround for this i think this also happens to other drives like Card Readers or is it olny the drive A that makes this problem ?

The olny way i found that could work is this here hop someone can make a script with the option 2 and 4 or do even a better script :)

http://www.devguru.com/technologies/vbscript/quickref/drive_drivetype.html

Edited by Outbreaker
0

Share this post


Link to post
Share on other sites

The second VBS scripts works fine on my computer. You have to fill below in

File = ":\SOME_FOLDER\FILE.NAME"

The script only reports active drives, so if there nothing in the drive it

ignores that drive.

0

Share this post


Link to post
Share on other sites

I set the path to the file and there is also no disk in the floppy drive but if i start the VBScript i get a "No Disk" Error MsgBox :( Do you have a also a floppy drive artefact in your Computer ?

Edited by Outbreaker
0

Share this post


Link to post
Share on other sites

No I dont have a floppy disk, but I tested on sdhc card.

Try this script, it going to list each drive as either active or empty


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Obj, Z1, Z2
For Each Obj In Fso.Drives
If Obj.IsReady Then
Z1 = Z1 & vbCrLf & "Active" & vbTab & Obj
Else
Z2 = Z2 & vbCrLf & "Empty " & vbTab & Obj
End If
Next

MsgBox Z1 & vbCrLf & Z2

My Results, with no errors.


---------------------------

---------------------------


Active C:

Active D:

Active E:

Active F:

Active G:

Active I:

Active J:



Empty H:

Empty K:

Empty L:

Empty M:

Empty N:
---------------------------
OK
---------------------------

0

Share this post


Link to post
Share on other sites

My Results, with a error on A: ->

A: -> Empty (Floppy disk drive)

C: -> Active (Intern SATA/AHCI Hard disk drive)

D: -> Active (Extern USB 2.0 Hard disk drive 1)

E: -> Active (Extern USB 2.0 Hard disk drive 2)

F: -> Active (Extern USB 2.0 Hard disk drive 3)

G: -> Active (Extern USB 2.0 Hard disk drive 4)

H: -> Empty (DVD/CD-ROM disk drive)

I: -> Active (Virtual DVD/CD-ROM disk drive)

--------------------------------------------------------------------------------------------------------------------------------------------

This is the exact error i get from A: ->

TITTEL -> No Disk

MESSAGE -> Exception Processing Message c00000013 Parameters 75b6bf7c 4 75b6bf7c 75b6bf7c

--------------------------------------------------------------------------------------------------------------------------------------------

Edited by Outbreaker
0

Share this post


Link to post
Share on other sites

Is there also a batch script that can check like the VBScript if it can read on the driver and then searches for the file so i will get no popup errors on Card Readers and ohter drives.

BOTH the ones I posted do EXACTLY that. :realmad:

jaclaz

0

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.