说不带字母的USB路径(vbs) [英] Say USB path without letter (vbs)

查看:83
本文介绍了说不带字母的USB路径(vbs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个VBScript,我想在其中将文件(例如记事本)保存到USB记忆棒中. 为此,我正在使用AppActive和SendKeys"^ s",这将弹出一个窗口,询问路径.

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.

问题是我不知道我的USB在某些计算机上会收到什么字母.在我的电脑上,它是E,但在我朋友的PC上,它是G(无论如何,无关紧要).有没有办法在不包含字母的情况下说出路径?

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?

我将USB命名为"USB",只是试图写出不带字母的路径.它适用于我的计算机,但不适用于任何其他PC.有什么建议吗?

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:我正在Windows(如果需要操作系统)上工作

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

对于我的研究,我得到了这个链接,它最接近我的需求,但不是我想要的. 从USB端口获取USB设备路径

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

更新:如果您想在不知道驱动器号的情况下找到它,那么Noodles的代码就非常好

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

更新2 :我也发现了这一点 http ://www.howtogeek.com/96298/assign-a-static-drive-letter-to-a-usb-drive-in-windows-7/ 因此,我基本上可以为USB分配一个随机字母(例如Z),并将其简单地用作驱动器字母(希望它也可以在Windows 10上使用)

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)

推荐答案

此代码监视音量变化,如果是USB,则将文件复制到c:\ test.您感兴趣的是Win32_Volume代码.

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

这篇关于说不带字母的USB路径(vbs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