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
566 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 56

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

630 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