Jump to content

Splitting text file


Recommended Posts

Kinda new to autoit. I have this situation where I need to take the print output from a dos program and split the pages apart so it will print properly. This is a 25+ year old program, running in DosBox. The issue is that when printing statements the next page will start on the first, and so on. I've tried different dos->windows printer type programs and none of them will split the pages apart properly. 

I had this working at one point but some changes in their system have removed that possibility. 

I believe a simple autoit script could do this. 

My thinking was something like this:

Read the text file - Easy

Split the pages - Eh 

Print the pages - 

It's been a while since I've done any coding but I can't figure out how to split the pages apart. Each page starts out "COMPANY NAME, INC". I can split around that but I lose that line or "COMPANY NAME, INC" depending on which method. 

Anyone have an idea on splitting?

I found this code

#include <File.au3>

Global $FileArray
$file = @ScriptDir & "\test.txt" ;"C:\SBT\Incoming\SBTFILE.txt"
$newfile = @ScriptDir & "\split-" ; "C:\SBT\Work\SBTFILE-"

_FileReadToArray($file, $FileArray)

$filecount = 1
$sNewFile_Text = ""

ProgressOn("Processing SBT File", "Reading The File...", "0 Lines")

For $i = 1 To $FileArray[0]

    If StringInStr($FileArray[$i], "****** END OF REPORT ******") Then
        ; We need to write the current file
        FileWrite($newfile & $filecount & ".txt", $sNewFile_Text & $FileArray[$i])
        ; And start a new one
        $filecount = $filecount + 1
        $sNewFile_Text = ""
    Else
        ; Add line to string
        $sNewFile_Text &= $FileArray[$i] & @CRLF
    EndIf

    $Percent = Int(($i / $FileArray[0]) * 100)
    ProgressSet($Percent, $Percent & " Percent Complete")

Next

ProgressSet(100, "Done", "Complete")
Sleep(1000)
ProgressOff()

And modified the END OF REPORT to match the string I am looking for but can't figure out how to make it split right before that line.  That code makes files like this

File 1:

COMPANY NAME etc

File 2 and so forth:

ADDRESS

** REST OF PAGE **

COMPANY NAME

Your help is appreciated.

 

Link to comment
Share on other sites

Does your file only contain the data lines or the output as sent to the printer?

IIRC there is a control character to tell the printer to start a new page. You could split at that character.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

COMPANY NAME, INC                                    DIL38          1
      XXX XXXXX
      XXX, XX XXXXX
      (XXX)XXX-XXX
                                                                03-24-15




      CUSTOMER NAME
      XXX XXXXX
      XXX, XX XXXXX



DATE      DESCRIPTION                      INVOICE        CHARGES       CREDITS


02-13-15  CHARGE                           59814            11.36
          Invoice Total                                     11.36
01-28-15  CHARGE                           60048           547.12
          Invoice Total                                    547.12
01-29-15  CHARGE                           60053           476.08
          Invoice Total                                    476.08
01-30-15  CHARGE                           60060            57.72
          Invoice Total                                     57.72
02-02-15  CHARGE                           60062           321.24
          Invoice Total                                    321.24
01-23-15  CHARGE                           60111          1398.08
          Invoice Total                                   1398.08
01-16-15  CHARGE                           60133          1299.52
03-03-15  5504120                          60133                       -1299.52
          Invoice Total                                                    0.00
01-20-15  CHARGE                           60151           197.84
          Invoice Total                                    197.84
01-21-15  CHARGE                           60157           511.76
          Invoice Total                                    511.76
01-22-15  CHARGE                           60162           349.60
          Invoice Total                                    349.60
01-26-15  CHARGE                           60167            83.92
          Invoice Total                                     83.92
01-26-15  CHARGE                           60169           147.68
          Invoice Total                                    147.68
01-27-15  CHARGE                           60172           170.40
          Invoice Total                                    170.40
