Jump to content

purge the file and then append array


mentosan
 Share

Recommended Posts

Hi !

Here is a part of the code:

$file = FileOpen(@filename, 2);Write mode erase previous contents
FileClose($file)
$file = FileOpen(@filename, 1);Write mode append to end of file
If $line[5] = $pid Then FileWrite($file, "PID=" & $line[5] & " " & "IP=" & $line[3] & @CRLF)
FileClose($file)

The reason I'm opening the file two times is: first I want to erase the previous content, and then I'm opening the second time because I want to have all the lines written from array $line. But it will not work. :P If I'm using just option with mode 1 for FileOpen it will work but the previous content will still be there; If I'm using just FileOpen with mode 2 then only the last line from array will be displayed.

Could you point me a solution for this ? Thank you !

Edited by mentosan
Link to comment
Share on other sites

Excuse me, I don't really understand what you say. Opening the file in erase content mode and reopening it for append mode? What is the difference? You're anyway gonna write things from scratch and the file write or read (if you have the handle open) pointer is updating accordingly so you're writing on the top of the new data. Please explain your self.

Link to comment
Share on other sites

$line is array. It has values:

$line[1]=aaaaaa
$line[2]=bbbbbb
$line[3]=ccccccc
$line[4]=dddddd
$line[5]=eeeeee

If I open the file in erase content mode I get this result for FileWrite($file, line[3] & line[4] & line[5] & @CRLF) :

eeeeee
so only the last line

I want result to be:

cccccc
dddddd
eeeeee

If I open the file in append mode, after few iterations the result of the file will be lot of lines repeating, so I need to erase it before proceeding again at this step.

But I think I could erase the file before opening it again. Don't you think ?

Link to comment
Share on other sites

So it should be FileWrite($file, line[3] & @CRLF & line[4] & @CRLF & line[5] & @CRLF) and using only one call to open the file and erasing any previous content.

Maybe code explains better than I do:

Dim $sPath = @ScriptDir & '\123.txt'
Dim $DummyData = '123' & @CRLF & '122211' & @CRLF & '!_@_SSL@_$$)' & @CRLF & '___END OF FILE___'
FileWrite($sPath, $DummyData)

Dim $hFile = FileOpen($sPath, 2)
Dim $aData[5] = ['aaaa' & @CRLF, 'bbbb' & @CRLF, 'cccc' & @CRLF, _
                'dddd' & @CRLF, 'eeee' & @CRLF]

FileWrite($hFile, $aData[2] & $aData[3] & $aData[4])
FileClose($hFile)
ShellExecute('Notepad.exe', $sPath)
Edited by Authenticity
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...