Solved

Batch file that ignores drive availability errors

Posted on 2012-03-11
15
865 Views
Last Modified: 2012-03-15
Hello,

I'm trying to write a quick batch file that scans all the drives on a system (D-Z) and when it finds the desired file it runs it.

So far, I'm able to do that just fine with the following simple script.

For %%I In (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 "%%I:\MyUniqueFile.exe" "%%I:\MyUniqueFile.exe"

The problem I have is that Windows complains if a drive is unavailable. For example a floppy or CD drive with no disk inserted.

Error
If I click continue, it works fine. So my question is, how can I add error handling so that it will not present the user with an error and simply continue to the next drive?

I'm hoping to make this non-OS dependant, simply work across most or all MS Operating Systems.

Thanks!
0
Comment
Question by:charvett
  • 6
  • 5
  • 4
15 Comments
 
LVL 4

Expert Comment

by:kdebugs
ID: 37707720
Hi there,

Try with the exist command (i.e. if Exist file bla bla bla). Maybe this way the OS doesn't get the error condition.

Alex
0
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37707721
Try this .vbs file

****Example.vbs ****

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
Set objDrives = objFileSystem.Drives

For Each objDrive in objDrives
      If objFileSystem.FileExists(objDrive.DriveLetter &":\MyUniqueFile.exe") Then
            objShell.Run objDrive.DriveLetter &":\MyUniqueFile.exe",1,False
      End If
Next

Set objShell = Nothing
Set objDrives = Nothing
Set objFileSystem = Nothing
0
 
LVL 1

Author Comment

by:charvett
ID: 37707865
kdebugs, thanks for the suggestion, but I'm not too sure how to implement it so that it doesn't scan the drive first. I think the error occurs as soon as it discovers the drive. Perhaps I'm wrong, if you have an example implementation, I'd be happy to test.

fundacionrts, I tried your vbs script and it appears to have the same error behavior.

Perhaps there's no way to prevent the error, but instead error handling would do the trick.

Is there a way to tell Windows to basically "Continue" on error, so that the user is non the wiser that an error ever occured?

Thanks for all the help guys!
0
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37707877
charvett, what kind of drive is getting you the error? I run the example i post befoer with my laptop without cd-rom inserted and no dialog message appears...

I modify the script with something that i missed before:

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
Set objDrives = objFileSystem.Drives

For Each objDrive in objDrives
      If objDrive.IsReady AND objFileSystem.FileExists(objDrive.DriveLetter &":\MyUniqueFile.exe") Then
            objShell.Run objDrive.DriveLetter &":\MyUniqueFile.exe",1,False
      End If
Next

Set objShell = Nothing
Set objDrives = Nothing
Set objFileSystem = Nothing

This new script works ok too on my laptop (Windows 7 Ultimate x64).

In which Windows version are you execute the script?
0
 
LVL 1

Author Comment

by:charvett
ID: 37707888
Hi fundacionrts,

I updated the vbs file but still get the drive error. I double checked by putting a CD in the optical drive and the error went away.

I am running Vista 32bit.

I have a feeling that any script that scans all the drives will throw this error. Not sure.

Perhaps there's a way to error handle so that it continues on.

Also, somewhat related, I was originally using a batch file because it's very easy to convert to an exe file. I'm not sure if using a vbs file will introduce a new issue to compile it to an executable.

Thanks!
0
 
LVL 4

Accepted Solution

by:
kdebugs earned 250 total points
ID: 37707899
Hi,

Here's a quick and dirty example:

if exist a:\boot.ini goto exists
goto doesnt

:exists
echo boot.ini is there
goto end

:doesnt
echo file's not there

:end

Open in new window


That would do it. Just tested it in winxp and it works without errors.

Alex
0
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37707907
charvet, i run your batch file (FOR %%I...) in my computer (*) and no dialog appears again... Do you run the script in another computer to see is dialog message appears?

(*) I modified to echo hiberfil.sys if exists on ROOT:\hiberfil.sys and only SYSTEM:\hiberfil.sys is showed without any dialog message "No disk in drive"
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:charvett
ID: 37707969
Very Interesting, I tried my original bat file on a Windows 7 box and no error.

Seems like it may be specific to Vista.

I'll need to research more. If anyone has any ideas I'd be most greatful.

This is part of a bigger effort that will need to run across many different MS OS's. Including Vista. I wonder if it would error on another Vista box? Or if it's something specific to mine?

Thanks!
0
 
LVL 1

Author Comment

by:charvett
ID: 37708040
Update:

I tried another Vista box and there is no error. So it is specific to my box.

So I guess the real question is - is there a way to tell a batch file to "Continue" when a Windows error occurs?

That way I can be confident that no one else will encounter an issue regardless of their specific setup.

Thanks!
0
 
LVL 4

Expert Comment

by:kdebugs
ID: 37708054
The following command gives a list of existing drives:

fsutil fsinfo drives

Maybe this is a start?

Alex
0
 
LVL 1

Author Comment

by:charvett
ID: 37708233
Interesting command, does give a nice list of available drives. Unfortunately, it lists the drive that gives an error as well.

Still searching to find a way to supress the "cmd - No Disk" error.

Perhaps using the errorlevel function and sending the error out in a different way?

I hate it when something really simple kicks my but!

Thanks again!
0
 
LVL 4

Expert Comment

by:kdebugs
ID: 37708247
I just noticed that the command lists the same drives as explorer. I tried modifying the batch drive to look at the dvd drive and the batch file works well.

There's a chance that floppy's are treated differently. I don't have a computer with a floppy drive available to me right now, otherwise I'd try it there to confirm that this is so.

Alex
0
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37708670
Maybe you have in this computer an antivirus software that scan drives when they are accessed, so drive is accessed by this software?
0
 
LVL 1

Author Comment

by:charvett
ID: 37712035
Yes, perhaps an antivirus product would casue this condition. Good call!

In my case, it seems related to a phone and a tablet I have attached via USB for charging, but the drive functions disabled on devices. So it shows up as a drive, but it is unavailable.

I've given up finding a way to supress the 'no disk' error. Just seems like it's part of the OS that can not be avoided.

I wonder if there's a way to work backwards from Z to D and stop the search once it locates the desired file?

More often than not, the USB drive I'm looking for will be in the upper drive letters. If it would stop it's search once found, it would reduce the likelyhood of the error occuring.

Thanks!
0
 
LVL 8

Assisted Solution

by:fundacionrts
fundacionrts earned 250 total points
ID: 37712095
This version exit FOR loop at first match:

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
Set objDrives = objFileSystem.Drives

For Each objDrive in objDrives
      If objDrive.IsReady AND objFileSystem.FileExists(objDrive.DriveLetter &":\MyUniqueFile.exe") Then
            objShell.Run objDrive.DriveLetter &":\MyUniqueFile.exe",1,False
                  Exit For
      End If
Next

Set objShell = Nothing
Set objDrives = Nothing
Set objFileSystem = Nothing
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now