2013-08-19 58 views
0

我一直在使用下面的代碼將我的文本文件分割成兩個文件。我的原始文件只包含20線,我試圖分裂成2個文件。即使當該腳本運行,我得到的消息在最後說,過程是完整的,我看不到任何分裂的文件在輸出location.Please告訴我什麼是代碼中的問題;我是新的VBScript所以請幫助我。感謝提前:)沒有得到輸出分裂文本文件在VBScript

Dim Counter 
Const InputFile = "C:\Cs.txt" 
Const OutputFile = "C:\Users\rmehta\Desktop" 
Const RecordSize = 10 
Const ForReading = 1 
Const ForWriting = 2 
Const ForAppending = 8 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTextFile = objFSO.OpenTextFile (InputFile, ForReading) 
Counter = 0 
FileCounter = 0 
Set objOutTextFile = Nothing 

Do Until objTextFile.AtEndOfStream 
if Counter = 0 Or Counter = RecordSize Then 
    Counter = 0 
    FileCounter = FileCounter + 1 
    if Not objOutTextFile is Nothing then objOutTextFile.Close 
    Set objOutTextFile = objFSO.OpenTextFile(OutputFile & "_" & FileCounter & ".txt", ForWriting, True) 
end if 
strNextLine = objTextFile.Readline 
objOutTextFile.WriteLine(strNextLine) 
Counter = Counter + 1 
Loop 
objTextFile.Close 
objOutTextFile.Close 
Msgbox "Split process complete" 

回答

1

如果你離開了所有的虛假脂肪(在文本流有一個行計數器和第一輸出文件可以循環之前打開),你會得到

Option Explicit 
    Const cnSize = 10 
    Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject") 
    Dim sDir : sDir  = "..\testdata\18308970" 
    Dim tsIn : Set tsIn = oFS.OpenTextFile(oFS.BuildPath(sDir, "all.txt")) 
    Dim nFCnt : nFCnt  = 0 
    Dim tsOut : Set tsOut = oFS.CreateTextFile(oFS.BuildPath(sDir, nFCnt & "-part.txt")) 
    Do Until tsIn.AtEndOfStream 
    If 0 = tsIn.Line Mod cnSize Then 
     tsOut.Close 
     nFCnt  = nFCnt + 1 
     Set tsOut = oFS.CreateTextFile(oFS.BuildPath(sDir, nFCnt & "-part.txt")) 
    End If 
    tsOut.WriteLine tsIn.ReadLine() 
    Loop 
    tsIn.Close 
    tsOut.Close 

這個'工作' - 如果你有文件夾,輸入文件和權限 - 是顯而易見的。在你的代碼中,問題

>> Const OutputFile = "C:\Users\rmehta\Desktop" 
>> FileCounter = 0 
>> WScript.Echo OutputFile & "_" & FileCounter & ".txt" 
>> 
C:\Users\rmehta\Desktop_0.txt 

是深藏不露的。

+0

與保持cnSize = 11的小變化...非常感謝!非常有用:) – M3HTA

+0

請幫我這個:http://stackoverflow.com/q/18722318/2636830 – M3HTA

+0

在此先感謝:) – M3HTA