Link to home
Start Free TrialLog in
Avatar of camtz
camtzFlag for United States of America

asked on

Follow up question to ID 40605568

I should have asked this before closing above question.
I am constantly connecting and disconnecting various external drives to my computer (one at a time) - which is always "Drive F".  I find it very annoying to have to stop what I am doing and look for the little icon in the system tray that allows to "Safely remove Hardware and remove Media".

Could someone help me create a script that would do that using AutoHot keys? Thanks
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Hi Carlos,

I can't find question ID 40605568 — please post a URL to it.

Re this question, I suggest using the RemoveDrive command line utility:
http://www.uwe-sieber.de/drivetools_e.html

When you download the zip file, you'll see that there are 32-bit and 64-bit versions. Extract the right one for your version of Windows and put it in whatever folder you want, such as C:\MyProgs, in which case the command to Safely Remove drive F would be:

C:\MyProgs\removedrive.exe F: -L

The -L parameter means that it will try to to remove the drive in a loop until it is successful (shows "Success" in green) or until it is cancelled/failed (shows "Failed" in red).

Decide on a key or key combo to invoke this. Let's say you decide on Alt-Ctrl-R (for Remove). Then put this in your AutoHotkey macros script:

!^r::
Run,"C:\MyProgs\removedrive.exe" "F: -L"
Return

That should do it. Regards, Joe
Hi Qlemo,
Thanks for that comment, but note that even having the Question ID is not helpful in locating a question, as EE's Search and Advanced Search functions cannot search by Question ID. This has been reported in the New Launch project:

https://www.experts-exchange.com/PRI_3525.html
https://www.experts-exchange.com/PRI_6885.html

That's why I asked Carlos for the URL. Regards, Joe
Qlemo,
That's an excellent idea! I didn't know that you can ignore the Topic path. In other words, this:

https://www.experts-exchange.com/questions/28615686/Follow-up-question-to-ID-40605568.html

actually takes you to this:

https://www.experts-exchange.com/questions/28615686/Follow-up-question-to-ID-40605568.html

Very nice! Thanks, Joe
What's the link to your article?
Avatar of camtz

ASKER

Olemo, thank you for that.  I'll know in the future.  Joe, sorry to give you the bad news but this is not working for me.  Look at the pics and see if I've done anything wrong. User generated imageUser generated imageUser generated imageUser generated imageUser generated imageUser generated image
The only time I saw the window was when I used your suggestion.  In all other cases, the black window would show up momentarily and disapier.
I think the problem is that your AHK script has this:

Run,"C:\My Documents\My Programs\RemoveDrive" "F: -L"

Based on the screenshot you posted, it should be this:

Run,"C:\Users\Home\Documents\My Programs\RemoveDrive.exe" "F: -L"

Copy/paste the line above into your script (instead of the Run statement you have) and I think it will work. If that doesn't work, made a root folder on your C drive called MyProgs and put RemoveDrive.exe in there. Then copy/paste this line into your script:

Run,"C:\MyProgs\RemoveDrive.exe" "F: -L"

Btw, I never use the Documents, My Documents, My Music, My Pictures, My This and My That thingies in Windows, so I'm not an expert on how that works. I simply store all of my files on my J: drive in folder names of my choosing. But it seems that the problem with your script is that AutoHotkey can't find the RemoveDrive.exe file in order to run it, and I suspect it has something to do with the path to it. I'm sure that putting your AHK scripts in a folder of C: (or any other drive) will work fine. All of my AHK scripts (including my hotkeys script, which runs from the Startup group) are stored in a folder called J:\bats (where all of my batch files, AHK scripts, REXX scripts, and other scripts are stored). I'm sure you can achieve the same with Documents/My Documents, but I'm not very familiar with that. Regards, Joe
Avatar of camtz

ASKER

Dear Joe, I created a root folder in C:\ as you suggested but it didn't fix the problem.  Now I know what the black box was trying to tell me.  It comes and goes so fast that it took two of us to take a picture with my cell phone.  The problem seems to be that it considers Drive F  "an invalid" drive rather than it can't find it. Please look at the pic which shows the description of this Drive under "My Computer"  ( Pic 2 above) and tell me if we should modify the description of Drive F with more specificity. Thanks
Avatar of camtz

ASKER

User generated imageSorry, forgot picture.
Let's forget AutoHotkey for now — we'll worry about that later. Just open up a command prompt and issue this command:

dir f:

