How would I run a batch file when I eject my external HDD?
7 replies, posted
I don't know how you guys feel about batch, but I am currently using it for it's simplicity. If needed I can reference some vbscript for this part.
Basically what I am doing is creating a little script which generates a shortcut on the desktop to my external HDD, which is hosting the script. I already have it set up to autorun the script, and it executes flawlessly. Using a small executable for shortcuts and a refresh desktop dll I found via google, (I am currently only pausing to test the script) I managed to generate the shortcut, delete it, then refresh the desktop to get rid of the left over shortcut that "doesn't exist".
[code]@echo off
cls
shortcut /f:"%USERPROFILE%\Desktop\EVERYTHING.lnk" /a:c /t:"%~d0"
pause
del "%userprofile%\desktop\everything.lnk"
pause
rundll32 rfshdktp,refreshDesktop
pause[/code]
%~d0 is a shortcut to the drive letter that the script is running from, works like a charm.
Now I wish to move away from my "test" and into something that could be very useful. To do this, I want to take the last two commands and move them to a seperate batch file which I move over to the primary drive, and have it execute when the external HDD is disconnected. I can easily copy the new script over to something like C:\scriptdata. All I need is something to detect the removal of my external HDD.
On to my three main questions:
1) Is it possible to run xcopy or robocopy where the destination folder is hidden?
2) How would I implement a system to run a script after the drive has been ejected?
3) How would I delete the local script if it's already running (in order to have no trace left on the system after the drive is removed)?
Why am I doing this? Because first, I always go Start Orb -> Computer -> *Drive letter*:\ to open my external HDD. Why not have a no-hassle shortcut that pops up when the drive is there, and disappears when it's gone. I think OSX has a similar feature...
Thanks in advance
PS - in case you wanted the download links to shortcut.exe and rfshdsktp, here they are:
shortcut.exe - [url]http://www.optimumx.com/download/Shortcut.zip[/url]
rfrshdsktp - [url]http://nsis.sourceforge.net/mediawiki/images/2/2b/Rfshdktp.zip[/url]
Copy a .bat laying next to your script to c:\blabla
That .bat has something like:
del shortcut
del itself
Idk how to find out when it ejects, but when it does, call the .bat above
[url]http://en.wikipedia.org/wiki/List_of_DOS_commands[/url]
edit:
Forgot to mention, call it early from the bat, so that it runs when HDD is ejected. Have HDD eject-detect in the copied .bat
hm, perhaps if I could pass on a variable %~d0 to keep a constant drive letter, then have a while loop to see if that drive letter exists. Then if it doesn't exist, delete the shortcut and itself and exit.
Now my only question is how much processor usage will this take up? Maybe I could have the program wait (with the ping trick) for the removal of the drive. I will see what I can do with hstart (to remove the command line windows)
Won't use much processor. Maybe 1% each time they call.
Will definitely not be noticeable.
I just finished the code and polished it with Hstart. Only issue is that my script will use 30-40% of my processor while waiting for the drive to disconnect. I am currently using a loop while checking to see if F: (my external HDD drive letter, although I pass the drive letter as a variable because my remove script runs from C:\everything_auto. (Everything is my external HDD's volume name)) exists. It will give me an occasional lagspike, how would I optimize my code to idle for a second between checks, to reduce the frequency of the check, lowering CPU utilization.
Or maybe I could use hstart's priority switch...
EDIT: priority set to /idle doesn't seem to be doing much, just lowers the RAM usage a bit, CPU usage is still up at 30-40%
EDIT2: the ping waiting trick works, ping.exe pops up every 500 milliseconds, using 1-2% then disappearing, cmd.exe is down to 0%, except for when I disconnect the drive and I actually have it do something... Most of the lag now is because firefox (not 3.5) has a memory leak and now takes nearly 500MB of RAM with 3 tabs open, facebook, facepunch, and another help forum I found via google.
[QUOTE=robmaister12;16052465]
EDIT2: the ping waiting trick works, ping.exe pops up every 500 milliseconds, using 1-2% then disappearing, cmd.exe is down to 0%, except for when I disconnect the drive and I actually have it do something... Most of the lag now is because firefox (not 3.5) has a memory leak and now takes nearly 500MB of RAM with 3 tabs open, facebook, facepunch, and another help forum I found via google.[/QUOTE]
Then use 3.5, 149mb with 10 tabs open.
[QUOTE=danharibo;16053858]Then use 3.5, 149mb with 10 tabs open.[/QUOTE]
that's exactly what I did.
It's all done now. If you want the script, drop this into the root folder of a drive that lets you autorun. It will work on any computer you plug the drive into and leaves nothing behind when ejected. You may have to modify the bat files to reflect the volume name of your drive, I named mine EVERYTHING, so the script was written with that in mind.
[url]http://www.sagevgdc.com/projects/scripts/autorun.zip[/url]
Sorry, you need to Log In to post a reply to this thread.