Jump to content

Safely Remove USB Thumb Drive


Recommended Posts

I'll be traveling soon so I have started putting together a USB drive to take with me. Had some trouble finding a batch script or other method I liked in order to safely remove the USB device by shutting down all of the open programs or files and then ejecting/removing the device. So here is what I ended up with:

@ECHO OFF
Set USBDrive=%~d0
copy /Y *.* "%TEMP%"
c:

"%TEMP%\unlocker.exe" %USBDRIVE%
REM "%TEMP%\unlocker.exe" %USBDRIVE% /s
REM 2nd option (/s) hides GUI


"%TEMP%\removedrive.exe" %USBDRIVE% -l -b

The first thing we have to do is copy all of the files being used over to the temp directory or else we won't be able to close out the device. We change drives to C:\ so that the command window itself doesn't lock the device.

After that we call unlocker.exe which, in my case, is a stripped down version of Unlocker 1.7.7. I had lots of problems getting a version of Unlocker that would work in a portable manner like this (it usually needs to be installed). The only files I use for this are unlocker.exe and UnlockerDriver4.sys. I choose to use unlocker because it gives me a complete picture of all the files that are in use on the USB device. Then it allows me to choose whether to just unlock a file or kill some processes. I like this better than other methods (such as forcing all programs to close) because it leaves me with a little flexibility depending on the situation.

The last step is to call removedrive.exe which actually removes/ejects/unmounts the device. I tried some other, similar programs but this one worked best for me. It complete unmounts so that you can pull the device right out of the port. Most of the other programs I tried would not completely unmount. The drive would be safe to remove, but Windows still thought it was unsafe. This method completely powers down the device. You can remove the -b flag to disable the "safe to remove" dialog.

Hope this helps someone!

Link to comment
Share on other sites

  • 2 weeks later...

I just tried your batch script and it does all you claim. It's going to be very useful to me on a "travel drive" as well.

Unlocker 1.8.7 also works well here (using UnlockerDriver5.sys in place of UnlockerDriver4.sys).

Thanks for making this available!

Link to comment
Share on other sites

  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...