Jump to content

Arrays/text files problem


Recommended Posts

Good morning to all! I've tried to find a solution through the forum posts but it seems that my problem is rare...

So here is what I'm trying to do.I have a text file called test.txt

This text file has the following format:

test
test
test
test
test
test





test2
test2
test2
test2






test3
test3
test3
test3
tesr3

So, between text there are minimum 5 and maximum 6 empty lines.

What I'm trying to do is to divide this file to 3 smaller files without the empty lines.

Since now I have the following code:

#Include <File.au3>

$file=FileOpen("test.txt",0)
;Calculate file lines
$CountLines = _FileCountLines("test.txt")
;Define that array will be 1 value longer than the lines counted before
$Max=$Countlines+1
;Define the array
Dim $VT[$Max]
;Define that the first array entry is the total lines of the file
$VT[0]=$Countlines
;Add each line to the array
for $x=1 to $Countlines

$line=FileReadLine($file,$x)
$VT[$x]=$line

Next

Any suggestions for my next step?I guess that I should define somewhere a trigger with initial value 0 and when the value

reaches 5 then it does all the stuff(close the file and create a new file).

Link to comment
Share on other sites

1) Read the whole file into an array (_FileReadToArray look it up).
2) Make a For...Next loop that iterates through the array.
3) For each pass in the loop:
   A) Make a variable holding an empty string ("").
   B) Check if the next line of the file is not empty. (StringLen)
   C) If it is not empty, then append it to your string variable. (Note: You are building each non-empty section here.)
   D) If it is empty, write the string you have built into a file.
4) DONE

Edited by omikron48
Link to comment
Share on other sites

I thought it would be easier to write the individual new files if each element of the array contained the contents of each new file.

This is a method that works.

;#include <Array.au3>

Local $sStr = StringStripWS(FileRead("test.txt"), 1) & @CRLF
;ConsoleWrite( $file & @CRLF)

Local $aFile = StringRegExp($sStr, "((?:[^\v]+\v{1,2})+\v)", 3)
;_ArrayDisplay($aFile)

For $i = 0 To UBound($aFile) - 1
    $file = FileOpen("test" & $i + 1 & ".txt", 2)
    FileWrite($file, StringStripWS($aFile[$i], 2))
    FileClose($file)
    ShellExecute("test" & $i + 1 & ".txt")
Next
Link to comment
Share on other sites

Ok, I'm here so far and it seems that till now everything is fine...

#Include <File.au3>

Dim $VT

$file=FileOpen("test.txt",0)
_FileReadToArray("test.txt",$VT)

For $x=1 to $VT[0]
    
    $v=""
    $line=filereadline($file,$x+1)
    $ln=StringLen($line)
    
    switch $ln
    
    ;problem here...
    
    
    EndSwitch
    
Next

The problem i'm facing is with the switch function.Sorry I'm just a beginner...1000 thanks for your time.

Link to comment
Share on other sites

Code is not tested. I just typed it up in notepad.

#Include <File.au3>
Opt("MustDeclareVars", 1)
Global $lines
_FileReadToArray("file.txt", $lines)
Global $count = 0
Global $section = ""
For $i = 1 To $lines[0]
    If StringLen($lines[$i]) Then
        $section &= $lines[$i] & @CRLF
    Else
        If StringLen($section) Then
            FileWrite(StringFormat("%3d", $count) & ".txt", StringTrimRight($section, 2))
            $count += 1
            $section = ""
        EndIf
    EndIf
Next
If StringLen($section) Then
    FileWrite(StringFormat("%3d", $count) & ".txt", StringTrimRight($section, 2))
EndIf
Link to comment
Share on other sites

Here is my version 2.

This script will split a text file on five or more consecutive blank lines. This allows four consecutive blank lines to exist within the divided files.

;#include <Array.au3>

Local $sStr, $aFile, $sFileName
Local $sOrigFileName = "test.txt"

;Allows for newline character of either @CR, @LF, or @CRLF. And, strips leading and trailing white spaces.
$sStr = StringStripWS(StringRegExpReplace(FileRead($sOrigFileName), @CRLF & "|" & @CR & "|" & @LF, @CRLF), 3)
;ConsoleWrite($sFileName & @CRLF)

; Allows up to 4 consecutive blank lines. Will split file on 5 or more consecutive blank lines.
$aFile = StringRegExp($sStr, "((?:[^\v]+\v{0,10})+(?:\v{12,}|$))", 3)
;_ArrayDisplay($aFile)

For $i = 0 To UBound($aFile) - 1
    ;Modify original filename by adding number e.g. Changes Filename.txt to Filename_1.txt, Filename_2.txt, etc.
    $sFileName = StringRegExpReplace($sOrigFileName, "^.*\\|\..*$", "") & "_" & $i + 1 & _
                StringRegExpReplace($sOrigFileName, "^.*\.", ".$1")
    If FileExists($sFileName) Then FileDelete($sFileName)
    FileWrite($sFileName, $aFile[$i])
    ShellExecute($sFileName) ; Use your system's default file extension application to view file.
Next

Edit: RegExp automaticly wrongly changed when posted.

Edited by Malkey
Link to comment
Share on other sites

Can you please explain to me or tell me where I can find some documentation about the expression in bold?

$aFile = StringRegExp($sStr, "((?:[^\v]+\v{0,10})+(?:\v{12,}|$))", 3)

Actually i forgot something at the beginning.The txt files are like this:

test
test
test
test
test
test
**




test2
test2
test2
test2
**





test3
test3
test3
test3
test3
**

So when the app scans ** and then 4 empty lines it divides the file...

Thanks in advance for all your time and help.

Edited by geofromalimos
Link to comment
Share on other sites

Ok, I've written some code here:

$v=0

For $i=1 to 7259
    
    $inline=filereadline("test.txt",$i)
    
    switch $inline
        case ""     
        case Else
        filewriteline("test"&$v&".txt",$inline)
        Endswitch
    consolewrite($inline&@LF)
    
    switch $inline
    case "**"
    $inline2=filereadline("test.txt",$i+1)
            switch $inline2
                case ""
                $inline3=filereadline("test.txt",$i+2)
                    switch $inline3
                    Case ""
                    consolewrite("END")
                    $v=$v+1
                case Else
                EndSwitch
            
            case Else
            Endswitch
        
    case Else
    EndSwitch   
                        
Next

It finally works fine!(Keep in mind that it also removes blank lines beetween the text but that is not a problem for me, it actually helps!)

Edited by geofromalimos
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...