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?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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
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