Solved

vbscript search for all files given the files extension

Posted on 2009-05-20
2
456 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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 look for a specific file type in a local or remote server directory using PHP.

622 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