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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive File Search With VBScript 2

Status
Not open for further replies.

Wetzel

MIS
Jan 5, 2005
4
0
0
US
I need to search through entire drives, looking for *.fmp files. I need an inventory which includes path and last modified date. I'm going to put all of this into a csv file an import into excel.

My question is, can someone give me a push on how to recursively search for all files with a given extension? Once I find the file, I think I can just pull the other information off by using the FileSystemObject...

Thanks a lot for any help you can give.

-Mark Wetzel
 
Do a keyword search in this forum for fso recursive

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Something like this... I threw this together out of some old code and it's got a syntax error on line 16... probably something simple but have a look and see if this helps you.
**********************************************************
Option Explicit

Dim ObjFSO
Dim ObjStartDrive
Dim ObjFolder
Dim Folder
Dim ObjFile

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjStartDrive = ObjFSO.GetFolder("D:\")

For Each ObjFolder In ObjStartDrive.SubFolders
FindFiles(ObjFolder)

'##############################################
Function FindFiles(Folder)

Set Folder = ObjFSO.GetFolder(Folder)
For Each ObjFile In Folder.Files
If Instr(1, ObjFile.Name, ".cswlog", 1) Then
Wscript.Echo ObjFile.Path
Wscript.Echo ObjFile.DateCreated
Else
End If
Next

If Folder.SubFolders.Count > 0 Then
For Each Folder In Folder.SubFolders
FindFiles(Folder.Path)
Next
End If

End Function
'################################################
 
try this:

Code:
Dim FSO, WSH, objDirectory, objFile, TheFiles

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("Wscript.Shell")


Set objDirectory = FSO.GetFolder(InputBox("Enter Starting Folder path!"))
Set TheFiles = objDirectory.Files

Parentfolder =  mid(fso.GetFolder(objDirectory),InstrRev(fso.GetFolder(objDirectory),"\",-1,0)+1,len(fso.GetFolder(objDirectory)) - InstrRev(fso.GetFolder(objDirectory),"\",-1,0))
objextension = InputBox("Enter extension" & vbcrlf & vbcrlf & "Ex..." & vbcrlf & "mp3" & vbcrlf & "bmp" & vbcrlf & "exe")
	

If FSO.FileExists ("FileNames " & Parentfolder & ".txt") then
    FSO.DeleteFile "FileNames " & Parentfolder & ".txt"
End If

Set txtFileName = FSO.CreateTextFile ("FileNames " & Parentfolder & ".txt",TRUE)
Set txtFileName = Nothing
Set txtFileName = FSO.openTextFile("FileNames " & Parentfolder & ".txt", 8)

WorkWithSubFolders objDirectory

txtFileName.Close


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub WorkWithSubFolders(objDirectory)
	Dim MoreFolders, TempFolder

	ListFilesWithExtension objDirectory
	Set MoreFolders = objDirectory.SubFolders

	For Each TempFolder In MoreFolders
		WorkWithSubFolders TempFolder
	Next

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ListFilesWithExtension(objDirectory)
	Dim TheFiles

	Set TheFiles = objDirectory.Files
	For Each objFile in TheFiles
		strExt = fso.GetExtensionName(objFile.Path)
		If (strExt = objextension) Then
			txtFileName.writeline objFile.Path & vbtab & objFile.Size & vbtab & objFile.DateCreated & vbtab & objFile.DateLastModified
		end if
	Next
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Now that it is in a txt file you can import it into Excel or you could add code to the script and have the script do it for you.
 
Hey, Thanks a lot everyone!
Your information was very helpful. I think I have the script that will do what I need it to do.
It searches recursively from a defined starting point and puts the output (Filename, Filepath, Last Modified Date) into a CSV.
I appreciate the help

I'm new at this, so it's probably not the most efficient thing in the world, but hey, it works.
Here's what I came up with:
Code:
'sets varialbes
Dim objFSO, filecsv, csvname, path, colFiles, colFolders, tmpFile, tmpPath, tmpModDate, oneLine
strFolder = "r:"

'object creation
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolder)


'creates the file. sets the path of that file.
Set filecsv = objFSO.CreateTextFile("I:\My Documents\Filemaker Script\FilemakerInventory.csv", True)
path = objFSO.GetAbsolutePathName("I:\My Documents\Filemaker Script\FilemakerInventory.csv")

'gets the name of the csv file
csvname = objFSO.GetFileName(path)

Set colFiles = objFolder.Files

For Each File in colFiles
	set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
Next

If lcase(Right(objFile.Name,3)) = "fmp" Then
	tmpFile = objFile.Name
	tmpPath = objFile.Path
	tmpModDate = objFile.DateLastModified
	oneLine = tmpFile & "," & tmpPath & "," & tmpModDate
	filecsv.WriteLine(oneLine)
End If

If lcase(Right(objFile.Name,3)) = "fp3" Then
	tmpFile = objFile.Name
	tmpPath = objFile.Path
	tmpModDate = objFile.DateLastModified
	oneLine = tmpFile & "," & tmpPath & "," & tmpModDate
	filecsv.WriteLine(oneLine)
End If

ScanSubFolders(objFolder)

'search through the directory structure of the drive
Sub ScanSubFolders(objFolder)
	
	Set colFolders = objFolder.SubFolders
	
	For Each objSubFolder In colFolders
		Set colFiles = objSubFolder.Files
		
		For Each objFile in Colfiles
			If lcase(Right(objFile.Name,3)) = "fmp" Then
			 wscript.echo objFile.Name
				tmpFile = objFile.Name
				tmpPath = objFile.Path
				tmpModDate = objFile.DateLastModified
				oneLine = tmpFile & "," & tmpPath & "," & tmpModDate
				filecsv.WriteLine(oneLine)
			End If
			
			If lcase(Right(objFile.Name,3)) = "fp3" Then
			 wscript.echo objFile.Name
				tmpFile = objFile.Name
				tmpPath = objFile.Path
				tmpModDate = objFile.DateLastModified
				oneLine = tmpFile & "," & tmpPath & "," & tmpModDate
				filecsv.WriteLine(oneLine)
			End If
		Next
			ScanSubFolders(objSubFolder)
	Next
End Sub

'close the csv file
filecsv.Close

'output that goes to the command line
If objFSO.FileExists(path) Then
	wscript.echo ("Your file, " & csvname & ", has been created.")
Thanks again!
-wetzel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top