Converting one record to many

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Converting one record to many

#31 Post by foxidrive » 19 Sep 2014 03:15

RMSoares wrote: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.


You are implying that the line length can be any figure, and plain batch files just don't work with any figure.

From XP and on 8191 is the limit and that includes the command and all associated characters.

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

Re: Converting one record to many

#32 Post by RMSoares » 19 Sep 2014 04:13

foxidrive wrote:
RMSoares wrote: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.


You are implying that the line length can be any figure, and plain batch files just don't work with any figure.

From XP and on 8191 is the limit and that includes the command and all associated characters.



Do you have any sugestion ?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Converting one record to many

#33 Post by penpen » 20 Sep 2014 03:32

I've seen a "trick" to seperate the single characters of a file: http://www.dostips.com/forum/viewtopic.php?f=3&t=5050.
But i have no experience, if that is really reliable: I think there are some poisonous character combinations, so you better check the result.

In addition the target file MUST have lines of limited length (don't know anymore where the limit is).

Then you could do something like this:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "input=09_24_32_003_IN_RMS.txt"
set "output=09_24_32_003_Output.txt"

:: txar == -1 :<==> detect header
:: txar ==  0 :<==> detected header is no TXAR line
:: txar ==  1 :<==> detected header is a TXAR line
set "txar=-1"
set "header="
set "buffer="

>"%output%" (
   for /F "tokens=1* delims=:" %%a in ('cmd /d /u /c type "%input%" ^| find /v "" ^| findstr /N "^"') do (
      set "buffer=!buffer!%%~b"

      if "%%~b" == "" (
         if defined buffer (
            if "!txar!" == "1" (
               echo(!header!!buffer!
               echo(
               >&2 echo(warning unfinished entry: "!header!!buffer!"
            ) else (
               rem no TXAR line: just buffered copy
               echo(!buffer!
            )
         )
         set "txar=-1"
         set "header="
         set "buffer="
      ) else if "!txar!" == "-1" (
         rem header not fully read: autodetect header (TXAR line has 13 characters)
         set "check=!buffer:~12!"
         if defined check (
            set "check=!buffer:~0,4!"
            if "!check!" == "TXAR" (
               set "header=!buffer!"
               set "buffer="
               set txar=1
            )
         )
      ) else if "!txar!" == "1" (
         rem TXAR lines
         set "check=!buffer:~98!"
         if defined check (
            echo(!header!!buffer:~0,99!
            set "buffer=!buffer:~99!"
         )
      )
   )
)

endlocal

If the "trick" above is not reliable, you have to dump the file to hex, maybe using: http://www.dostips.com/forum/viewtopic.php?t=1786.
Then you have to do the same on the hex data, as above.
Finally you have to reconvert the hex data to binary data:
I'm sure i've seen a tool here on dostips which can do this, but i don't know it's name anymore - so i cannot provide a link.

penpen

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

Re: Converting one record to many

#34 Post by Aacini » 20 Sep 2014 12:04

The Batch file below is a Batch-JScript hybrid script that should solve your problem. The JScript section is a (more or less) direct translation of your original Batch code, but JScript have no problems reading very long lines:

Code: Select all

@if (@CodeSection == @Batch) @then


@echo off

set input=C:\tmp\09_24_32_003_IN_RMS.txt
set output=C:\tmp\09_24_32_003_Output.dat

cscript //nologo //E:JScript "%~F0" < "%input%" > "%output%"
goto :EOF


@end


while ( ! WScript.Stdin.AtEndOfStream ) {
   var line = WScript.Stdin.ReadLine();
   if ( line.substr(0,4) == "TXAR"  &&  line.length > 112 ) {
      for ( var b = 13 ; b < line.length ; b += 99 ) WScript.Stdout.WriteLine( line.substr(0,13)+line.substr(b,98)+line.slice(-1) );
   } else {
      WScript.Stdout.WriteLine(line);
   }
}

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

For further details, see JScript Reference.

Antonio

Post Reply