开发者

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

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 plugg开发者_如何学编程ed 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?


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:\
    


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.


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:\


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