Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for files using vb script 1

Status
Not open for further replies.

ultraplop

Technical User
Feb 26, 2002
1
0
0
US
Hello

I'm a beginning programmer whos learning vb script. One of things I would like to do is use vb script to search for a files with certain extentions much like you can do with the dir command or basic windows search functions.

Right now I can use the folder and file objects to search for files if I specify a path, but I have absolutely no idea how to get it to search through an entire directory tree. maybe i could if i knew all the subfolders and all thier names, but most of the time I'm not going to know how deep the directory tree goes. Is thier an easy way to do this? I'm kind of stumped, I'm hoping someone can point me in the right direction

Thanks
 
Try this one. Copy and paste into notepad
'Purpose:
'Recursively search a directory of files for a match by extension
' and place the results into an Excel spreadsheet
'
'Usage:
'Simply double-click the vbs file. The script will prompt for
' a directory to begin searching. It will then continue
' to search recursively and place all matched files (match
' by extension) in the spreadsheet along with file details.
'
'Requires:
'The usual VBScript requirements plus Excel. Tested with Excel 2000,
' but should work with other versions as well.
'
' Disclaimer - Please test the script carefully on a local machine before running
' on a network.
'

Dim FSO, WSH, objDirectory, objFile, TheFiles
'
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("Wscript.Shell")
Set objDirectory = FSO.GetFolder(InputBox("Enter Starting Folder"))
Set TheFiles = objDirectory.Files
'
' Create and Set up Excel Objects
Column = 1
Row = 1
RowErr = 1
Set objXL = WScript.CreateObject("Excel.Application")
'
objXL.Workbooks.Add
objXL.Cells(1,Column).Value = "Parent Folder"
objXL.Cells(1,Column+1).Value = "File Name"
objXL.Cells(1,Column+2).Value = "File Size"
objXL.Cells(1,Column+3).Value = "Date Created"
objXL.Cells(1,Column+4).Value = "Date Last Modified"
objXL.Visible = True
'
WorkWithSubFolders objDirectory
'
Sub WorkWithSubFolders(objDirectory)
Dim MoreFolders, TempFolder
ListFilesWithExtension objDirectory
Set MoreFolders = objDirectory.SubFolders
For Each TempFolder In MoreFolders
WorkWithSubFolders TempFolder
Next
End Sub

'
'ListFilesWithExtension objDirectory
'
Sub ListFilesWithExtension(objDirectory)
Dim TheFiles
Set TheFiles = objDirectory.Files
For Each objFile in TheFiles
strExt = fso.GetExtensionName(objFile.Path)
If (strExt = "jpg") Or (strExt ="mpg") Or (strExt = "scr") Then
Row = Row+1
objXL.Cells(Row,Column).Value = objDirectory
objXL.Cells(Row,Column+1).Value = objFile.Name
objXL.Cells(Row,Column+2).Value = objFile.Size
objXL.Cells(Row,Column+3).Value = objFile.DateCreated
objXL.Cells(Row,Column+4).Value = objFile.DateLastModified
End If
Next
End Sub
'
MsgBox("All Done!")
WScript.Quit
 
Would this be of any use?

' If this does not work, then need to install a vb5cce.exe file.

on error resume next

set WSHShell = wscript.CreateObject("wscript.Shell")
set objDlg = wscript.CreateObject("MSComDlg.CommonDialog.1")

objDlg.Filter = "All Files (*.*)|*.*|VBScript Files (*.vbs)|*.vbs"
objDlg.FilterIndex = 2
objDlg.MaxFileSize = 260
objDlg.CancelError = true
objDlg.ShowOpen

msgbox "Set to run"

WSHShell.popup "The path is " & objDlg.Filename

Parke
 
Can You search also for files containing specific text?
Then this script that combines search and results in spreadsheet would be very useful fo me :)
Any ideas?
Thanx in advance,
/R
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top