02-04-15  CHARGE                           60178           376.40
          Invoice Total                                    376.40
02-03-15  CHARGE                           60192           214.40
          Invoice Total                                    214.40
01-30-15  CHARGE                           60206          1206.08
          Invoice Total                                   1206.08
02-05-15  CHARGE                           60226           306.00
          Invoice Total                                    306.00
02-09-15  CHARGE                           60232           101.52
          Invoice Total                                    101.52
02-06-15  CHARGE                           60268          1233.28




      COMPANY NAME, INC                                    DIL38          2
      XXX XXXXX
      XXX, XX XXXXX
      (XXX)XXX-XXX
                                                                03-24-15




      CUSTOMER NAME
      XXX XXXXX
      XXX, XX XXXXX



DATE      DESCRIPTION                      INVOICE        CHARGES       CREDITS


          (Continued)
          Invoice Total                                   1233.28
02-11-15  CHARGE                           60278           328.16
          Invoice Total                                    328.16
02-12-15  CHARGE                           60284            44.32
          Invoice Total                                     44.32
02-13-15  CHARGE                           60309          1117.20
          Invoice Total                                   1117.20
02-20-15  CHARGE                           60312          1293.28
          Invoice Total                                   1293.28
02-17-15  CHARGE                           60320            86.68
          Invoice Total                                     86.68
02-18-15  CHARGE                           60327           347.88
          Invoice Total                                    347.88
02-26-15  CHARGE                           60364          1293.60
          Invoice Total                                   1293.60
02-19-15  CHARGE                           60372           203.20
          Invoice Total                                    203.20
02-20-15  CHARGE                           60377            79.32
          Invoice Total                                     79.32
02-25-15  CHARGE                           60380           178.40
          Invoice Total                                    178.40
02-25-15  CHARGE                           60384           173.68
          Invoice Total                                    173.68
03-03-15  CHARGE                           60389           240.92
          Invoice Total                                    240.92
02-26-15  CHARGE                           60393           170.12
          Invoice Total                                    170.12
03-02-15  CHARGE                           60396           275.84
          Invoice Total                                    275.84
03-04-15  CHARGE                           60407           476.80
          Invoice Total                                    476.80
01-13-15  CHARGE                           61460            85.76
03-03-15  5504120                          61460                         -85.76
          Invoice Total                                                    0.00
01-14-15  CHARGE                           61465            76.44
03-03-15  5504120                          61465                         -76.44
          (Continued)

Example of first two pages.  Each page starts with their company name, acct #, page #.

Link to comment
Share on other sites

#include<File.au3>

Global $aFileArray

_FileReadToArray("history.txt", $aFileArray)

$aStart = _ArrayFindAll($aFileArray , " COMPANY NAME, INC" , '' , '' , '' , 1)

for $i = 0 to ubound($aStart) - 1
    If $i = ubound($aStart) - 1 Then
        $sOut = _ArrayToString($aFileArray , "" , $aStart[$i] , _FileCountLines("history.txt"))
    Else
        $next = $i + 1
        $sOut = _ArrayToString($aFileArray , "" , $aStart[$i] , $aStart[$next] - 1)
    EndIf

    filewrite("OUTPUT_" & $i & ".txt", $sOut)
next

**edit, many posting formatting issues, hopefully its all better

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

#include<File.au3>

Global $aFileArray

_FileReadToArray("history.txt", $aFileArray)

$aStart = _ArrayFindAll($aFileArray , " COMPANY NAME, INC" , '' , '' , '' , 1)

for $i = 0 to ubound($aStart) - 1
    If $i = ubound($aStart) - 1 Then
        $sOut = _ArrayToString($aFileArray , "" , $aStart[$i] , _FileCountLines("history.txt"))
    Else
        $next = $i + 1
        $sOut = _ArrayToString($aFileArray , "" , $aStart[$i] , $aStart[$next] - 1)
    EndIf

    filewrite("OUTPUT_" & $i & ".txt", $sOut)
