Help with Reading A Fixed Length Record Flat File with a Batch Script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RAYLAR67
Posts: 1
Joined: 09 Oct 2017 21:07

Help with Reading A Fixed Length Record Flat File with a Batch Script

#1 Post by RAYLAR67 » 09 Oct 2017 21:20

I have been reviewing posts on the internet. As well as this forum. But I still don't understand how to create a batch command file that will:

1. Read a Flat-File created by a main-frame system. Approx rec-size is 275 bytes in each record.
2. Bypass the first record, because its not needed by the target system. If one can define the 1st 2 positions in the file to be a record code, that can be used to bypass this header.
3. Need to bypass the last record on the file (as it is blank, and not needed).
4. After that, this is basically a file re-write. Keeping the same record length, and EOL char on each record in the output file.

I have looked at examples of the FOR stmt. But this seems to be reading the whole file as one chunk, into the variable. How do I parse the FOR stmt into reading this file on the logical file markers based upon the fixe length of each record?

Appreciative of any help someone can afford an old main-frame programmer..

Larry

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with Reading A Fixed Length Record Flat File with a Batch Script

#2 Post by dbenham » 09 Oct 2017 21:53

Native batch has no capability to read records based solely on length. The only thing batch can do is read one line at a time, where each line must be terminated by a newline (\n), or carriage return/newline (\r\n)

If your records are not terminated by a newline, then you will have to use some other scripting language such as VBScript, JScript, or PowerShell. Or you could possibly use some 3rd party utility within a batch script.


Dave Benham

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

Re: Help with Reading A Fixed Length Record Flat File with a Batch Script

#3 Post by Aacini » 10 Oct 2017 07:33

This is an example of how to read fixed-length records from a text file:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set chunkSize=18

set "input="
call :ReadFile < input.txt
goto :EOF

:ReadFile
call :ReadChunk %chunkSize%
if not defined chunk goto :EOF
echo %chunk%
goto ReadFile

:ReadChunk
if not defined input set /P "input="
if not defined input set "chunk=" & exit /B
set /A sizeM1=%1-1
if "!input:~%sizeM1%!" equ "" (
   set "buffer="
   set /P "buffer="
   set "input=!input!!buffer!"
)
set "chunk=!input:~0,%1!"
set "input=!input:~%1!"
exit /B

This general method may be improved in several ways.

input.txt:

Code: Select all

This is line one  This is line two  This is line threeThis is line four 

output.txt:

Code: Select all

This is line one  
This is line two 
This is line three
This is line four

Antonio

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

Re: Help with Reading A Fixed Length Record Flat File with a Batch Script

#4 Post by Squashman » 10 Oct 2017 11:21

I work on a mainframe every day and I am really not understanding what your file looks like coming from the mainframe. Too me it sounds like you are working with a variable block file.

Post Reply