0

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)

Community
  • 1
  • 1

2 Answers2

1

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
0

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".

Community
  • 1
  • 1
tambre
  • 4,625
  • 4
  • 42
  • 55
  • No, you didn't understand my question. I want to assign that path directly, not to open a window. – Craig Douglass Jul 23 '16 at 08:54
  • "I want to assign that path directly" What do you mean? I can't understand you. Like I said, drive letters for any storage devices may be different on different systems, so you'd need to know the drive letter. If you know the drive letter you could assign it to a variable and then concatenate drive letter to where you need to access the storage device. – tambre Jul 23 '16 at 08:56
  • yes, that would be easy. But what if I don't know the drive letter? Isn't there a way to solve this? – Craig Douglass Jul 23 '16 at 09:02
  • If you don't know the drive letter you ask the user for the drive letter. Or let him select the location to save to (this would make the most sense). You could even probably somehow enumerate the available USB devices and save to one of them, but that would be quite complicated and error-prone. – tambre Jul 23 '16 at 09:04