Craig Douglass
Craig Douglass

Reputation: 74

Say USB path without letter (vbs)

I am writing a piece of VBScript in which I want to Save a file (say, notepad) into my USB Stick. To do so, I am using AppActive and SendKeys "^s" which will pop up a window asking for the path.

The problem is I don't know what letter will my USB have on certain computers. On mine, it's E, but on my friend's PC it is G (anyway, irrelevant). Is there a way to say the path without including the letter?

I named my usb "USB" and simply tried to write the path without the letter. It works for my computer, but it doesn't work on any other PCs. Any suggestions?

PS: I'm working on Windows (if the OS is needed)

As for my research, I got this link, which is closest to my need, but not what I want. Getting USB Device path from USB port

UPDATE: Noodles' code was really good if you want to find the drive letter when you don't know it

UPDATE 2: I also found this http://www.howtogeek.com/96298/assign-a-static-drive-letter-to-a-usb-drive-in-windows-7/ So I can basically assign a random letter for my USB (say, Z) and simply use this as drive letter (hope it also works on windows 10)

Upvotes: 0

Views: 350

Answers (2)

user6017774
user6017774

Reputation:

This code monitors volume changes and if it's a USB then copies the files to c:\test. Your interest is the Win32_Volume code.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set evtDevice = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_VolumeChangeEvent")

Wscript.Echo "Waiting for events ..."
Do
    Set objReceivedEvent = evtDevice.NextEvent
    'report an event
    Wscript.Echo " Win32_Device Changed event occurred" & VBNewLine
    If objReceivedEvent.EventType = 1 Then 
         Wscript.Echo "Type = Config Changed" 
    ElseIf objReceivedEvent.EventType = 2 Then 
         Wscript.Echo "Type = Device Arrived" 

         Set colItems = objWMIService.ExecQuery("Select * From Win32_Volume")
         For Each objItem in colItems
               Wscript.Echo objitem.DriveType
               If objitem.DriveType = 2 then
                        Wscript.Echo objItem.DriveType & " " & objItem.Name & " " & objItem.driveletter

                        Wscript.Echo "Starting Copying"
                        Set objShell = CreateObject("Shell.Application")
                        Set Ag=Wscript.Arguments
                        set WshShell = WScript.CreateObject("WScript.Shell")

                        Set SrcFldr=objShell.NameSpace(objitem.driveletter)
                        Set DestFldr=objShell.NameSpace("c:\test\")
                        Set FldrItems=SrcFldr.Items
                        DestFldr.CopyHere FldrItems, &H214
                        Wscript.Echo "Finished Copying"


               End If
        Next


    ElseIf objReceivedEvent.EventType = 3 Then 
         Wscript.Echo "Type = Device Left" 
    ElseIf objReceivedEvent.EventType = 4 Then 
         Wscript.Echo "Type = Computer Docked" 
    End If
Loop

Upvotes: 1

tambre
tambre

Reputation: 4853

You can't write to any storage device without knowing its assigned drive letter. You likely would want to instead open a file dialog allowing the user to choose the appropriate USB driver or other storage drive and then use the path selected.

See "How to open a file dialog in VBS".

Upvotes: 0

Related Questions