Jump to content

Bat file help


Recommended Posts

I have a Bat file that checks for the usb drive. It was working fine for a long time but now it’s doing something different. Here is the code:

if exist D:\Install set drive=D

if exist E:\Install set drive=E

if exist F:\Install set drive=F

Simple but now when it comes to the CD-rom drive it sends a prompt saying to insert cd and click retry, cancel, or continue. If there is a CD in the CD-rom there is no issue no prompt. But why is there a prompt now and how to you stop the prompt?

Link to comment
Share on other sites


I'd like to provide a comment rather than a direct solution on this occasion.

I'm astonished how often this kind of question arises, I could understand to a degree when using CD-ROMs but not with removable drives.

All you need to do is create a Volume/Label Name for your removable drives, any script can then quickly check for that name as opposed to using a unique file/folder placed on its root.

Just a quick VBS example, it doesn't directly answer your question but could if required be altered to run from a batch file:

strVolName = "mythumb"
Set colDrives = CreateObject("Scripting.FileSystemObject").Drives
For Each drive In colDrives
If drive.isReady Then
If StrComp(drive.VolumeName, strVolName, 1) = 0 Then
WScript.echo drive.VolumeName & " is drive letter " & drive.Path
End If
End If
Next

Change the content of the quotes on the first line (case insensitive) to your drive volume name to test it!

The noticeable thing for your particular problem is that it checks to see if the drive is ready first, thus negating the not ready messages your currently getting.

Link to comment
Share on other sites

Here a cmd file that produces a VBS script that is similar to what Yzöwl posted works the same way.

Save As Usb_DriveCheck.cmd

@Echo Off
CLS
Color F9
Mode 65,5
Title Check For USB Drive
Echo.
Echo Processing the USB Drive Letter
set ChkUsb=%SystemDrive%\ChkUsb.vbs
Echo Dim Drv, ChkFile, strFile, StrD, TS, Fso : Set Fso = CreateObject("Scripting.FileSystemObject") > %ChkUsb%
Echo Set Drv = Fso.Drives >> %ChkUsb%
:: Place The Name Of The File Here
Echo ChkFile = "\i386\ZONEOC.DL_" >> %ChkUsb%
Echo strFile = Fso.GetFolder(Fso.GetParentFolderName(WScript.ScriptFullName)) ^& "\ChkUsb.cmd" >> %ChkUsb%
Echo For Each StrD In Drv >> %ChkUsb%
Echo If StrD.IsReady Then >> %ChkUsb%
Echo If Fso.FileExists(StrD ^& ChkFile) Then >> %ChkUsb%
Echo Set TS = Fso.CreateTextFile(strFile) >> %ChkUsb%
Echo Ts.WriteLine "Set USB=" ^& StrD ^& "\" : TS.Close >> %ChkUsb%
Echo Else >> %ChkUsb%
Echo Set TS = Fso.CreateTextFile(strFile) >> %ChkUsb%
Echo Ts.WriteLine "Set USB=Wrong_USB_Drive" : TS.Close >> %ChkUsb%
Echo End If >> %ChkUsb%
Echo End If >> %ChkUsb%
Echo Next >> %ChkUsb%

Start /w %ChkUsb%
Call %SystemDrive%\ChkUsb.cmd
Set %USB%=
Del %ChkUsb%
Del %SystemDrive%\ChkUsb.cmd
Echo %USB%
Pause
del %SystemDrive%\ChkUsb.cmd

Link to comment
Share on other sites

That would be fine if I use the same USB drive. This will need to be more portable then just this one drive. Meaning that the bat file can find any USB drive that has the files. I’m still puzzled why it is now all of a sudden sending a prompt when there is no CD in the CD-ROM?

Link to comment
Share on other sites

Perhaps this will work for you.

I am assuming that you have the fsutil command available.

This will skip checking against CD-ROM drives.

@echo off

