I want to first off say, I don't know Visual Basic. I have a XML script that for a program that can only accept VB Scripts. I want a simple script to check whether, on it's own or if it can, from the XML file to see if a USB drive is attached to the computer, if not, the XML file won't run until the USB drive is attached to the computer and only only until the USB drive is attached ? Right now the XML file is set as task schedule to run at a specific day and time, I don't know if the task will execute if the drive is not connected, as it's remained connected for a while.

Recommended Answers

All 21 Replies

I want to first off say, I don't know Visual Basic

What you want to achieve by checking if a USB drive is connected or not, is quite advanced in vb6. You'll be making use of tons of windows API's to check a pluged in device, what kind of plug in it was (camera, flash etc.) and then to ensure that it is safely unplugged.

Do you have any kind of code as yet? If so, show us what you have. If not, have a look at THIS link with some attached code samples.

As I have said, it is quite advanced, so I'm sure you will some more questions following. :) We can however not give you a full on working sample, it will be against our posting rules.

All I have presently is an XML file. I don't code in VB, I don't think those code examples will be helpful, although from my understanding, they seem to solve or partially solve the problem I'm having, or am I mistaken ?

First off VB script is only a subset of VB, which may make your job even harder. Second of all in regards to not knowing VB or VB script, no time like the present :)

There is no time like the present, I didn't know it would be a complicated task, is there any way ? All I want is a warning box to tell me the drive is not connected before executing the XML script.

All I want is a warning box to tell me the drive is not connected

That is your problem right there. Before you can get to the message, you need the code to check IF it is plugged in, in order to genertae the message. The link I gave you deals with vb6 code to check if the device is plugged in or not. There are a few samples that will solve your problem.

It's even more complicated than that. I just boned up on vbscript and it doesn't appear to support writing to or from files which makes sense since it was designed to interact with web pages. Porbably the only thing you can do is use an error trap that can keep running the xml file until it doesn't produce an error. This link can give you more information on porogramming the error trap.

commented: Nice alternate solution. +12

I have a VBScript generated from the Software. I want to somehow, add the volume info from the threads I read that were posted, into the script. The script runs and if the volume (drive) is not connected it prompts to connect the drive, so the script can run. Until the drive is connected the script is at a stand still, or delays for 8/12/24 hrs.

To be honest, I don't think I understand what you asking. Here is my interpretation.

  1. You have some program that creates a VBScript file.
  2. This script is to run only if a certain Drive Volume is available.
  3. If it is not available, you want to prompt the user to mount the drive.
  4. Then your script file is to run.

If this is the case then maybe a VBScript like this will work:

' Ref: http://ss64.com/vb/filesystemobject.html
' Get File System Object
dim fso
set fso = CreateObject("Scripting.FileSystemObject")

dim DriveLetter
' Replace the inputbox with your Volume Letter
DriveLetter = inputbox("What drive to check?", "Drive Letter?", "E") 'Default Drive E

On Error Resume Next
dim USBDrive
USBDrive = fso.Drives(CStr(DriveLetter))

while Err.Number <> 0 
    WScript.Echo("Drive " & DriveLetter & ": DNE")
    if Err.Number <> 0 then wscript.sleep(5000) ' sleep 5 seconds, increase to meet your needs
    Err.Clear
    USBDrive = fso.Drives(CStr(DriveLetter))
wend

WScript.Echo("Drive " & DriveLetter & ": is available")

' uncomment and modify this line
'call shell("YourScript.vbs")

wscript.quit()


sub shell(cmd)        
   ' Run a command as if you were running from the command line
    dim objShell
    Set objShell = WScript.CreateObject( "WScript.Shell" )
    objShell.Run(cmd)
    Set objShell = Nothing
end sub
OPTION EXPLICIT

' call the main function
Call VBMain()


'******************************************************************************
'* Sub:     VBMain
'*
'* Purpose: This is main function to start execution 
'*
'* Input:   None
'*
'* Output:  None
'*
'******************************************************************************
Sub VBMain()
    Dim objShell 
    Dim ExitCode

    Set objShell = WScript.CreateObject("WScript.Shell")

' Do the backup
    ExitCode = Backup ("""C:\<filepath> +parameters""")

' done
    Set objShell = nothing
    wscript.quit(ExitCode)
End Sub


'******************************************************************************
'* Function: Backup
'*
'* Purpose:  Calls Reflect.exe passing an XML BDF as a parameter
'*           Optionaly logs output to file
'*
'* Input:    strCmdLine Command Line Arguments
'* Output:   Exit Code
'*
'******************************************************************************
Function Backup(Byref strCmdLine)
    Dim objShell
    Dim objExecObject 
    Dim strLine
    Dim objFS 
    Dim objNewFile 
    Dim strLogFileName

    strCmdLine = Replace(strCmdLine, "<BACKUP_TYPE>", GetBackupTypeParameter)

' Run the backup or image
    Set objShell = WScript.CreateObject("WScript.Shell")
    Set objExecObject = objShell.Exec(strCmdLine)

' Log to file
    strLogFileName = "O:\InternetPC\log-" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Hour(Time) & "." & Minute(Time) & "." & Second(Time) & ".txt"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objNewFile = objFS.OpenTextFile(strLogFileName , 8, 1)
    Do Until objExecObject.StdOut.AtEndOfStream
        strLine = objExecObject.StdOut.ReadLine()
        If Instr(strLine , "%") = 0 Then
            objNewFile.WriteLine strLine
        end if
    Loop

    objNewFile.Close
    set objFS = nothing

' Wait for the process to finish
    Do While objExecObject.Status = 0
        WScript.Sleep 100
    Loop

    if objExecObject.exitcode = 2 then
' Handle XML validation error 

    elseif objExecObject.exitcode = 1 then
