2014-04-30 89 views
1

下午好!分割文本文件時應對循環批處理文件問題

長時間讀者,第一次海報!我一直在嘗試修改一個工作批處理文件以應對可變性問題,這段時間一直很愉快。情況是,我有一個可變大小的文本文檔,通常可以分成252行的部分。下面的代碼工作就像一個冠軍:

@echo off & setlocal EnableDelayedExpansion 

set param=%* 
if not defined param (
    echo. 
    echo. Usage: batchsplit [device:][pathname]filename 
    goto :EOF 
) 
set param=%param:"=% 
if not exist "%param%" (
    echo. 
    echo. File "%param%" not found 
    goto :EOF 
) 
for %%j in ("%param%") do (
    set name=%%~dpnj 
    set ext=%%~xj 
) 
for /F %%j in ('type "%param%" ^| find /V /C ""') do set Full=%%j 
set /A Split=%Full%/252 
for /L %%G in (1,1,%Split%) do type nul > "%name%_%%G%.new" 
set X=1 
set N=1 
set Q=1 
set limit = 252 
for /F "tokens=1* delims=]" %%j in ('type "%param%" ^| find /V /N ""') do (
    set /A N+=1 
    set /A Q+=1 
    echo.%%k>> "%name%_!X!%.new" 
    if !Q! gtr 252 (
    set /A X+=1 
    set /A Q=1 
) else if !N! gtr Full (goto theend 
) 
) 
:theend 
echo split into %split% files with 252 lines each 
rem pause 

然而,有一些改變文本的格式,而現在不是四頁的每個分割文件63行,也可以是完全可變的。唯一的常數是這樣的最後一行,這之前爲一個63行頁面的剩餘空間:

ON THIS FORM IS COMPLETE AND CORRECT AS NOTED:___________________ 

注意,有在其前面的單個空間,以及多個空格,冒號和下劃線字符。作爲我的頭號人物,我想我可以在for循環中插入一個if-then語句來觸發批處理以分割到下一頁。不過,我現在可以遠離這一點。這是我一直在砸我的頭代碼:

rem @echo off & setlocal EnableDelayedExpansion 
setlocal EnableDelayedExpansion 

