Say USB path without letter (vbs)

℡╲_俬逩灬. 提交于 2020-02-23 07:09:10

问题


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)


回答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



回答2:


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



来源:https://stackoverflow.com/questions/38539863/say-usb-path-without-letter-vbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!