使用batch with operator分割文本文件

时间:2014-08-06 05:40:35

标签: batch-file text split

我一直在处理一个基于重复文本拆分文本文件的批处理文件

例如:

<AL> 999939339393
Text1
Text2
Text3
Text4
<AL> 8484848484848
Text1
Text2
Text3
Etc.
<AL> 737373737733737
Etc
Etc
Etc

等等。

批处理文件会输出一个文本文件,从下到下等等

我的问题是无论我如何编写批处理文件,我只能在不使用运算符的情况下使其工作。取代。例如,如果我删除&lt;&gt;从测试文本文件中,只需搜索并按&#34; AL&#34;然后它工作正常但是当我使用&#34;&#34;&#34;&#34;它似乎仍然看到&lt;和&gt;作为操作员甚至在报价单内。有没有办法创建一个批处理文件来搜索和拆分基于文本的文本文件与操作员的每一方?

2 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/24588489/2152082

稍作修改,它应该适合你:

@echo off
setlocal enabledelayedexpansion
set count=1
set flag=0
for /f "delims=" %%i in (t.t) do (
  set string=%%i
  if "!string:~0,4!" neq "<AL>" ( 
    set flag=0
    echo %%i>>file!count!.txt
  ) else (
    if !flag!==0 set /a count+=1 
    set flag=1
  )
)

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "destdir=U:\destdir"
SET "outfile=out"
FOR /f "tokens=1*delims=<>" %%a IN (q25152964.txt) DO (
IF /i "%%a"=="AL" (SET "outfile=%%b"
 ) ELSE (
  >>"%destdir%\!outfile!.out" ECHO %%a
 )
)

DIR "%destdir%\*.out"
TYPE "%destdir%\*.out"
GOTO :EOF

您需要更改sourcedir的设置以适合您的具体情况。 我使用了一个名为q25152964.txt的文件,其中包含我的测试数据。

结果:

 Volume in drive U has no label.
 Volume Serial Number is 0420-0000

 Directory of U:\destdir

06/08/2014  14:30                28  999939339393.out
06/08/2014  14:30                27  8484848484848.out
06/08/2014  14:30                15  737373737733737.out
               3 File(s)             70 bytes
               0 Dir(s)   2,126,184,448 bytes free
U:\destdir\ 999939339393.out

Text1
Text2
Text3
Text4

U:\destdir\ 8484848484848.out

Text1
Text2
Text3
Etc.

U:\destdir\ 737373737733737.out

Etc
Etc
Etc

不幸的是,你还没有让我们继续下去。

相关问题