2013-08-19 2 views
0

다음 코드를 사용하여 텍스트 파일을 두 개의 파일로 분할했습니다. 원본 파일은 20 개의 파일로 나뉘어져 있으며 2 개의 파일로 나뉘어 있습니다. 스크립트가 실행되고 프로세스가 완료되면 출력 위치에서 분할 된 파일을 볼 수 없다는 메시지가 맨 끝에 표시됩니다. 코드에서 문제가 무엇인지 말해 주시고, 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

관련 문제