for /f "tokens=*" %%a in ('fsutil fsinfo drives^|more') do (
setlocal enabledelayedexpansion
set chkdrive=%%a
set chkdrive=!chkdrive:Drives:=!
set chkdrive=!chkdrive: =!
fsutil fsinfo drivetype !chkdrive!|find /i "CD-ROM">nul
if errorlevel 1 call :doStuff !chkdrive!)

goto skipme

:doStuff
endlocal
if exist %1Install set drive=%1
goto :eof

:skipme

endlocal
echo Performing task on %drive%

You may need to change the statement in the doStuff routine to check for your folder/file.

Link to comment
Share on other sites

If you're happy to use FSUTIL which requires Admin status to run, then you could improve on Scr1ptW1zard's example still further.

Since the task is not to ignore the CD-ROM but to find the USB Drive, then this should do fine:

@Echo off&Setlocal enableextensions
For /f %%# In ('Mountvol^|Findstr [d-z]:\\') Do (
Fsutil fsinfo drivetype %%#|Find "Removable Drive">Nul&&(
If Exist %%#Install Set "ThumbDrv=%%~d#"))
If defined ThumbDrv Echo:%%ThumbDrv%%=%ThumbDrv%

Link to comment
Share on other sites

Sorry Yzowl your code didn’t work. Nothing was returned.

Can anyone explain how Scr1ptW1zard's scrip works? Mainly these two lines.

set chkdrive=!chkdrive:Drives:=!

set chkdrive=!chkdrive: =!

What are the "!" used for?

Link to comment
Share on other sites

Here is an explanation to my script:

'fsutil fsinfo drives^|more'

The output from the above command produces this output (your drive list may vary):

Drives: A:\
C:\
D:\
E:\
X:\

But we only want the drive letters, so I remove the string "Drives:" with

  set chkdrive=!chkdrive:Drives:=!

The above simply replaces the string "Drives:" with "".

The statement

  set chkdrive=!chkdrive: =!

Does a similar replacement of the space " ". This is not really needed, but I

like to be neat. :rolleyes:

The exclamation marks (!) are required when referencing environment variables

(instead of %) while enabledelayedexpansion is set.

Yzöwl's script works as well, and actually eliminates the need for what I am

performing as described above. The problem you may be having with his script

could be that your USB drive is not recognized as a "Removable Drive". I actually

have two USB drives attached to my system, and one is seen as a "Removable Drive"

while the other is seen as a "Fixed Drive". To test this, run the following command

(replace D:\ with your USB drive):

fsutil fsinfo drivetype D:\

For Yzöwl's script to work, the above command will need to return:

D:\ - Removable Drive

My script is checking all drives that are NOT a CD-ROM drive, therefore you are

receiving the desired result.

:thumbup

Link to comment
Share on other sites

Nothing was returned.
The problem you may be having with his script could be that your USB drive is not recognized as a "Removable Drive". I actually have two USB drives attached to my system, and one is seen as a "Removable Drive" while the other is seen as a "Fixed Drive"

It is your Removable Drive which therefore isn't working properly, not my code! which brings me full circle back to this!

create a Volume/Label Name for your removable drives, any script can then quickly check for that
Your refusal to do so doesn't really hold water, it's just as quick and simple to do this as to add the install folder in the first place.

In any case, if you still wished to continue to just ignore the CD-ROM querying you only need to replace

"Removable Drive"

with

/v "CD-ROM"

in my script.

Link to comment
Share on other sites

Yzowl:

Sorry but your code doesn't work at all! LOL j/k You seem to be offended by my statement. I didn't mean to offend anyone. I guess I should have been more specific in my statement. The code didn't work for me would have been a more accurate statement. I tested your code and after replacing "Removable Drive" with /v "CD-ROM" as you stated it worked like a charm. Thanks again for the help.

Scr1ptW1zard:

Thanks for the script but even more thanks for explaining it. That helped me out the most. It is nice that people are willing to write scripts for others but it's even nicer when they explain how it works. That way others can learn from the script and use it for themselves.

Thanks again for everyone's help! :)ly

Edited by Smiley357
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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