Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
602 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 57

Accepted Solution

by:
Bill Prew earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

670 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