Question

Looking to have a vbscript that can copy a folder and its contents to a active usb drive. So the script need to found the active usb drive put in it. Next copy the folder and its contests to the usb drive. Then after the copy job it done need to tell it's finish. There is what I get so far for it.

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Test" , "?:\Test" , OverWriteFiles 'the "?:\Test" is the part set the usb drive at.
Wscript.Echo "I am Done."

So, what part I really need help with is found out what the active usb drive letter. Next tell is to copy to that usb drive letter. I know it's not alot of code. But, any info wold be a great help.

Was it helpful?

Solution

This example shows the type of each drive:

Set oFSO = CreateObject("Scripting.FileSystemObject")
sRes = ""
For Each oDrive In oFSO.Drives
    sRes = sRes & "DriveLetter: " & oDrive.DriveLetter & ", DriveType: "
    Select Case oDrive.DriveType
        Case 0
            sRes = sRes & "Unknown"
        Case 1
            sRes = sRes & "Removable"
        Case 2
            sRes = sRes & "HDD"
        Case 3
            sRes = sRes & "Network Drive"
        Case 4
            sRes = sRes & "CD-ROM"
        Case 5
            sRes = sRes & "RAM-Drive"
    End Select
    sRes = sRes & vbNewLine
Next
MsgBox sRes

Your script should be like this one:

Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDrive In objFSO.Drives
    If objDrive.DriveType = 1 Then
        objFSO.CopyFolder "C:\Test" , objDrive.DriveLetter & ":\Test" , True
        MsgBox "Copy to " & objDrive.DriveLetter & " Completed"
    End If
Next

UPD: Last drive can be found this way:

Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDrive In objFSO.Drives
    Set objLastDrive = objDrive
Next
MsgBox objLastDrive.DriveLetter

OTHER TIPS

Well, there may be more than one "active" USB drive. There's nothing stopping me from plugging flash drives into every available USB port on my computer. But here's how you can identify a USB drive (aka, "Removable Drive") on your computer and copy a folder to it with the CopyFolder() function.

Const TYPE_REMOVABLE = 1

With CreateObject("Scripting.FileSystemObject")
    For Each Drive In .Drives
        If Drive.DriveType = TYPE_REMOVABLE Then
            .CopyFolder "C:\Test", Drive.DriveLetter & ":\Test", True
        End If
    Next
End With
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top