That will tell us if drive F exists. If it doesn't, that's the problem. If it does, then navigate to the folder where  RemoveDrive is and type in the command:

removedrive.exe F: -L

Note that there's a space between the colon and the hyphen. What happens when you issue this command?
Carlos,
I haven't heard back from you on my last post, but if you can't get RemoveDrive.exe to work, here's a different idea that uses just an AutoHotkey script — no need for another utility. First, get the script from here:

http://ahkscript.org/boards/viewtopic.php?f=6&t=4491

In the code block at the top of that post, click Select All and then Ctrl-C (Copy). Then paste it (Ctrl-V) at the bottom (after the last Return) of your AHK script that has your hotkeys defined. Then add this new hotkey in your hotkeys script:

^]::
Eject("F")
If (ErrorLevel=0)
  Return
Msgbox,4112,Error,ErrorLevel=%ErrorLevel% trying to eject F drive
Return

Open in new window


I just tested it here in W7/64-bit with the latest AHK release — it works perfectly! It displays the error MsgBox only in the case where the Eject failed (for example, if the F drive doesn't exist, in which case you'll see an ErrorLevel of -1). Regards, Joe
Avatar of camtz

ASKER

Sorry, I was tied up on something else.  I went to command line and sending pic of results.  Note that it states the name is EXTRA.  Copied and pasted your script and sending a pic of that as well. I don't know where the folder for this is.  I just go to the bottom of the task bar on the right and pull up a window that contains icons of things that are running , select the one that says Safely Remove ... and click on it which releases the drive. User generated imageUser generated image
Carlos,
The "Call to nonexistent function" error means that you didn't copy/paste the Eject source code into the bottom of that file. What you were supposed to do is in my last post, but I'll repeat here what you need to do:

(1) Go to:
http://ahkscript.org/boards/viewtopic.php?f=6&t=4491

(2) Click Select All in the code block at the top (pointed to by red arrow in this screenshot):
User generated image
(3) Hit Ctrl-C (that copies the code to the clipboard).

(4) Open your AutoHotkey.ahk file — the one you posted above showing lines 53-86. Go to the bottom of that file, which should have Return as the last line. Add a new line and then hit Ctrl-V. This will paste the Eject source code into the file. Save the modified file.

(5) Reload the modified script — right-click on its icon in the system tray and select Reload This Script.

I guarantee that if you follow these steps, it works. Regards, Joe
Avatar of camtz

ASKER

You are right, I did not follow your instructions correctly.  I just put the script that you said I needed AFTER installing the long script from the site.  Well, this time I did that.  Selected all, pasted it at the bottom of my list after return and pasted your script again.  I don't know if I have the latest update for AHK (that could be the problem) but this is what I got now - see picUser generated image
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of camtz

ASKER

WOW !!! Found out what is going on.  I was looking at my computer where all the drives are displayed and when I invoked the command, the F drive DISAPPEARED from the screen.  I then went down to see if it was on the icon on the system tray and GONE.  I didn't get the green or red light etc. but I think we are finally there.  I am sending you a pic regarding the version I am running.  I want to express my appreciation for alll the hard work you put into this and also about telling me about the root folder you set up in C:\ drive.  I tried it and think it's a great idea.User generated image
> when I invoked the command, the F drive DISAPPEARED from the screen

Great! That means it is working!

> I then went down to see if it was on the icon on the system tray and GONE.

Great again! Exactly what is supposed to happen.

> I didn't get the green or red light

That's a feature of RemoveDrive, not of Eject. Since you switched from using RemoveDrive to using Eject, you won't be getting the green/red indicator. The way it is coded now, you won't get anything if it succeeds; you'll get the Error dialog box that you posted if it fails. But if you'd like to get a message when it is successful, change your hotkey definition to this:

^]::
Eject("F")
If (ErrorLevel=0)
  MsgBox,4160,Success,Successfully ejected F drive
Else
  MsgBox,4112,Error,ErrorLevel=%ErrorLevel% trying to eject F drive
Return

Open in new window

> I think we are finally there.

Yes, I agree!

> I am sending you a pic regarding the version I am running.

It's good to see that you are on W7 Pro 64-bit. That's also what I use on my primary machine.

> I want to express my appreciation for all the hard work you put into this

You're very welcome — happy to help!

> I tried it and think it's a great idea.

I'm glad you like it.

Regards, Joe
Avatar of camtz

ASKER

Excellent help.
Thanks for the kind words — I appreciate it!