Posted by : Sudhir Chekuri Thursday, 3 October 2013

Explanation: a.txt is the file i want to check and it is stored in a variable named as filename. I created an array named drives which can store 6 values in it. I saved all the drive names in which i want to check for a.txt file. For loop is used to repeat 5 times. Two if conditions are used in for loop. Outer if condition to check wheather that drive exists in the system or not. Inner if condition checks wheather file exists in the root folder of that drive or not. If the file exists then that drive name is saved in the variable named msg to display after coming out of the for loop in the last line of code.

VBScript code to search for a file in all drives in a system

Set fso = CreateObject("Scripting.FileSystemObject")
msg="a.txt doesn't exist"
Dim drives(5)

drives(0)="C"
drives(1)="D"
drives(2)="E"
drives(3)="F"
drives(4)="G"
drives(5)="H"
filename   = "a.txt"

For i = 0 To 5
  drv = drives(i)
   If fso.DriveExists(drv) Then
If fso.FileExists(fso.BuildPath(drv &":\\", filename)) Then
  msg= filename & " exist in drive: "& drv  
  End If
   End If
Next
 WScript.Echo msg


Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -