2015-11-04 50 views
0

我 大家好,文件分割基於

我需要根據文件中匹配變量的內容拆分文件。 只有在以25開頭的行之前和剛剛在split參數之後的行不同時,我需要在拆分參數(此處爲「///」)上拆分文件。 然後應使用:25:標記的表達式命名文件。 我需要使用.cmd命令來做到這一點... 我已經厭倦了令牌,但一直不成功。 有人可以幫我解決這個問題嗎?

例如,下面的文件的初始文件:

:01:BLABLA 
:25:123456 
:71:BLABLABLA1 
:86:BLABLABLA2 
:71:BLABLABLA3 
:86:BLABLABLA4 
/// 
:25:123456 
:71:BLABLABLA5 
:86:BLABLABLA6 
:71:BLABLABLA7 
:86:BLABLABLA8 
/// 
:25:123457 
:71:BLABLABLA0 
:86:BLABLABLA9 

應在兩個文件進行分割:

123456.TXT

:01:BLABLA 
:25:123456 
:71:BLABLABLA1 
:86:BLABLABLA2 
:71:BLABLABLA3 
:86:BLABLABLA4 
/// 
:25:123456 
:71:BLABLABLA5 
:86:BLABLABLA6 
:71:BLABLABLA7 
:86:BLABLABLA8 

123457.TXT

:25:123457 
:71:BLABLABLA0 
:86:BLABLABLA9 
+0

帶有:25的行總是第一個_below_分割參數? – Aacini

+0

嗨Aacini。不,25:並不總是低於分割參數的第一個。這就是拆分應該發生在「///」而不是「:25:」上的原因。 – Chris

+0

你應該在你的例子中在'///'和':25:...'之間插入一行,或者在文本中仔細解釋這一點! – Aacini

回答

0

試試這個:

@echo off 
setlocal EnableDelayedExpansion 
set inFile=inputTextFile.txt 
set outFile=tempFile.txt 
for /F "tokens=*" %%L in (%inFile%) do (
    set currentline=%%L 
    if "%%L"=="///" (
     set outFile=tempFile.txt 
    ) 
    if "!currentLine:~0,4!"==":25:" (
     set outFile=!currentLine:~4!.txt 
     if exist tempFile.txt (
      if exist !outFile! (
       copy !outFile!+tempFile.txt !outFile! 
       del tempFile.txt 
      ) else ren tempFile.txt !outFile! 
     ) 
    ) 
    echo %%L>>!outFile! 
) 

只是正確的文件/路徑替換inputTextFile.txt

+0

嗨MichaelS, 謝謝!但是,我怎麼能讓腳本在「///」而不是「:25:」上分割文件呢?因爲:25:並不總是在「///」下面的第一行,並且您的腳本正在剪切文件:25: 謝謝! – Chris

+0

@Chris我的代碼沒有分割:25:但在///上。如果:25:尚未找到,它將所有內容保存在臨時文件中。只要我們發現:25:它將文件重命名爲後面的字符串:25 :.在///上它開始再次寫入一個新的臨時文件。等等。 – MichaelS

0
@echo off 
setlocal EnableDelayedExpansion 

del temp.tmp *.out 2> NUL 

set "file=not25tag" 
for /F "tokens=1* delims=:" %%a in (input.txt) do (
    if "%%a" equ "25" set "file=%%b" 
    if "%%a" neq "///" (
     echo :%%a:%%b>> temp.tmp 
    ) else (
     if not exist "!file!.out" (
     ren temp.tmp "!file!.out" 
    ) else (
     echo ///>> "!file!.out" 
     type temp.tmp >> "!file!.out" 
     del temp.tmp 
    ) 
     set "file=not25tag" 
    ) 
) 

if not exist "!file!.out" (
    ren temp.tmp "!file!.out" 
) else (
    echo ///>> "!file!.out" 
    type temp.tmp >> "!file!.out" 
    del temp.tmp 
) 

ren *.out *.txt