' Handle backup error 
    elseif objExecObject.exitcode = 0 then
' Everything OK
        objShell.LogEvent 0, "Macrium Reflect - Successful Backup"
    end if
    Backup = objExecObject.exitcode
    Set objExecObject = nothing
    Set objShell = nothing
End Function


'******************************************************************************
'* Function: GetBackupTypeParameter
'*
'* Purpose:  determines the backup type from command line parameter
'*           -full Full backup
'*           -inc  Incremental backup
'*           -diff Differential backup
'*
'* Input:    None
'* Output:   the parameter
'*
'******************************************************************************
Function GetBackupTypeParameter
    Dim i

    for i = 0 to Wscript.Arguments.Count - 1
        If Wscript.Arguments(i) = "-full" OR _
           Wscript.Arguments(i) = "-inc" OR _
           Wscript.Arguments(i) = "-diff" Then
            GetBackupTypeParameter = Wscript.Arguments(i)
            Exit Function
        End If
    Next

    GetBackupTypeParameter  = ""

End Function

How do I the code posted, that works, lets just go with that and take the VB script I have and blend the two, together ?

There is an error in the code I posted last night.

call shell("YourScript.vbs")

should be

call shell("""YourScript.vbs""")

Note the 3 quotation marks on each side of the filename.

This code will then launch your script file.

Correct me if I'm not understanding. I let the program run the script you created, and in that case will run the script that was created from the program, seamlessly ?

Yes that is the intent. You run my script file that will in turn run your file.

The statement call shell("""YourScript.vbs""") will run the script file YourScript.vbs once the specified volume becomes available. I just noticed that I forgot to set fso to nothing, so here is the revised coding with both corrections.

' Ref: http://ss64.com/vb/filesystemobject.html
' Get File System Object
dim fso
set fso = CreateObject("Scripting.FileSystemObject")

dim DriveLetter
' Replace the inputbox with your Volume Letter
DriveLetter = inputbox("What drive to check?", "Drive Letter?", "E") 'Default Drive E

On Error Resume Next
dim USBDrive
USBDrive = fso.Drives(CStr(DriveLetter))

while Err.Number <> 0 
    WScript.Echo("Drive " & DriveLetter & ": DNE")
    if Err.Number <> 0 then wscript.sleep(5000) ' sleep 5 seconds, increase to meet your needs
    Err.Clear
    USBDrive = fso.Drives(CStr(DriveLetter))
wend

fso = Nothing
WScript.Echo("Drive " & DriveLetter & ": is available")

' uncomment and modify this line
'call shell("""YourScript.vbs""")

wscript.quit()

sub shell(cmd)        
   ' Run a command as if you were running from the command line
    dim objShell
    Set objShell = WScript.CreateObject( "WScript.Shell" )
    objShell.Run(cmd)
    Set objShell = Nothing
end sub

I unplugged the drive, and the program is set to do a schedule shortly (today), I hope everything exectues.

One concern, I don't know if the program will read the original programmed script within your script, I had to add the original programmed script as a second script in the program as I can add a few VBS. I'll see what problems arise.

The program is firing back a script error. Let me find out anything specific to the program first, although I was told, a VBS will work, so maybe something is not joining up, so to speak :)

@TinTinMn. The program cannot run a script that contains a script within a script, as the script you posted. I have to run each script seperately. I don't know if any changes have to be done the script ? I suppose check to see if the drive is connected, then if proceed with the next script, otherwise do no proceed with the next script, which is a schedule script until the first script can find the drive as in the "find drive script" you posted.

Set objShell = WScript.CreateObject("WScript.Shell")

objShell.Run """C:\my first script.vbs""", 1, true
objShell.Run """C:\my second script.vbs""", 1, true

Have you tried to merge the logic into your original script file?

Sorry, I don't understand ?

This is what I meant.

'******************************************************************************
'* Sub:     VBMain
'*
'* Purpose: This is main function to start execution 
'*
'* Input:   None
'*
'* Output:  None
'*
'******************************************************************************
Sub VBMain()
    ' **********************
    ' Added this
    '
        ' Ref: http://ss64.com/vb/filesystemobject.html
        ' Get File System Object
        dim fso
        set fso = CreateObject("Scripting.FileSystemObject")

        dim DriveLetter
        DriveLetter = "E"  ' ****** Replace this with the drive letter that would be mounted
        Dim SleepTime 
        SleepTime = 5 ' in Seconds,increase to meet your needs

        On Error Resume Next
        dim USBDrive
        USBDrive = fso.Drives(CStr(DriveLetter))

        while Err.Number <> 0 
            WScript.Echo("Drive " & DriveLetter & ": is not available, Please mount the drive")
            if Err.Number <> 0 then wscript.sleep(SleepTime * 1000) 
            Err.Clear
            USBDrive = fso.Drives(CStr(DriveLetter))
        wend

        set fso = Nothing
        DriveLetter = Nothing

    ' End of the splice
    ' ************************************
    ' Your Original code
    Dim objShell 
    Dim ExitCode

    Set objShell = WScript.CreateObject("WScript.Shell")

' Do the backup
    ExitCode = Backup ("""C:\<filepath> +parameters""")

' done
    Set objShell = nothing
    wscript.quit(ExitCode)
End Sub

I don't understand this program that the VB script is to run within. When I run the script within the program, the program fires back that it was successful, but there was no prompt mentioning there is no USB drive connected etc before running. I've been trying to get as much information as possible. A VBScript, is a VBScript it should work, unless the code has to be wrapped within the VBScript created by the program.

Does the Drive Letter automatically detect a USB Drive within the script ?

To run VbScript within the program I have to have (WScript.exe) associated with the script as an extension. (Click Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.