Re: Converting one record to many
Posted: 17 Oct 2012 11:02
Thanks abc0502 for your help, it work's!
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
input.txt wrote:H201209110000011022E
C10001 000R000000000040000000006
X0003012100000000000102200000784L
R20120911110101ALFA123456789CTX
X0003012100000110222010209000784L
R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY
T2012091111080000DFRFGT07609530000002030
output.txt wrote:H201209110000011022E
C10001 000R000000000040000000006
X0003012100000000000102200000784L
R20120911110101ALFA123456789CTX
X0003012100000110222010209000784L
R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY
T2012091111080000DFRFGT07609530000002030
Code: Select all
set line=EOF
set /P line=
if %line:~0,3% equ EOF exit /B
if %line:~0,1% neq R (
When SET /P command read an empty line (or reach the End Of File), it does NOT modify the previous value of the variable
Code: Select all
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000
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
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?
Code: Select all
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000
RMSoares wrote:Hi,
the example taht i had inclued in my post is an export from the input file of the process
example
input:
<Header 12 characters> <Block 1 - with 99 characters> <Block 2 - with 99 characters> <Block 3 - with 99 characters>
Code: Select all
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000
RMSoares wrote:Hi,
there is only two records, the first with 806 charcaters and the second record with 1004 characters
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>
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>
Code: Select all
TXAR0023012251200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000001200000000000101800004209LT 00000002000EUR0000000000400000000021023000CT0000420900000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000256LT 00000001000EUR0000000000400000000029023000CT0000025600000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT0000025100000000000000000001200000000000101800000251LT 00000001000EUR0000000000400000000029023000CT000002510000000000000000000
TXAR0023072111200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000001200000000000101800001435LT 00000002000EUR0000000000400000000057023000CT0000143500000000000000000002100000000000102000000964LT 00000001561EUR0000000000400000000039023000CT0000096400000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000716LT 00000001000EUR0000000000400000000029023000CT0000071600000000000000000002050000000000100600000566LT 00000001001EUR0000000000400000000023023000CT0000056600000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT0000071700000000000000000001200000000000101800000717LT 00000001000EUR0000000000400000000029023000CT000007170000000000000000000
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
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