INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

I am looking for a way to automatic

I am looking for a way to automatic

(OP)
I am looking for a way to automatically Eject a flash/usb drive after backup. The backup goes to drive C: in a hardcoded path,
then uses a for-each next to find the usb drive and copy to a hardcoded folder, then resaves to C: for subsequent saves.

Andy (one of the programmers on here) sent me a huge macro that I wouldn't even begin to understand all of it. Thank you for
that info Andy, but shouldn't there be something much simpler?

The usb drive is a local drive (usually F:) and not a network drive. I suck at networking so hopefully there is a local drive
equivalent to RemoveNetworkDrive() or some type of EJECT command with a path?

If not, should I change the USB drive to a shared resource so i can use the network drive command with the Bforce, Bupdateprofile, etc?

I just want to be able to pull the drive out of the port and not have to go thru the click and manually eject process. No big deal to
do it, but I want to be able to work with drives way more than i can now.

Code Follows:

Sub flashtest()

' Backup to Flash Drive, then unmount flash drive automatically

Dim Ans, fs, cvl, d, dc, drvltr, drvpath1, drvpath2, drvpath3, savedtoc

Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
savedtoc = 0

'fs=vb crap, cvl=current volume label, d&dc=vbcrap/for-each
'drvltr=text drive letter, drivepath*=paths for each bkp drive
'savedtoc is if you already saved to C: and another hard drive exists >C:,
'resave back to C: because of the order of the For-Each-Next (C,D,E,F,G,etc.)


Ans = MsgBox("Plug in Flash Drive & Click OK", vbOKOnly, "Flash Backup")

Application.DisplayAlerts = False 'DISABLES Alert Prompts (auto file overwrite)

For Each d In dc
cvl = Dir(d.driveletter & ":", vbVolume)

If d.drivetype = 1 And (cvl = "16" Or cvl = "32" Or cvl = "64") Then 'Removable - Flash Drive
If d.IsReady Then
drvpath2 = d.driveletter & ":\XL\flashtest.xls"
ActiveWorkbook.SaveAs (drvpath2)
Ans = MsgBox("saved to flash", vbOKOnly, drvpath2)
End If
End If

If d.drivetype = 2 And cvl = "7" Then '2-Hard Disk

drvpath1 = d.driveletter & ":\Users\A\My Documents\XL\flashtest.xls"
ActiveWorkbook.SaveAs (drvpath1)
Ans = MsgBox("saved to Drive C:", vbOKOnly, "Save C:\..")
savedtoc = 1
End If
Next

If savedtoc = 1 Then
ActiveWorkbook.SaveAs (drvpath1)
End If

If you have any suggestions or any help, I would appreciated it!

RE: I am looking for a way to automatic

I once used RemoveDrive.exe Command line utility in a program. Just call it from within VB. Works great.
https://betanews.com/2011/11/24/use-command-line-t...

Example:

Quote (vb)

Shell "C:\Users\Jack\Downloads\removedrive\Win32\RemoveDrive.exe F:"

or

Shell "C:\Users\Jack\Downloads\removedrive\Win32\RemoveDrive.exe MyUSBdrive"


RE: I am looking for a way to automatic

(OP)
Hopefully I can concatenate a character string to give the path that the Shell command requires. I read the shell command last night and there are some switches in there that may help.

By "friendly name" I assume they mean Volume Label??

RE: I am looking for a way to automatic

Yes example 2 "MyUSBDrive" in the example is the volume name / label. If the drive letter changes, the name will stay the same, so in your case maybe the best option. Thanks for the star.

RE: I am looking for a way to automatic

pullmyefinger: for future reference, it looks like you are using vbscript, not VBA... if so, better to post to this forum:
forum329: VBScript

RE: I am looking for a way to automatic

>it looks like you are using vbscript

Well, apart from

Application.DisplayAlerts = False

and

ActiveWorkbook.SaveAs

Which implies VBA in Excel. And here's a VBA solution (example, not production code):

CODE

' Assumes you have set references to Microsoft Scripting Runtime and Microsoft Shell Controls and Automation
Public Sub EjectByName(strVolumeName As String)
    Dim fso As New FileSystemObject
    Dim USBDrive As FolderItem2
    Dim myShell As New Shell
    Dim myDrive As Drive

    For Each myDrive In fso.Drives
        If myDrive.DriveType = 1 And myDrive.IsReady = True Then  'removeable and mounted
            If myDrive.VolumeName = strVolumeName Then
                Set USBDrive = myShell.Namespace(ssfDrives).Parsename(myDrive.Path)
                If USBDrive.Type = "USB Drive" Then ' Check it really is a USB drive
                    USBDrive.InvokeVerb "Eject"
                End If
            End If
        End If
    Next
End Sub 

RE: I am looking for a way to automatic

(OP)
' Assumes you have set references to Microsoft Scripting Runtime and Microsoft Shell Controls and Automation


Where do I find this information other than what I have already set?

I am using the visual basic editor is all I know. I have not done all that much with this.

RE: I am looking for a way to automatic

Menu at the top of the IDE editor

Tools>References... and then add the two references I mentioned.

RE: I am looking for a way to automatic

(OP)
I did all that, called it from my original coding and it looks like nothing happened.
System tray still says "Eject Ultra Backup".

Is there a way to tell in code if the drive is actually unmounted?
Where do I look for coding references? VBA or MS Scripting?
Never worked on anything in this area, so I am clueless.

RE: I am looking for a way to automatic

Presumably you've stepped through the code to verify that

USBDrive.InvokeVerb "Eject"

is actually running?

RE: I am looking for a way to automatic

(OP)
Yes, but I missed some things in my haste.
What I did was ro run another macro first from a command button.
This macro runs fine but does not eject the flash drive.
After I have all the backups saved, I

Call EjectByName(16) where "16" is the volume label of usb (F:) drive.

I put a msgbox in there before and the sub is called,
but it Skips all the Dim statements

If USBDrive.Type = "USB Drive" Then ' Check it really is a USB drive
USBDrive.InvokeVerb "Eject"
End If

Eventually it takes the IF statement and Never Does do the Eject like you said.

The more I try this, the more I realize I don't understand.

When I look in the tray to see what the results are, it says

Eject Ultra Backup
16 F:

That is why I asked for links to find out what I need to know. I don't know what needs changed, added, etc.

RE: I am looking for a way to automatic

Quote:

but it Skips all the Dim statements

True. Dim statements are not executable.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: I am looking for a way to automatic

(OP)
Strongm: take a couple statements out, substitute others and your sample works.. Thank You..

RE: I am looking for a way to automatic

Thanks for posting that VBA example code. That's something I've thought about toying with in the past, and now I've got an example I can use to push that further along.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

RE: I am looking for a way to automatic

(OP)
Good, glad someone else has a use for it. Use Strongm's version of the code, but take the IF-Endif out around "usbdrive.invokeverb "EJECT".

it will remove the flash drive faster than a child running from the dinner table after seeing veggies...

RE: I am looking for a way to automatic

> take the IF-Endif out around "usbdrive.invokeverb "EJECT".

Truish. It is a belts and braces check to ensure we don't dismount something we should not. Alternative to removing it completely, we could actually check what the return value of USBDrive.Type actually is for your drive, and modify the If clause appropriately. (my assumption would be that in pullmyefinger's case, the eject was not running because their flashdrive was not reporting itself to be a "USB Drive")

RE: I am looking for a way to automatic

(OP)
yes, that would make the IF false and the eject would never work.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close