Jump to content

batch to detect if there is pendrive?


Recommended Posts

hi guys!

im needing a batch that can detect if there is a pendrive on the pc, and if it is connected, launch an application.

i will explain my idea:

after install my unattended w7, (and disable uac), i need to detect if there is a pendrive...

if a pendrive is connected to the pc, launch wpi.exe on the root of the pendrive.

if there isnt any removable store device on the pc, then execute wpi.exe on %cdrom%.

any help? :hello:

Link to comment
Share on other sites


Here's a quick VBScript that will parse all drives, and check all removable disks (USB keys, USB HDDs, etc) and CD/DVD drives for strFile (wpi.exe). If found, it'll execute the file via cmd /c, and loop the vbscript until the file finishes executing (then continue the vbscript).

'// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'//
'// NAME: findfileonexternaldisk.vbs
'//
'// Original: cluberti at cluberti dot com
'// Last Update: 14th May 2010
'//
'// Comment: VBS script for finding a file on an external drive.
'//
'// NOTE: Provided as-is - usage of this source assumes that you are at the
'// very least familiar with the vbscript language being used and
'// the tools used to create and debug this file.
'//
'// In other words, if you break it, you get to keep the pieces.
'//
'// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

RunMeWithCscript()

strComputer = "."
strFile = "wpi.exe"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colDiskItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_LogicalDisk",,48)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
FileFound = FALSE

On Error Resume Next
For Each objDiskItem in colDiskItems
'// Removable disk check:
If objDiskItem.DriveType = 2 Then
If Not objDiskItem.FileSystem = "" Then
If (objFSO.FileExists(objDiskItem.DeviceID & "\" & strfile)) Then
FileFound = TRUE
WScript.StdOut.Write vbCrLf & "wpi found - executing: " & objDiskItem.Caption & "\" & strFile
Set isRunning = WshShell.Exec("cmd /c " & objDiskItem.Caption & "\" & strFile)
Do While isRunning.Status = 0
WScript.Sleep 100
execOutput = isRunning.StdOut.ReadAll
Loop
Else
'// No file on this drive, do nothing and move on to the next removable disk
End If
End If
End If

'// CDROM check:
If objDiskItem.DriveType = 5 Then
If Not objDiskItem.FileSystem = "" Then
If (objFSO.FileExists(objDiskItem.DeviceID & "\" & strfile)) Then
FileFound = TRUE
WScript.StdOut.Write vbCrLf & "wpi found - executing: " & objDiskItem.Caption & "\" & strFile
Set isRunning = WshShell.Exec("cmd /c " & objDiskItem.Caption & "\" & strFile)
Do While isRunning.Status = 0
WScript.Sleep 100
execOutput = isRunning.StdOut.ReadAll
Loop
Else
'// No file on this drive, do nothing and move on to the next Compact Disc drive
End If
End If
End If
Next

If FileFound = FALSE Then
WScript.StdOut.Write vbCrLf & "wpi not found - exiting"
End If
On Error Goto 0

'//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'// Subroutine: RunMeWithCScript()
'//
'// Purpose: Forces the currently running script to use Cscript.exe as the Script
'// engine. If the script is already running with cscript.exe the sub exits
'// and continues the script.
'//
'// Sub Attempts to call the script with its original arguments. Arguments
'// that contain a space will be wrapped in double quotes when the script
'// calls itself again. To verify your command string you can echo out the
'// scriptCommand variable.
'//
'// Usage: Add a call to this sub (RunMeWithCscript) to the beggining of your script
'// to ensure that cscript.exe is used as the script engine.
'//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Sub RunMeWithCScript()

Dim scriptEngine, engineFolder, Args, arg, scriptName, argString, scriptCommand

scriptEngine = Ucase(Mid(Wscript.FullName,InstrRev(Wscript.FullName,"\")+1))
engineFolder = Left(Wscript.FullName,InstrRev(Wscript.FullName,"\"))
argString = ""

If scriptEngine = "WSCRIPT.EXE" Then
Dim Shell : Set Shell = CreateObject("Wscript.Shell")
Set Args = Wscript.Arguments

For each arg in Args 'loop though argument array as a collection to rebuild argument string
If instr(arg," ") > 0 Then arg = """" & arg & """" 'if the argument contains a space wrap it in double quotes
argString = argString & " " & Arg
Next

'Create a persistent command prompt for the cscript output window and call the script with its original arguments
scriptCommand = "cmd.exe /k " & engineFolder & "cscript.exe """ & Wscript.ScriptFullName & """" & argString

Shell.Run scriptCommand,,False
Wscript.Quit
Else
Exit Sub 'Already Running with Cscript Exit this Subroutine
End If
End Sub

Edited by cluberti
Updated - added CDROM check
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...