2011-01-23 86 views
0

我試圖做一些文本文件的粗略解析。基本上,我希望刪除像(){}[]"'這樣的字符,然後用分號替換冒號,然後用更好看的字符串替換字符串。VBA解析多個文本文件?

踢球者是我有大約1,500個文件,需要對他們做這件事。如果我合併所有的文件,首先,然後嘗試解析,應用程序停止響應。
我一直在使用Windows宏來做到這一點,它可以在每個文件上單獨運行,但我不知道如何讓它執行該目錄中的所有文件。我使用的代碼

例子:

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 
    .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 
+0

你們願意接受一個PowerShell的解決方案還是你有意使用VB? – squillman 2011-01-23 00:40:09

回答

1

以下子將通過目錄遍歷您指定並進行串變化,你想:

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