MSFN Forum: Bat file help - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Bat file help If exist... Rate Topic: -----

#1 User is offline   Smiley357 

  • Junior
  • Pip
  • Group: Members
  • Posts: 85
  • Joined: 12-October 07

Posted 10 April 2008 - 12:53 PM

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?


#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 11 April 2008 - 10:21 AM

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.

#3 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 11 April 2008 - 08:36 PM

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

Quote

@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


#4 User is offline   Smiley357 

  • Junior
  • Pip
  • Group: Members
  • Posts: 85
  • Joined: 12-October 07

Posted 14 April 2008 - 01:14 PM

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?

#5 User is offline   Scr1ptW1zard 

  • Junior
  • Pip
  • Group: Members
  • Posts: 56
  • Joined: 05-July 07

Posted 15 April 2008 - 09:58 AM

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.

#6 User is offline   Smiley357 

  • Junior
  • Pip
  • Group: Members
  • Posts: 85
  • Joined: 12-October 07

Posted 15 April 2008 - 01:45 PM

This worked perfectly. Thanks for everyone’s help!

I made a few slight changes to fit my bat file but it was exactly what I was asking for. Thanks again!

#7 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 15 April 2008 - 03:44 PM

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%


#8 User is offline   Smiley357 

  • Junior
  • Pip
  • Group: Members
  • Posts: 85
  • Joined: 12-October 07

Posted 16 April 2008 - 11:58 AM

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?

#9 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 16 April 2008 - 01:48 PM

View PostSmiley357, on Apr 16 2008, 06:58 PM, said:

Sorry Yzowl your code didn’t work. Nothing was returned.
Of course my code works!

It has worked flawlessly on hundreds of systems for many years now.

#10 User is offline   Smiley357 

  • Junior
  • Pip
  • Group: Members
  • Posts: 85
  • Joined: 12-October 07

Posted 16 April 2008 - 03:08 PM

Yzowl:
I just copied and pasted your code into a note pad added a pause at the end and saved it as a bat file. Ran it and the only output I got was “Press any key to continue . . .”

#11 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 16 April 2008 - 03:35 PM

Try this attached version as copied/pasted by me with your included Pause!

Attached File(s)



#12 User is offline   Scr1ptW1zard 

  • Junior
  • Pip
  • Group: Members
  • Posts: 56
  • Joined: 05-July 07

Posted 16 April 2008 - 08:20 PM

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

#13 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 16 April 2008 - 10:24 PM

View PostSmiley357, on Apr 16 2008, 06:58 PM, said:

Nothing was returned.

View PostScr1ptW1zard, on Apr 17 2008, 03:20 AM, said:

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!

View PostYzöwl, on Apr 11 2008, 05:21 PM, said:

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.

#14 User is offline   Smiley357 

  • Junior
  • Pip
  • Group: Members
  • Posts: 85
  • Joined: 12-October 07

Posted 17 April 2008 - 07:56 AM

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

This post has been edited by Smiley357: 17 April 2008 - 07:58 AM


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy