Converting one record to many

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Converting one record to many

#16 Post by RMSoares » 17 Oct 2012 11:02

Thanks abc0502 for your help, it work's!

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Converting one record to many

#17 Post by Aacini » 26 Oct 2012 23:07

This is a different (clearer, I think) method:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
call :ProcessFile < input.txt > output.txt
goto :EOF

:ProcessFile
set line=EOF
set /P line=
if %line:~0,3% equ EOF exit /B
if %line:~0,1% neq R (
   echo %line%
) else (
   set R-record=%line:~0,15%
   set moves=!R-record:~-2!
   if "!moves:~0,1!" equ "0" set moves=!moves:~1!
   rem Store each movement in an array
   set type=%line:~15%
   for /L %%i in (1,1,!moves!) do (
      set movement[%%i]=!type:~0,15!
      set type=!type:~15!
   )
   rem Output each movement
   for /L %%i in (1,1,!moves!) do (
      echo !R-record!!movement[%%i]!!type!
   )
)
:endif
goto ProcessFile

Previous Batch file assume that:

- There is not empty lines in the input file
- Each R-type record is correctly formatted, with no spaces

Maximum input line length for SET /P command is 1024 characters; this means that the maximum number of movements in R-type records is 67 (this can not be fixed).
input.txt wrote:H201209110000011022E
C10001 000R000000000040000000006
X0003012100000000000102200000784L
R20120911110101ALFA123456789CTX
X0003012100000110222010209000784L
R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY
T2012091111080000DFRFGT07609530000002030
output.txt wrote:H201209110000011022E
C10001 000R000000000040000000006
X0003012100000000000102200000784L
R20120911110101ALFA123456789CTX
X0003012100000110222010209000784L
R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY
T2012091111080000DFRFGT07609530000002030

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Converting one record to many

#18 Post by abc0502 » 27 Oct 2012 00:44

That is neat, :)
But I don't understand this:

Code: Select all

set line=EOF
set /P line=
if %line:~0,3% equ EOF exit /B
if %line:~0,1% neq R (

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Converting one record to many

#19 Post by Aacini » 27 Oct 2012 01:25

When SET /P command read an empty line (or reach the End Of File), it does NOT modify the previous value of the variable, so the way to detect these situations is by previously load a value that can not exist in the file (I used "EOF" string in this solution).

This is the reason because the file can not contain empty lines (although this limitation may be fixed).

Antonio

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Converting one record to many

#20 Post by abc0502 » 28 Oct 2012 05:54

When SET /P command read an empty line (or reach the End Of File), it does NOT modify the previous value of the variable

@Aacini, thanks for the clarification, that is a new info to me

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Converting one record to many

#21 Post by RMSoares » 18 Sep 2014 06:14

I am using a batch to convert a record in several records. The logic will always be to find a certain type of record ("TXAR") create several records, as many as the number of blocks of 99 existing carcateres the register.
Since the final output for each record must contain the first 13 characters of the records and then the bloc of 99 carcateres.

example
input:
<Header 12 characters> <Block 1 - with 99 characters> <Block 2 - with 99 characters> <Block 3 - with 99 characters>

output:
<Header 12 characters> <Block 1 - with 99 characters>
<Header 12 characters> <Block 2 - with 99 characters>
<Header 12 characters> <Block 3 - with 99 characters>

however for records with more than 1000 characters is not working properly, it didn't convert the record with more than 999 characters
any idea what could be the problem?

An example of the source file

Code: Select all

TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000


The source code that in using int he batch file

Code: Select all


set input=C:\tmp\09_24_32_003_IN_RMS.txt
set output=C:\tmp\09_24_32_003_Output.dat
del "%output%"
touch "%output%"

Setlocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in ('Type "%input%"') Do (
   set "line=%%A"
   IF "!line:~0,4!" NEQ "TXAR" ( Echo !line!>>"%output%"
   ) Else (
      IF "!line:~0,4!" EQU "TXAR" (
         Call :strLen "!line!" "len_line"
         IF "!len_line!" GTR "112" (
            set /a end = len_line - 99
            for /l %%b in (13,99,!end!) Do Echo !line:~0,13!!line:~%%b,98!!line:~-1!>>"%output%"
            ) Else ( Echo !line!>>"%output%" )
      )
   )

)
Exit /B

:strLen string len --
:$created 20140302 :$changed 20140312 :$categories StringOperation
(
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A%~1"
set "len=0"
for /L %%A in (13,-1,0) do (
    set /a "len|=1<<%%A"
    for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
(
ENDLOCAL
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Converting one record to many

#22 Post by Squashman » 18 Sep 2014 06:50

RMSoares wrote:however for records with more than 1000 characters is not working properly, it didn't convert the record with more than 999 characters
any idea what could be the problem?

How many times in this thread were you told that BATCH files do have line length limitations!

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Converting one record to many

#23 Post by RMSoares » 18 Sep 2014 07:12

There is any why to resolved this limitation of 1024 characters ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Converting one record to many

#24 Post by foxidrive » 18 Sep 2014 08:09

Do you have an authentic file to test with (say on dropbox) and information about how it is to be processed?

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Converting one record to many

#25 Post by RMSoares » 18 Sep 2014 11:22

Hi,
the example taht i had inclued in my post is an export from the input file of the process

Code: Select all

TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000


or you can get it in https://dl.dropboxusercontent.com/u/20579791/09_24_32_003_IN_RMS.txt

thanks in advance four your help

regards

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Converting one record to many

#26 Post by foxidrive » 18 Sep 2014 11:31

RMSoares wrote:Hi,
the example taht i had inclued in my post is an export from the input file of the process


You said this was the format

example
input:
<Header 12 characters> <Block 1 - with 99 characters> <Block 2 - with 99 characters> <Block 3 - with 99 characters>


but what you are showing us has 100 characters on line 1 and fewer on the next lines.
How do you explain that?

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Converting one record to many

#27 Post by RMSoares » 18 Sep 2014 11:40

Hi,
there is only two records, the first with 806 charcaters and the second record with 1004 characters


Code: Select all

TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Converting one record to many

#28 Post by Squashman » 18 Sep 2014 11:47

Foxidrive, the forum is wrapping the lines. Download the text file. But you are still correct. The original explanation of the data format is different then the latest data format example.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Converting one record to many

#29 Post by foxidrive » 18 Sep 2014 12:32

RMSoares wrote:Hi,
there is only two records, the first with 806 charcaters and the second record with 1004 characters


ok, and thanks Squashman.

RMSoares please show us the output file that you need, from the input file you have shown.

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Converting one record to many

#30 Post by RMSoares » 19 Sep 2014 01:23

Good Morning,
the formatting of the file has been changed, is currently fomatting is that indicated

Code: Select all

<Header 12 characters> <Block 1 - with 99 characters> <Block 2 - with 99 characters> <Block 3 - 99 characters with> ... <Block n - with 99 characters> 


It is not possible to indicate the number of blocks that each record will contain or indicate what the maximum number of blocks per file.

Based on the format indicated, the format of the output will be as follows.

Code: Select all

<Header 12 characters> <Block 1 - with 99 characters> 
<Header 12 characters> <Block 2 - with 99 characters>
<Header 12 characters> <Block 3 - with 99 characters>
  ...
<Header 12 characters> <Block n - with 99 characters>


Based on the example, two records from the input file process

Code: Select all

TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000


is expected the following output

Code: Select all

TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT000042090000000000000000000
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT000042090000000000000000000
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT000042090000000000000000000
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT000042090000000000000000000
TXAR0023012252100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT000009640000000000000000000
TXAR0023012251200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT000002560000000000000000000
TXAR0023012251200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023012251200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT000014350000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT000014350000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT000014350000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT000014350000000000000000000
TXAR0023072112100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT000009640000000000000000000
TXAR0023072111200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000
TXAR0023072111200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT000007160000000000000000000
TXAR0023072112050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT000005660000000000000000000
TXAR0023072111200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000
TXAR0023072111200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000


with the current process already can correctly process correctly the 1º records,

Code: Select all

set input=C:\tmp\09_24_32_003_IN_RMS.txt
set output=C:\tmp\09_24_32_003_Output.dat
del "%output%"
touch "%output%"

Setlocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in ('Type "%input%"') Do (
   set "line=%%A"
   IF "!line:~0,4!" NEQ "TXAR" ( Echo !line!>>"%output%"
   ) Else (
      IF "!line:~0,4!" EQU "TXAR" (
         Call :strLen "!line!" "len_line"
         IF "!len_line!" GTR "112" (
            set /a end = len_line - 99
            for /l %%b in (13,99,!end!) Do Echo !line:~0,13!!line:~%%b,98!!line:~-1!>>"%output%"
            ) Else ( Echo !line!>>"%output%" )
      )
   )

)
Exit /B

:strLen string len --
:$created 20140302 :$changed 20140312 :$categories StringOperation
(
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A%~1"
set "len=0"
for /L %%A in (13,-1,0) do (
    set /a "len|=1<<%%A"
    for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
(
ENDLOCAL
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b


but in the case of the 2nd records is not processed correctly. It pass the full record, with any transformation to the output

Any idea how I can process the records with higher dimension?

Thank you for your help

Post Reply