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

Read All files in Directory 1

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
How can i open each file in a directory line by line and if the line = "TEST!" then wirte that line to a new file?
 
Do a search in this forum for 'File system object' and/or FSO. There are plenty of examples.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Something like this should work.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*********************************************************
'Change "C:\TESTIT.TXT" to where you want the file to save
Set f1 = objFSO.OpenTextFile("C:\TESTIT.TXT", 8, True)
'*********************************************************

'*********************************************************
'Change "C:\VBFiles" to the folder you want to recurse through
objStartFolder = "C:\TESTIT"
'*********************************************************

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
    Set f2 = objFSO.OpenTextFile(objFolder.Path & "\" & objFile.Name, 1)
    FirstLine = f2.ReadLine
    If FirstLine = "TEST!" Then f1.WriteLine FirstLine
    f2.Close
Next

ShowSubFolders objFSO.GetFolder(objStartFolder)
f1.Close
Set f1 = Nothing
Set f2 = Nothing
Set objFSO = Nothing
MsgBox "Complete!", vbInformation

Sub ShowSubFolders(Folder)
    For Each Subfolder In Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile In colFiles
            Set f2 = objFSO.OpenTextFile(Subfolder.Path & "\" & objFile.Name, 1)
            FirstLine = f2.ReadLine
            If FirstLine = "TEST!" Then f1.WriteLine FirstLine
            f2.Close
            f1.WriteLine Subfolder.Path & "\" & objFile.Name
        Next
        ShowSubFolders Subfolder
    Next
End Sub

Swi
 
Sorry, I had an extra line in there I shouldn't have.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*********************************************************
'Change "C:\TESTIT.TXT" to where you want the file to save
Set f1 = objFSO.OpenTextFile("C:\TESTIT.TXT", 8, True)
'*********************************************************

'*********************************************************
'Change "C:\TESTIT" to the folder you want to recurse through
objStartFolder = "C:\TESTIT"
'*********************************************************

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
    Set f2 = objFSO.OpenTextFile(objFolder.Path & "\" & objFile.Name, 1)
    FirstLine = f2.ReadLine
    If FirstLine = "TEST!" Then f1.WriteLine FirstLine
    f2.Close
Next

ShowSubFolders objFSO.GetFolder(objStartFolder)
f1.Close
Set f1 = Nothing
Set f2 = Nothing
Set objFSO = Nothing
MsgBox "Complete!", vbInformation

Sub ShowSubFolders(Folder)
    For Each Subfolder In Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile In colFiles
            Set f2 = objFSO.OpenTextFile(Subfolder.Path & "\" & objFile.Name, 1)
            FirstLine = f2.ReadLine
            If FirstLine = "TEST!" Then f1.WriteLine FirstLine
            f2.Close
        Next
        ShowSubFolders Subfolder
    Next
End Sub

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top