您可以自动检查是否有可移动磁盘吗?

时间:2019-12-25 13:57:55

标签: batch-file

我想批量编写一个程序,该程序可以识别您是否插入了USB记忆棒,然后自动复制了一个文件夹。复制很容易,但是我很难识别USB记忆棒。

我已经完成了这一步,但是如果USB记忆棒还有另一个字母并且字母“ E”上有硬盘驱动器,该怎么办。此外,它仅每2分钟检查一次。如果程序可以立即识别USB记忆棒,那就太好了。

我尝试过的代码:

@echo off
goto search
:search
IF EXIST E: GOTO E
timeout /T 120 /nobreak
goto search
:F
xcopy /s F:\test\*.* C:\Users\sebas\Desktop\copied\*.*
exit

3 个答案:

答案 0 :(得分:0)

USB驱动器是可移动磁盘,可以找到如下:

Prompt>wmic logicaldisk get DeviceID, Description
Description       DeviceID
Local Fixed Disk  C:
Removable Disk    D:
CD-ROM Disc       E:
Removable Disk    F:

答案 1 :(得分:0)

您可以使用以下方法准确获取可移动磁盘:

wmic logicaldisk get deviceid, description | find "Removable"

答案 2 :(得分:0)

使用提供的链接标记,这是将驱动器号存储到文本文件中,然后启动批处理程序的方式。

在批处理中包含一些代码以恢复驱动器号并将其应用于变量:

<StoreDriveLeterFilepath.txt (
Set /p Drive_Letter=
)

调整XCOPY行以使用变量。

xcopy /s %Drive_Letter%\test\*.* C:\Users\sebas\Desktop\copied\*.*

修改了vbs代码以将驱动器存储到.txt中并启动您的Batch程序。 在指示的位置插入批次的文件路径

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
               If objitem.DriveType = 2 then
                        Wscript.Echo objItem.DriveType & " " & objItem.Name & " " & objItem.driveletter

            Dim objFSO 'File System Object
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Dim objTS 'Text Stream Object
            Const ForWriting = 2
            Set objTS = objFSO.OpenTextFile("StoreDriveLeterFilepath.txt", ForWriting, True)
            objTS.Write ("objItem.driveletter")
            objTS.Close()
            Set bjFSO = Nothing 'Destroy the object.
            Set objTS = Nothing 'Destroy the object.
            set WshShell=createobject("wscript.shell") 
            WshShell.run "Your Batch Filepath Here.bat", 1, true


               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