I'm trying to do some crude parsing of a bunch of text files. Basically, I'm looking to remove characters like (){}[]"'
, then replace colons with semicolons then replace strings with better looking strings.
The kicker is that I have about 1,500 files that need to have this done to them. If I merge all the files, first, then try to do the parsing, the app stops responding.
I have been using windows macros to do this, and it works on each of the files individually, but I don't know how to have it do it to all the files in that directory.Example of the code I'm using:
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "["
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
开发者_如何学Go .Text = ","
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = """"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
The following sub will iterate through a directory you specify and make the string changes you want:
Option Explicit
Sub FileProcessing()
Dim fileDirectory As String
Dim fileName As String
Dim fileContents As String
Dim inputFileNumber As Integer
Dim outputFileNumber As Integer
Dim iFileCount As Integer
fileDirectory = "C:\deleteme\"
'Get first file in directory
fileName = Dir(fileDirectory & "*.*")
'Begin loop to iterate through each file
Do While fileName <> ""
If iFileCount Mod 50 Then 'Display a message in Immediate window for every 50 files
Debug.Print "Working on file number " & iFileCount & " : " & fileName
End If
'Open File
inputFileNumber = FreeFile
Open fileDirectory & fileName For Input As #inputFileNumber
'Put file contents into a variable
'NOTE: A variable-length string can contain up to approximately 2 billion (2^31) characters.
fileContents = Input$(LOF(inputFileNumber), 1)
'Close the File
Close #inputFileNumber
'Do your replacements
fileContents = Replace(fileContents, "(", "")
fileContents = Replace(fileContents, ")", "")
fileContents = Replace(fileContents, "{", "")
fileContents = Replace(fileContents, "}", "")
fileContents = Replace(fileContents, "[", "")
fileContents = Replace(fileContents, "]", "")
fileContents = Replace(fileContents, ":", ";")
'Get the output file ready to write:
outputFileNumber = FreeFile
Open fileDirectory & fileName For Output As #outputFileNumber
Print #outputFileNumber, fileContents
Close #outputFileNumber
'Get next file
fileName = Dir
Loop
MsgBox "Finished"
End Sub
精彩评论