Solved

How can I use VBScript to find what drive letter contains a specific file in its root directory?

Posted on 2014-10-22
2
522 Views
Last Modified: 2014-10-29
Greetings -

I require a VBScript to iterate through the drives installed in my system that are capable of read and write (essentially, what WMI would classify as a LogicalDisk type 3), searching for a file in the root directory of each.  Once the first instance of the file is found, the drive letter is returned in a variable.

So... let's say I have the following drives:

C: (HDD)
D: (HDD)
E: (USB Stick)
F: (CD-ROM)
G: (HDD)
H: (CD-ROM)

Let's then say I'm looking for a "Tag" file such as one named "Drive.tag".

Let's say the Drive.tag file exists as G:\Drive.tag.

I want the script to iterate through the drives, find the Drive.tag file in the root of G:\ and return "G" to me.  Drives not available for both Read AND Write (in this case F, H, and possibly E) should not be touched.

I'm not sure how to implement this in VBScript, it's a new language to me.

A batch file to do this operation would look like this:
for /F "skip=2 tokens=2 delims=," %%p in ('wmic logicaldisk where drivetype^=3 get deviceid^,volumename /format:csv') do (
if EXIST "%%p\Drive.tag" (
set BackupDriveLetter=%%p
@echo FindMedia: Found backup drive at Volume %%p:.
goto DRIVEFOUND
)
)

Open in new window


It's that behavior I'm trying to reproduce in VBScript.

Thanks in advance!
0
Comment
Question by:amendala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40398465
This should get you started.  Not sure what you want to do once you find the matching drives.  Maybe run a subroutine to do some processing on that drive???

' Specify name of tag file to search for
strTagFile = "drive.tag"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Recover from errors in the code (prevent from ending script)
On Error Resume Next

' Access the WMI service on this computer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
If Err.Number = 0 Then
   ' Query WMI for all fixed disks on the system
   Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
     
   ' Search this drive for the file
   For Each objDisk in colDisks
      If objFSO.FileExists(objDisk.DeviceID & "\" & strTagFile) Then
         Wscript.Echo "Match found: " & objDisk.DeviceID & "\" & strTagFile
      End If
   Next
     
   ' Release objects
   Set colDisks = Nothing
   Set objWMIService = Nothing
End If

Open in new window

~bp
0
 

Author Comment

by:amendala
ID: 40405175
I sincerely apologize for not responding when you posted an answer.  I used to get E-mail notifications when a question received a response.  For some reason I didn't... gotta check that out.

Anyway, I'll look at this solution probably this evening and if it looks good or close enough, I'll wrap it up.  Thanks for your help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question