Jump to content

noob needs help to strip a file


Recommended Posts

Guest mccormackc
Posted

HI

Help needed I have a file in the format attached, i have been reading the forum and the help files, can understand how to open the file for reading, i can make a script that opens the file and writes it to another file line by line, What i would like it to do is be able to search the file for a marker like #### then read the next line and set this as a file name to be used to save the rest of the data until the script finds the next marker, Then delete what it has saved and start again till the file is empty.

Can anyone help with this little project or at the very least show me or point me the right direction.

Cheers

test.txt

Posted

Well, I'm trying this code, but it's giving me a subscript error.

Dim $test, $temp, $x = 1
$file = "C:\temp\test.txt"
$file2 = "C:\temp\test" & $temp & ".txt"

FileOpen($file, 0)

_FileReadToArray($file, $test)

;~ MsgBox(0,"",$test[6])
;~ Exit

Do
   If $test[$x] = "####" Then
      $temp = $temp + 1
      FileOpen($file2, 1)
      FileWrite($file2, $test[$x])
   EndIf
   $x = $x + 1
Until $test[$x] = "####"


Exit

I know it doesn't help much, being that it has errors, but perhaps it's a start? I'll be able to work on it more later.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Posted (edited)

Something like this may do?

$x = 1

While 1
    $ans = FileReadLine(@ScriptDir & '\test.txt', $x)
    If @error = -1 Then ExitLoop; If end of file, then exitloop
    If $ans = '####' Then; Create new filename
        $x = $x + 1
        $filename = FileReadLine('test.txt', $x)
    EndIf
    FileWriteLine($filename, $ans); Write file
    $x = $x + 1
WEnd

Edit: removed dim

Edited by MHz
Guest mccormackc
Posted

MHz

You Star thanks for this snipet, how would you go about removing the #### before writing the file.

Once again cheers for this.

Something like this may do?

$x = 1

While 1
    $ans = FileReadLine(@ScriptDir & '\test.txt', $x)
    If @error = -1 Then ExitLoop; If end of file, then exitloop
    If $ans = '####' Then; Create new filename
        $x = $x + 1
        $filename = FileReadLine('test.txt', $x)
    EndIf
    FileWriteLine($filename, $ans); Write file
    $x = $x + 1
WEnd

Edit: removed dim

<{POST_SNAPBACK}>

Posted

$x = 1

While 1
    $ans = FileReadLine(@ScriptDir & '\test.txt', $x)
    If @error = -1 Then ExitLoop; If end of file, then exitloop
    If $ans = '####' Then; Create new filename
        $x = $x + 1
        $filename = FileReadLine('test.txt', $x)
        $x = $x + 1
        ContinueLoop
    EndIf
    FileWriteLine($filename, $ans); Write file
    $x = $x + 1
WEnd

Slightly modified, should remove ####

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
×
×
  • Create New...