Go Premium for a chance to win a PS4. Enter to Win

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
?
639 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 59

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

963 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