Parsing VBA解析多个文本文件?

Parsing VBA解析多个文本文件?,parsing,vba,Parsing,Vba,我试图对一堆文本文件进行一些粗略的解析。基本上,我希望删除像(){}[]”这样的字符,然后用分号替换冒号,然后用外观更好的字符串替换字符串 更重要的是,我有大约1500个文件需要对其进行分析。如果我先合并所有文件,然后尝试进行分析,应用程序将停止响应。 我一直在使用windows宏来完成这项工作,它可以单独处理每个文件,但我不知道如何让它处理该目录中的所有文件 我正在使用的代码示例: Selection.Find.Execute Replace:=wdReplaceAll With Select

我试图对一堆文本文件进行一些粗略的解析。基本上,我希望删除像
(){}[]”这样的字符,然后用分号替换冒号,然后用外观更好的字符串替换字符串

更重要的是,我有大约1500个文件需要对其进行分析。如果我先合并所有文件,然后尝试进行分析,应用程序将停止响应。
我一直在使用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

以下子项将遍历指定的目录,并进行所需的字符串更改:

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
选项显式
子文件处理()
将文件目录设置为字符串
将文件名设置为字符串
将文件内容设置为字符串
Dim inputFileNumber为整数
Dim outputFileNumber为整数
Dim iFileCount为整数
fileDirectory=“C:\deleteme\”
'获取目录中的第一个文件
fileName=Dir(fileDirectory&“****”)
'开始循环以遍历每个文件
文件名“”时执行此操作
如果iFileCount Mod 50,则“在即时窗口中每50个文件显示一条消息”
调试。打印“处理文件号”&iFileCount&“:”&fileName
如果结束
'打开文件
inputFileNumber=FreeFile
打开文件目录和文件名以作为#inputFileNumber输入
'将文件内容放入变量中
'注意:可变长度字符串最多可包含约20亿(2^31)个字符。
fileContents=Input$(LOF(inputFileNumber),1)
'关闭文件
关闭#输入文件号
“做你的替补。”
fileContents=Replace(fileContents,(,)
fileContents=Replace(fileContents,“)”,“”)
fileContents=Replace(fileContents,{,“”)
fileContents=Replace(fileContents,},“”)
fileContents=Replace(fileContents,“[”,“”)
fileContents=Replace(fileContents,“]”,“”)
fileContents=Replace(fileContents,“:”,“;”)
'将输出文件准备好写入:
outputFileNumber=FreeFile
打开文件目录和文件名,输出为#outputFileNumber
打印#输出文件号,文件内容
关闭#输出文件号
'获取下一个文件
fileName=Dir
环
MsgBox“已完成”
端接头

您愿意接受Powershell解决方案还是希望使用VB?我愿意接受任何有效的解决方案。不过,我恐怕我对Powershell一无所知,所以如果不太复杂的话,我可能需要一些指导。我很确定您指的是VBA,而不是VB。您将获得更多答案,并带有人们遵循的标记。这就是我说,你在找这样的东西吗: