-1

I've been working on a batch file that will split up a text file based on recurring text

For example:

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

And so on.

The batch file would output a text file from the down to the next one etc

My issue is no matter how I write the batch file I can only get it to work without using the operators < >. For example if I remove the <> from the test text file and just search and split by "AL" then it works fine however when I use """" it seems to be still seeing < and > as an operator even inside quotations. Is there any way around this creating a batch file to search and split up a text file based on text with an operator each side?

2 Answers2

0

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

With a little modification, it should work for you:

@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
  )
)
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks this worked! Except it's not spitting out the first line with the " any ideas? I was using similar code to this linked above however it did not include the neq. Besides the first line not being included this is perfect. Thanks for your assistance – user3912552 Aug 06 '14 at 23:10
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

You would need to change the setting of sourcedir to suit your circumstances. I used a file named q25152964.txt containing your data for my testing.

Results:

 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

You haven't really given us mch to go on, unfortunately.

Magoo
  • 77,302
  • 8
  • 62
  • 84