Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CMD script to check if a volume is plugged in or removed (for Windows)
:: ===================================================
:: This script checks if a volume is plugged in or
:: if it has been removed. Useful to detect if
:: someone removed your USB stick from your computer
:: during your absence.
:: ---------------------------------------------------
:: Note: all actions are logged in drive-listener.txt
:: (this file will be created in the same folder
:: where this file resides)
:: TODO: specify the volume below (set vol=***)
:: ===================================================
:: Setup stuff
@echo off
set logfile=drive-listener.txt
set removed=0
:: IMPORTANT: select the volume you want to check:
set vol=E:\
echo.>%logfile%
title Check drive [%vol%]
cls
:: Check drive
:check
cls
IF NOT EXIST %vol% goto :fail
echo Status: [%vol%] exists and is plugged in
ping localhost -n 2 > nul
goto :check
:: Drive has been removed
:fail
cls
title +++ %vol% HAS BEEN REMOVED +++
IF "%removed%"=="0" echo [%date% %time%] %vol% HAS BEEN REMOVED >> %logfile%
set removed=1
IF EXIST %vol% goto :pluggedin
echo %vol% IS REMOVED ...
ping localhost -n 2 > nul
goto :fail
:: Drive has been plugged in again
:pluggedin
title +++ %vol% HAS BEEN REMOVED AND PLUGGED IN AGAIN +++
echo [%date% %time%] %vol% HAS BEEN PLUGGED IN AGAIN >> %logfile%
set removed=0
goto :check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.