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
441 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
2 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now