set param=%* 
if not defined param (
    echo. 
    echo. Usage: textsplit [device:][pathname]filename 
    goto :EOF 
) 
set param=%param:"=% 
if not exist "%param%" (
    echo. 
    echo. File "%param%" not found 
    goto :EOF 
) 
for %%j in ("%param%") do (
    set Name=%%~dpnj 
    set ext=%%~xj 
) 
for /F %%j in ('type "%param%" ^| find /V /C ""') do set Full=%%j 
set stopvar= ON THIS FORM IS COMPLETE AND CORRECT AS NOTED:___________________ 
set Split=1 
echo %stopvar% 
set X=1 
type nul > "%name%_!X!%.new" 
set N=1 
set Q=1 
set S=0 
set L=63 
for /F "tokens=1* delims=]" %%j in ('type "%param%" ^| find /V /N ""') do (
    set /A N+=1 
    echo %N% 
    set /A Q+=1 
    echo %Q% 
    echo.%%k>> "%name%_!X!%.new" 
    if ["%%k%" == "!stopvar!"] (
    set /A S+=1 
    ) 

    if !Q! gtr !L! (
    if !S! == 1 (
    set /A X+=1 
     set /A Q=1 
type nul > "%name%_!X!%.new" 
set /A Split+=1 
set S=0 
    ) 
     else set /A L+=63 
     else if !N! gtr Full goto theend 

) 
:theend 
echo Split into %split% files! 
pause 

的前提是,每63線,停止變量(S)被選中。如果它關閉(0),則該批次將繼續寫入另外63行(一頁)。如果stopvar與for循環所讀取的行相匹配,則S變爲1.當程序再次檢查時,它將創建一個新文件並開始寫入新文件。現在,基於關閉@echo關閉hangup在for循環。見下:

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>rrtextsplit texttest.txt 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>rem @echo off & setlocal Enabl 
eDelayedExpansion 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>setlocal EnableDelayedExpansio 
n 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set param=texttest.txt 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>if not defined param (
echo. 
echo. Usage: rrtextsplit [device:][pathname]filename 
goto :EOF 
) 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set param=texttest.txt 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>if not exist "texttest.txt" (
echo. 
echo. File "texttest.txt" not found 
goto :EOF 
) 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>for %j in ("texttest.txt") do 
(
set Name=%~dpnj 
set ext=%~xj 
) 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>(
set Name=C:\Users\theangryasiancp\Desktop\TEXT_Split_Test\texttest 
set ext=.txt 
) 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>for /F %j in ('type "texttest. 
txt" | find /V /C ""') do set Full=%j 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set Full=567 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set stopvar= ON THIS FORM IS C 
OMPLETE AND CORRECT AS NOTED:___________________ 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set Split=1 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>echo ON THIS FORM IS COMPLETE 
AND CORRECT AS NOTED:___________________ 
ON THIS FORM IS COMPLETE AND CORRECT AS NOTED:___________________ 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set X=1 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>type nul 1>"C:\Users\theangry 
asiancp\Desktop\RRRR_Split_Test\texttest_!X!.new" 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set N=1 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set Q=1 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set S=0 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test>set L=63 

C:\Users\theangryasiancp\Desktop\TEXT_Split_Test> 

你的想法是什麼?我在哪裏出錯了?我希望我可以使用不同的東西,但是由於公司內部的原因,我不能。謝謝你的幫助!

回答

0
@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "name=q23396663" 
SET "ext=.txt" 
SET /a pagelength=10 
SET "targetstring= ON THIS FORM IS COMPLETE AND CORRECT AS NOTED:___________________" 
SET /a filenum=0 
SET /a linecount=pagelength + 1 
FOR /f "tokens=1*delims=]" %%a IN (
'find /v /n "" "%name%%ext%"') DO (
IF !linecount! GEQ %pagelength% (
    SET /a linecount=0 
    SET /a filenum+=1 
) 
>>U:\%name%_!filenum!.new ECHO(%%b 
IF "%%b"=="%targetstring%" SET /a linecount=pagelength 
SET /a linecount+=1 
) 

GOTO :EOF 

出於測試目的,我建立了一個包含觸發數據的文件q23396663.txt。我已經離開目標目錄U:\適合我,頁長10,這使我的測試更容易。

+0

該解決方案完美地滿足了我的要求。在實現批處理時,我意識到我需要它繼續到頁面末尾生成輸出的「最後一行」,因爲我正在操作的輸出文件將剩餘的空白空間插入到以下分割文件的頂部。我爲此創建了一個解決方案,我將「回答我自己的問題」。非常感謝您的解決方案,因爲它有助於指導我前進。 – angryasianguy

0
@echo off 
setlocal EnableDelayedExpansion 


REM ------------------THIS SECTION SPECIFIES THE FILE------------------------- 

set param=%* 
if not defined param (
    echo. 
    echo. Usage: filesplit [device:][pathname]filename 
    goto :EOF 
) 
set param=%param:"=% 
if not exist "%param%" (
    echo. 
    echo. File "%param%" not found 
    GOTO :EOF 
) 
for %%j in ("%param%") do (
    set name=%%~dpnj 
    set ext=%%~xj 
) 

ECHO SPLITTING %name%.%ext% ................. 

REM ----------------THIS SECTION SETS THE VARIABLES--------------------------- 

set "trigger= ON THIS FORM IS COMPLETE AND CORRECT AS NOTED:___________________" 
set /a pagelength=63 
set /a filenum=0 
set split=1 
set /a linecount=pagelength 
set stopvar=0 

REM ------------------THIS SECTION IS THE FOR LOOP---------------------------- 

FOR /f "skip=2 tokens=1* delims=]" %%a IN (
'find /v /n "" "%name%%ext%"') DO (
     SET /a linecount-=1 
IF !linecount! LEQ 0 (
IF !stopvar! EQU 1 (
SET /a "linecount=pagelength" 
SET /a filenum+=1 
SET /a split+=1 
SET /a stopvar-=1 
) else set /a "linecount=pagelength" 
) 
    echo.%%b>> "%name%_!filenum!.new" 
IF "%%b"=="%trigger%" (
    set /a "stopvar+=1" 
           REM THIS TRIGGERS TO CHANGE OUTPUT 
    set /a linecount+=1 
           REM THIS WILL ADJUST THE OUTPUT EOF 
    ) 
) 

REM ----------------THIS SECTION ENDS THE FOR LOOP---------------------------- 


ECHO Split into %split% files! 
ping 1.1.1.1 -n 1 -w 2500 > nul 
            REM THIS PAUSES THE BATCH FOR A SEC 

正如我貼在上面的評論,這僅僅是一個,如果需要,他們從具有頂部不必要的空格保持輸出文件佔空格第一個答案的變化。當打印管理器在開始下一部分之前直到頁面結束時才吐出空白,而不是直接進入下一部分時,這是特別有用的。