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 关闭挂断是在 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>

你觉得呢?你有没有什么想法?我的批次哪里出错了?我希望我可以使用不同的东西,但可惜我不能,因为公司内部的原因。谢谢你的帮助!

4

2 回答 2

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

正如我在上面的评论中发布的那样,这只是第一个答案的变体,如果需要空格来防止输出文件在顶部出现不必要的空格,则可以考虑空格。当打印管理器在开始下一部分之前只是吐出空白直到页面末尾而不是直接进入下一部分时,这尤其有用。

于 2014-05-02T17:06:10.737 回答
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,这使我的测试更容易。

于 2014-05-01T00:56:47.760 回答