Sign in to follow this  
Followers 0
mentosan

purge the file and then append array

6 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



But in this way I will get only one line (the last line) from array variable

Share this post


Link to post
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.

Share this post


Link to post
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 ?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0