next

When I run that it doesn't appear to do anything. No errors no OUTPUT_* files. 

Edit saw your revision.

So that works but it removes all the whitepace from the file. 

Edited by mikeroq
Link to comment
Share on other sites

I suspect that the company name changes, which means that you might want to read the file to array:

_FileReadToArray

Then loop through the array until you find a line that has "charge" in it, with the following line being empty...that's your signifier that you are going to start the next company in a few lines.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

did you name it history.txt, or change those file names in my script?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

 

I suspect that the company name changes, which means that you might want to read the file to array:

_FileReadToArray

Then loop through the array until you find a line that has "charge" in it, with the following line being empty...that's your signifier that you are going to start the next company in a few lines.

 

All pages start with the same company name. 

did you name it history.txt, or change those file names in my script?

 

 

I was using your first code. It works now but removes the line breaks from the file.

COMPANY NAME, INC                              DIL38          1      xxxxxx     xxxxx     xxxxx                                                              03-24-15      xxx      xxx      xxx     DESCRIPTION                      INVOICE        CHARGES       CREDITS02-13-15  CHARGE                           59814            11.36          Invoice Total                                     11.3601-28-15  CHARGE                           60048           547.12          Invoice Total                                    547.1201-29-15  CHARGE                           60053           476.08          Invoice Total                                    476.0801-30-15  CHARGE                           60060            57.72          Invoice Total                                     57.7202-02-15  CHARGE                           60062           321.24          Invoice Total                                    321.2401-23-15  CHARGE                           60111          1398.08          Invoice Total                                   1398.0801-16-15  CHARGE                           60133          1299.5203-03-15  5504120                          60133                       -1299.52          Invoice Total                                                    0.0001-20-15  CHARGE                           60151           197.84          Invoice Total                                    197.8401-21-15  CHARGE                           60157           511.76          Invoice Total                                    511.7601-22-15  CHARGE                           60162           349.60          Invoice Total                                    349.6001-26-15  CHARGE                           60167            83.92          Invoice Total                                     83.9201-26-15  CHARGE                           60169           147.68          Invoice Total                                    147.6801-27-15  CHARGE                           60172           170.40          Invoice Total                                    170.4002-04-15  CHARGE                           60178           376.40          Invoice Total                                    376.4002-03-15  CHARGE                           60192           214.40          Invoice Total                                    214.4001-30-15  CHARGE                           60206          1206.08          Invoice Total                                   1206.0802-05-15  CHARGE                           60226           306.00          Invoice Total                                    306.0002-09-15  CHARGE                           60232           101.52          Invoice Total                                    101.5202-06-15  CHARGE                           60268          1233.28
Edited by mikeroq
Link to comment
Share on other sites

fine, with existing formatting.

#include<File.au3>

Global $aFileArray

_FileReadToArray("history.txt", $aFileArray)

$aStart = _ArrayFindAll($aFileArray , " COMPANY NAME, INC" , '' , '' , '' , 1)

for $i = 0 to ubound($aStart) - 1
    If $i = ubound($aStart) - 1 Then
        _FileWriteFromArray("OUTPUT_" & $i & ".txt" , $aFileArray , $aStart[$i] , $aFileArray[0])
    Else
        $next = $i + 1
        _FileWriteFromArray("OUTPUT_" & $i & ".txt" , $aFileArray , $aStart[$i] , $aStart[$next] - 1)
    EndIf
next
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

mikeroq,

This looks like it was designed to output x number of lines. The device would have been setup to generate a pagefeed

after the corresponding number of lines.

So the real question is what do you want to use to print it now?

kylomas

edit: And do you really want to output a file for each page?

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

kylomas, 

I can use a program to monitor a folder and print whatever file is created there. Not ideal but It works.

I'm going to try to figure out how to script all this on my own. So I'll prob ditch the file creation and print from autoit.

