0
我有> 100MB(超过20L行)的XML文件,我无法直接将它传递给我的一个进程。我需要通过假设一些分隔符将其拆分为多个较小的文件,并尝试在VBScript中使用FileSystemObject以及BATCH文件选项。两者都需要超过8分钟才能读取并创建10000行的第一个小文件。将大文件读取和分割为多个小文件的最佳方法
请为此任务传达一些良好的性能导向选项。
感谢您的帮助。
方法一:
Function SplitXML()
Dim oSrcFile, oTgtFile, strHeader, intFiles, strContent, intSize
Dim arrLines()
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFilePath = Application.GetOpenFilename
intLinesToSplit = InputBox("Enter the No of Lines to split with for each file:", "XML Splitter", 10000)
strTgtPath = Replace(strFilePath, oFSO.GetFileName(strFilePath), "")
strFileName = Replace(oFSO.GetFileName(strFilePath), ".xml", "")
Set oSrcFile = oFSO.OpenTextFile(strFilePath)
'Headers
strHeader = "": strContent = ""
Do
strTemp = oSrcFile.ReadLine
strHeader = strHeader & vbNewLine & strTemp
Loop While InStr(1, strTemp, "</BLHeader>", 1) <= 0
'Split
intTemp = 0: intFiles = 0: blnNewFile = True: intSize = -1
Do While Not oSrcFile.AtEndOfStream
intTemp = intTemp + 1
'Content
'intSize = intSize + 1
'ReDim Preserve arrLines(intSize)
'arrLines(intSize) = oSrcFile.ReadLine
strTemp = oSrcFile.ReadLine
strContent = strContent & vbNewLine & strTemp
If intTemp >= intLinesToSplit Then
If InStr(1, strTemp, "</EndingTag>", 1) > 0 Then
'Add Header
intFiles = intFiles + 1
Set oTgtFile = oFSO.CreateTextFile(strTgtPath & strFileName & "_" & intFiles & ".xml", True)
oTgtFile.WriteLine strHeader
'Add Content
oTgtFile.WriteLine strContent 'Join(arrLines, vbNewLine)
'Add tail
oTgtFile.WriteLine "</FinalFileTag>"
oTgtFile.Close
End If
End If
Loop
oSrcFile.Close
端功能
方法2:
@echo off
setlocal EnableDelayedExpansion
set InFile=c:\ee\EE28352646\in.txt
set OutDir=c:\ee\EE28352646
REM Can not be larger than 2147483648 !!!
set MaxLines=1000000
if not exist "%InFile%" (
echo *ERROR* Input file does not exist!
exit /b
)
if not exist "%OutDir%\" (
echo *ERROR* Output folder does not exist!
exit /b
)
for %%A in ("%InFile%") do (
set Name=%%~nA
set Ext=%%~xA
)
set /a Line=MaxLines+1
set File=0
for /f "usebackq tokens=*" %%A in ("%InFile%") do (
set /a Line+=1
if !Line! GTR %MaxLines% (
set /a File+=1
set OutFile=%OutDir%\%Name%_!File!%Ext%
if exist "!OutFile!" del "!OutFile!"
set Line=1
)
echo.%%A>>"!OutFile!"
)
没有什么可以在你的头文件中优化的部分,恕我直言。但是在你的'Split'部分:首先,事先做好'oTgtFile.WriteLine strHeader',然后你可以一行一行地写入内容:'oTgtFile.WriteLine strTemp'。避免内存和时间耗尽'strContent = strContent&vbNewLine&strTemp' – JosefZ 2014-12-08 00:06:21