Solved

vbscript search for all files given the files extension

Posted on 2009-05-20
2
455 Views
Last Modified: 2012-05-07
I'm trying to create a vbscript to search for all files given the files extension but got bit stuck when im trying to create a sub function to search for the files in  the subfolders.

I wanted to create a sub function with a loop inside so it continue on to find the subfolders after it finish scanning the targeted folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOut = objFSO.CreateTextFile ("C:\scanreport.txt")
Set objFolder = objFSO.GetFolder("c:\backup")
Set colFiles = objFolder.Files
 
For Each objFile in colFiles
  If LCase(Right(objFile.Name, 3)) = "txt" Then 
		objOut.Write objFile.Name
		objOut.Write objFile.Path
		objOut.WriteLine
  End If
Next
 
objOut.Close
wscript.Quit

Open in new window

0
Comment
Question by:vbsnoob
[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 92

Accepted Solution

by:
Patrick Matthews earned 125 total points
ID: 24430686
Dim objFSO, objOut, objFolder

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOut = objFSO.CreateTextFile ("C:\scanreport.txt", True)
Set objFolder = objFSO.GetFolder("c:\backup")

ListTheFiles objFolder, objOut

objOut.Close
Set objOut = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

Sub ListTheFiles(UseFolder, UseTS)

    Dim objSF, objFile

    For Each objFile In UseFolder.Files
        If LCase(Right(objFile.Name, 3)) = "txt" Then objOut.WriteLine objFile.Path
    Next

    For Each objSF In UseFolder.SubFolders
        ListTheFiles objSF, UseTS
    Next

    Set objFile = Nothing
    Set objSF = Nothing

End Sub
0
 

Author Closing Comment

by:vbsnoob
ID: 31583416
thank you for ur help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

739 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