2013-05-17 56 views
0

我正在尋找一個簡單的批處理腳本,將我的文本文件分成兩個文件。通過分隔符將文本文件拆分成2個文件

test 
test1 
test2 
test5 
test 
test1 
test2 
test5 
============================================================================ 
test 
test1 
test2 
test5 
test 
test1 

回答

3

試試這個:

@echo off&setlocal 
set "file=file.txt" 
for /f "delims=[]" %%i in ('^<"%file%" find /n "="') do set "split=%%i" 
(for /f "tokens=1*delims=[]" %%i in ('^<"%file%" find /n /v ""') do if %%i lss %split% echo(%%j)>"%file%.new1" 
<"%file%">"%file%.new2" more +%split% 
type "%file%.new?" 

..output是:

 
file.txt.new1 


test 
test1 
test2 
test5 
test 
test1 
test2 
test5 

file.txt.new2 


test 
test1 
test2 
test5 
test 
test1 
的文本文件,通過與

============================================================================ 

文本文件看起來像這樣分離

+0

我試圖運行這個批處理文件,但是當我執行它時,它掛起。當我想停止執行時,我必須終止它。 – user1809753

+0

這是'findstr'中的一個「bug」,你的輸入文件必須以最後一行的「CR/LF」結尾。 – Endoro

+0

我做了一個編輯,現在代碼使用'find',我希望這會對你有效。 – Endoro

2
@echo off 
set /p file=FILE TO PROCESS : 
del /q /f out.file* 
setlocal enableDelayedExpansion 
set out_file_counter=1 
for /f "usebackq delims=" %%L in ("%file%") do (
set line=%%L 
if "!line:~0,5!" equ "=====" (
    set /a out_file_counter=out_file_counter+1 

) else (
    echo !line!>>out.file.!out_file_counter! 
) 
) 
endlocal 
+0

謝謝你的回覆。當我運行這個文件時,我沒有得到任何輸出。 – user1809753

+0

你應該改變'(myfile.txt)'你的文件路徑... – npocmaka

+0

我已經改變了腳本有點更容易使用 – npocmaka