The problem is I've tried multiple programs, even bought one called Printfil they work to an extent. (had issues with the text being too big and parts of the page missing. But none of them will split the page. Even though I see options on these programs that specify # of lines, "auto page" etc. 

Link to comment
Share on other sites

I see, boththose has shown you how you might split the file.

Look at a multiple page output file and check if a new page occurs every 60 lines.  Some of the really old crap just printed line by line and paging was controlled at the device.

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

$sFile = FileRead("thefile.txt")

$aFile = StringSplit($sFile, "      COMPANY NAME, INC                                    DIL38          ", 3)

For $i = 1 To UBound($aFile) - 1
    ConsoleWrite("      COMPANY NAME, INC                                    DIL38          " & $aFile[$i] & @LF)
Next

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Does your file only contain the data lines or the output as sent to the printer?

IIRC there is a control character to tell the printer to start a new page. You could split at that character.

 

My thoughts exactly. If the files read contain the output as meant to be sent to the printer, they are likely to contain a formfeed character. In the StringRegExp help:

f Represents "formfeed" (Chr(12)).

 

If it's in there, you're lucky, just split on that. Open the file in something like Notepad++ (with the "Show All Characters" mode active, Notepad++ shows a form feed as a black box with "FF" in it).

It's an oldschool way to instruct a printer, but 25 years ago this was a widely used control character to make a printer feed through paper until there was no paper left. Some printers weren't safeguarded with a maximum form-feeding time (like the ones at our school) and therefor it was NOT funny to do this to a printer that was loaded with "continuous form paper". At least, our teacher didn't think it was funny. Ah, good old times.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

I checked, no form feed. Just line feeds and carriage returns. If the form feed was there I wouldn't be here :P since the dos printing programs would pick that up and split the page. 

Seems like its doing 60 lines per page on this report. 

My thoughts would be to replace the dos printing helper program and replace it with an autoit that would:

Monitor the file c:printprint.txt

Wait for the program to finish printing 

Split the pages apart

Send each page to the printer

Delete the print.txt file

Edited by mikeroq
Link to comment
Share on other sites

My crude code so far:

#include <File.au3>

Global $aFileArray


Func _FileWriteLine_top($file, $txt)
  $txt &= @CRLF & FileRead($file)
  $fh = FileOpen($file, 2)
  If $fh = -1 Then Return SetError(1, 0, 0)
  Local $iWriteFile = FileWriteLine($fh, $txt)
  Local $iRet = FileClose($fh)
  If $iWriteFile = -1 Then Return SetError(2, $iRet, 0)
  Return $iRet
EndFunc
_FileReadToArray("print.txt", $aFileArray)

$aStart = _ArrayFindAll($aFileArray , "COMPANY NAME REDACTED" , '' , '' , '' , 1)

for $i = 0 to ubound($aStart) - 1
    If $i = ubound($aStart) - 1 Then
        _FileWriteFromArray("OUTPUT_" & $i & ".txt" , $aFileArray , $aStart[$i] , $aFileArray[0])
        _FileWriteLine_top("OUTPUT_" & $i & ".txt", @CRLF & @CRLF)
        _FilePrint("OUTPUT_" & $i & ".txt")
         Sleep(500)
         FileDelete("OUTPUT_" & $i & ".txt")
    Else
        $next = $i + 1
        _FileWriteFromArray("OUTPUT_" & $i & ".txt" , $aFileArray , $aStart[$i] , $aStart[$next] - 1)
        _FileWriteLine_top("OUTPUT_" & $i & ".txt", @CRLF & @CRLF)
        _FilePrint("OUTPUT_" & $i & ".txt")
        Sleep(500)
        FileDelete("OUTPUT_" & $i & ".txt")
    EndIf
next

I installed a virtual printer which lets me view what the printed page would look like and it is working the way I want it. Now I need to monitor the print file!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...