Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

batch to detect if there is pendrive?

Featured Replies

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:


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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.