Hello, I have some code here that I have been using to format a single text file by stripping out all the junk I don't want in the file. This works great, but now I want to just do this process for ALL text files in a folder. I was trying to add a loop but keep getting errors at the "InFile" line. I don't think I am understanding how this needs to be done. Any help would be great along with some explanation of how it works!
Code:
Dim InputFile
Dim outFile
' Writing Data to a Text File
Const ForReading = 1
Dim words(1)
Dim msg
words(0) = "#C"
Set objFSO = CreateObject("Scripting.FileSystemObject")
InputFile = InputBox("Enter the you want to format: ")
Set inFile = objFSO.OpenTextFile("c:\80-230\" & InputFile & ".txt", ForReading)
'Set outFile = objFSO.OpenTextFile("c:\Temp2\output.txt", 8, True)
Set outFile = objFSO.OpenTextFile("c:\80-230\" & InputFile & "-Formatted.txt", 8, True)
Do Until inFile.AtEndOfStream
strSearchString = inFile.ReadLine
For i = 0 To UBound(words)-1
If InStr(strSearchString,words(i)) Then
msg = msg & Mid(Split(strSearchString,";")(0),4) & vbCrLf
End If
next
Loop
inFile.Close
outfile.WriteLine msg
outFile.Close
'Stripped file complete
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'Const ForReading = 1
Const ForWriting = 2
Set objNetwork = CreateObject("Wscript.Network")
strFile1 = "C:\80-230\" & InputFile & "-Formatted.txt"
strFile2 = "C:\80-230\NoDuplicates- " & InputFile & ".txt"
If Not objFSO.FileExists(strFile1) Then
MsgBox "Input File does not exist, Make the text file called" &_
" input.txt and try again!", vbOKOnly, "Error"
WScript.Quit
End If
Set objFile = objFSO.OpenTextFile(strFile1, ForReading)
If Not objFSO.FileExists(strFile2) Then
objFSO.CreateTextFile(strFile2)
End If
result = MsgBox("Are you just removing the blank lines", vbYesNo)
Select Case result
Case vbYes
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
If Not LCase(Left(strLine, 7)) = "version" Then
strNewContents = strNewContents & strLine & vbCrLf
End If
End If
Loop
Case vbNo
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
If Not LCase(Left(strLine, 7)) = "version" Then
strNewContents = strNewContents & "Case" & vbTab & Chr(34) & strLine & Chr(34) & vbCrLf
End If
End If
Loop
End Select
objFile.Close
Set objFile = objFSO.OpenTextFile(strFile1, ForWriting)
objFile.Write strNewContents
objFile.Close
Set objOutputFile = objFSO.OpenTextFile(strFile2, ForWriting, True)
Set objFile = objFSO.OpenTextFile(strFile1, ForReading)
Set Dict = CreateObject("Scripting.Dictionary")
Do until objFile.AtEndOfStream
strCurrentLine = objFile.ReadLine
If not Dict.Exists(strCurrentLine) then
objOutputFile.WriteLine strCurrentLine
Dict.Add strCurrentLine,strCurrentLine
End if
Loop
objOutputFile.Close
MsgBox"Task completed! ", vbInformation, "Format Files"