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