Safely remove USB from batch file?
15,729
Solution 1
Eject Media and Remove Drive
Freeware by Uwe Sieber - http://www.uwe-sieber.de
Remove drive is what you need for USB safe removal.
Solution 2
Download RemoveDrive.exe from http://www.uwe-sieber.de/drivetools_e.html
and then use the code below.
removedrive\x64\RemoveDrive.exe F: -L
Note that
- F: is the drive you want to eject
- -L means Loop
Solution 3
Please try this script to safely remove disk, maybe useful to you :)
@echo off
cls
set tempfile="%TEMP%\tmp_disk.dsk"
cd %SystemRoot%\system32
echo.
echo ...:: Safely Remove Disk ::...
echo.
echo Select the disk volume number (if the disk has multiple volumes, select any of them)
echo.
echo.
echo list volume | diskpart | findstr /C:Volume /C:---
echo.
set /p volume=" Selected volume: "
echo.
echo select volume %volume% >>%tempfile%
echo offline disk >>%tempfile%
echo online disk >>%tempfile%
diskpart /s %tempfile% | findstr /C:"not valid"
if "%ERRORLEVEL%"=="1" (
echo Disk has been unlocked successfully. Try to safely remove it now...
pause
)
del /F %tempfile%
Solution 4
While you can use powershell. Try the following:
public string GenerateScript(string driveLetter)
{
return "$vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq '" + driveLetter + :\\'};\n" +
"$vol.DriveLetter = $null\n" +
"$vol.Put()\n" +
"$vol.Dismount($false, $false)\n";
}
Author by
Naseer
Updated on June 16, 2022Comments
-
Naseer almost 2 years
Can a batch file be made in windows that could safely eject the USB?So far on various sites there exist different utilities like devcon.
-
jeb about 9 yearsNice idea, but it seems to fail. Tested with Win7x64. Got an error message
The process isn't supported for removable media
(translated) -
Eko Junaidi Salam about 9 yearsHmm... this's interesting... I've tested it on windows and any usbdrive like flashdisk, external hardrive, it work fine. Can you provide more information about your usb stick you've tested it ?
-
jeb about 9 yearsI tested with a
Cruzer Blade 32GB
from SanDisk -
Eko Junaidi Salam about 9 yearsI've tested it too but another SanDisk product. You can use this script if your usb get locked in process handler. :)