Check for presence of removable drive

I'd like to be informed (either immediately or with a delay of at max 10 minutes) if drive H: is not available/connected.

ChatGPT said I should schedule this .bat file in Task scheduler, but I have no clue if this is safe or has issues:


@echo off

set DriveLetter=H:
set FlagFile=%TEMP%\DriveNotConnected.flag

if not exist %DriveLetter%\ (
    if not exist %FlagFile% (
        msg * "Drive %DriveLetter% not connected!"
        echo. > %FlagFile%
    )
) else (
    del %FlagFile% 2>nul
)

Would someone care to tell me if the the above ChatGPT-code is bug/issue-free and could be used / are there any suggesitons for improvement, or other ways to achieve this?

(I tried asking for this on the Elevenforums but apparently no responses, and it's now buried under lots of new topics. https://www.elevenforum.com/t/small-app-script-that-will-warn-if-an-external-drive-is-not-connected.20206/ no responses as of Saturday Dec 2, 2023, and like none after that)

Thank you a lot!

What do you want to do in response to the drive being added / removed? That may impact on the best way to do it.

1 Like

The device is needed by various backup programs, but it's not always attached. I can't rely on it being attached so I'd like to be informed. Could also be a network drive that could lose connectivity due to WiFi issues, for example, I guess. Here it's a USB flash drive.

To be honest, there's an underlying issue with Parallels and possibly (!!) NTFS for Mac (Paragon) that I'm in the process of troubleshooting (NTFS-formatted USB drive won't attach the the Windows VM automatically / is unreliable), but then again there are also more reasons than that that the drive might not be attached to the Windows VM so it's always good to get a notification. The backup programs have their own (partly weird or not helpful) ways of informing about unavailable drives, so I just wanted a simple and reliable solution. Generally speaking it would not hurt if a certain backup task will be triggered once it's attached, but that's not a must.

The .bat file should do that, in that case. Or, at least, nothing about it looks unsafe.

1 Like

Hello,

it did not work, unfortunately.

I cannot code, but I used ChatGPT. It produced all kinds of stuff that did not work, but finally I got it to work.

C:\Admin\Backup\Drive_Check\nircmd.exe
C:\Admin\Backup\Drive_Check\Drive_Check.vbs
C:\Admin\Backup\Drive_Check\Drive_Check.bat

C:\Admin\Backup\Drive_Check\Drive_Check.vbs


Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c C:\Admin\Backup\Drive_Check\Drive_Check.bat", 0
Set objShell = Nothing

C:\Admin\Backup\Drive_Check\Drive_Check.bat


@echo off

set DriveLetter=H:
set FlagFile=C:\Admin\Backup\Drive_Check\DriveNotConnected.flag
set "nircmdPath=C:\Admin\Backup\Drive_Check\nircmd.exe"

if not exist %DriveLetter%\ (
    if not exist %FlagFile% (
        "%nircmdPath%" infobox "Please connect flash drive!" "Drive Checker" 0 0
        echo. > %FlagFile%
    )
) else (
    if exist %FlagFile% (
        del %FlagFile%
    )
)

I would appreciate any comments if there's anything that doesn't look good.

Looks alright. I guess the tricky part was to get the darn task scheduler to run every few minutes :wink: