Sign in to follow this  
Followers 0
gazeranco

Help writing to multiple text files

6 posts in this topic

#1 ·  Posted (edited)

Hey All,

I have to find and replace some text in alot of files and I have been trying to do it by mashing together various autoit examples as tbh im not a coder :S

anyway, i am failing massively and was hoping someone could show me the error of my ways!

I basically have 500 text files in a directory. I want to in turn go through all of these files erasing the last line of text and writing in some new lines of text. i tried to use the $FILE_END with a -3 to get me where i want to start typing but it still writes the text to the end of the file??

plz help!!

thanks

#include <Constants.au3>
Local $search = FileFindFirstFile("*.spf*")
Local Const $sFile = ($search)
Local $hFile = FileOpen($sFile, 256)
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    Local $hFile = FileFindNextFile($search)
    If @error Then ExitLoop
    
  ; Check if file opened for writing OK
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

fileread($hFile)
MsgBox(0, "", "Position: " & FileGetPos($hFile))
Local $n = FileSetPos($hFile, -30, $FILE_END)
MsgBox(0, "", "Position: " & FileGetPos($hFile))
filewrite($hFile, "zzzxxx")
MsgBox(0, "", "Position: " & FileGetPos($hFile))

  
    MsgBox(4096, "File:", $hFile)

FileFlush($hFile)

WEnd

FileClose($hFile)

FileDelete($sFile)
Edited by gazeranco

Share this post


Link to post
Share on other sites



Use _FileListToArray() to get the name of files into an array and then _FileCountLines() to get the number of lines in each file (implicitly last line) and _FileWriteToLine() to overwrite the last line with what you want.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Does it count empty lines? i.e if i open the file in notepad go to the bottom and hit CR 10 times and save the file. Have I just created 10 lines?

Is will try and find examples of filelisttoarray and filcountlines, in the meantime if someone can provide some code to get me started that would be super dooper!

:oops:

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I made a text file with one line of text then hit enter a bunch of times in Notepad. _FileCountLines() returned 1. Apparently it doesn't count empty lines at the end of files. Then I added text with some empty lines between. Empty line in the middle are counted, as they should be. So it seems safe to use as suggested.

Edited by MilesAhead

Share this post


Link to post
Share on other sites

Thanks guys, this is working now utilizing the methods as suggested.

The only thing I didn't do was the array thing as I didn't understand it, the findnextfile thingy seems to be doing its job ok! countinglines lets me overwrite last line of text and away i go.

I wish a would of cam on here str8 away instead of failing for hours and hours!! lol

Super forum/site, even supererer program, thanks! :oops:

Share this post


Link to post
Share on other sites

Just to update again!

I just ran it in my folder with 586 text files expecting it to die half way through as these things never go to plan... but! The fan on my laptop made me think it was trying to do a harrier take off and 45 seconds later it was done, just done a quick check through the files and all are displaying as they should!!

:oops:

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