determining if a flash drive exists from a batch file without error messages
Asked Answered
A

4

5

I have batch files with the construct:

if exist F:\ copy /y Application.exe F:\

at the end of a compile, to copy the executable to a USB key if it is plugged in. It has worked fine with USB keys but when I had a USB multi card reader plugged in (this looks like drives E:..H:, and if there is no SD card plugged in, when I execute the above batch line, I get a "Windows - no disk" snag message.

If there is a card plugged in, I don't get the message, (and the file is copied), if the card reader is not plugged in, I don't get the message and the file is not copied (obviously), but in neither of these cases does the batch file stop running. It's only if the card reader is plugged in but there is no card in the drive.

Can I check quietly for no "disk" in these USB drives from a batch file?

Austria answered 14/12, 2010 at 8:58 Comment(0)
C
7

Replace IF EXIST with DIR and use the && or || depending on what you want to happen.

For example,

  • To replace IF EXIST...

    DIR F:\ && copy /y Application.exe F:\
    
  • To replace IF NOT EXIST

    DIR F:\ || copy /y Application.exe F:\
    

If You want to suppress the STDOUT and STDERR of the DIR to mimic the IF EXIST exactly...

  • To replace IF EXIST...

    DIR F:\ 1>NUL 2>&1 && copy /y Application.exe F:\
    
  • To replace IF NOT EXIST

    DIR F:\ 1>NUL 2>&1 || copy /y Application.exe F:\
    
Caiaphas answered 3/10, 2011 at 15:51 Comment(1)
Thanks @David. Worked a treat. Using the construct if not exist F:\ echo hello displays a dialog --------------------------- Windows - No Disk --------------------------- Exception Processing Message c0000013 Parameters 75b6bf7c 4 75b6bf7c 75b6bf7c --------------------------- Cancel Try Again Continue --------------------------- if F:\ refers to a USB card reader with no card plugged in, whereas using DIR F:\ 1>NUL 2>&1 || echo hello just echos hello like it should.Austria
E
0

I think that there used to be that you could run command.com with a /F parameter to make it always automatically fail those error messages so that it didn't break on them. Not sure about it though and not at all sure if that still works on modern Windows.

Edan answered 14/12, 2010 at 9:11 Comment(0)
B
0

DIR is ok only when the drive is not empty.

I no files exist on the drive it will not work even if drive is properly plugged in.

Use CDinstead:

CD F:\ && copy /y Application.exe F:\
Brainwashing answered 19/4, 2013 at 12:42 Comment(0)
B
0

if exist will work if you add NUL after F:\. Like this:

if exist F:\NUL copy /y Application.exe F:\
Boisvert answered 7/6, 2014 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.