Jump to content

Recommended Posts

Posted

Hey All,

I am still stuck with a problem here, the code I am using successfully writes a "3" to the last line in a file. The bit that is not working is the readto array and write from array. It doesn't give me an error when I run it but when I open the file. The contents of the text.txt file has not been written to the .spf file.

Can anyone see from the code why this isn't working??

Thanks,

#include <File.au3>
; Open the text file and read it ready for writing.

; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
Local $answer = MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?")


; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
    Exit
    MsgBox(0, "SPF-Editor", "Buh Bye!")
EndIf

MsgBox(0, "Running", "The program will prompt you when it has finished!")

Local $filer = FileOpen("text.txt", 0)

; Check if file opened for reading OK
If $filer = -1 Then
    MsgBox(0, "Error", "Unable to open file!!")
    Exit
EndIf

; Start finding files for writing.
; Shows the filenames of all files in the current directory
Local $search = FileFindFirstFile("*.spf*")

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "Eeeeeroar!!")
    Exit
EndIf
Global $Text = "", $Array
$line = FileRead($filer) ; reads the entire file at one time

_FileReadToArray($filer, $Array)

While 1
    Local $file = FileFindNextFile($search)
    If @error Then ExitLoop

    Local $CountLines = _FileCountLines($file)
    _FileWriteToLine($file, ($CountLines), "3", 1)

    _FileWriteFromArray($file, $Array) ; writes $$Array to $File

WEnd
MsgBox(0, "Done", "The script has stopped and your files are edited!")

; Close the search handle mofo
FileClose($filer)

FileClose($search)
Posted (edited)

Specifically, this is the area where I think I am missing something...

Global $Text = "", $Array
$line = FileRead($filer) ; reads the entire file at one time

_FileReadToArray($filer, $Array)

While 1
    Local $file = FileFindNextFile($search)
    If @error Then ExitLoop

    Local $CountLines = _FileCountLines($file)
    _FileWriteToLine($file, ($CountLines), "3", 1)

    _FileWriteFromArray($file, $Array) ; writes $$Array to $File

WEnd
Edited by gazeranco
  • Moderators
Posted

gazeranco,

Your problem is here:

$line = FileRead($filer) ; reads the entire file at one time

_FileReadToArray($filer, $Array)

Go and look at the parameters you nee to use for _FileReadToArray - you need the filename, not the file content. ;)

And here:

_FileWriteToLine($file, ($CountLines), "3", 1)

_FileWriteFromArray($file, $Array) ; writes $$Array to $File

You are correctly changing the last tine with _FileWriteToLine, but then immediately overwriting it with the non-existent array. :doh:

And you are yet another one who uses the cumbersome FileFindFirst/NextFile pair when it is so much easier to use _FileListToArray to do all the work for you. :oops:

Try this script which works for me to convert all of the final lines of the *.spf* files to "3":

#include <File.au3>

; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
Local $answer = MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?")


; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?") = 7 Then
    MsgBox(0, "SPF-Editor", "Buh Bye!")
        Exit
EndIf

MsgBox(0, "Running", "The program will prompt you when it has finished!")

Local $filer = FileOpen("text.txt")

; Check if file opened for reading OK
If $filer = -1 Then
    MsgBox(0, "Error", "Unable to open file!!")
    Exit
EndIf

; Start finding files for writing.
; Shows the filenames of all files in the current directory
Local $aList = _FileListToArray(@ScriptDir, "*.spf*", 1)

; Check if the search was successful
If Not IsArray($aList) Then
    MsgBox(0, "Error", "Eeeeeroar!!")
    Exit
EndIf

; Now loop through the files
For $i = 1 To $aList[0]
    ; Get line count
    $iCount = _FileCountLines(@ScriptDir & "" & $aList[$i])
    ; And change the final line
    _FileWriteToLine(@ScriptDir & "" & $aList[$i], $iCount, "3", 1)
Next

; No idea why you opened it - but you need to close it sometime
FileClose($filer)

Please ask if you have any questions. :bye:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted (edited)

Hey,

That's a bit complex for me, I will try and understand it!!

It does what I want, it replaces the last line with "3" now I just need it to read from text.txt and write from text.txt to the current file. I don't understand the _writefromarray command though, it's a little confusing for me at least... text.txt currently just contains...

LINE1
LINE2
LINE3
LINE4
LINE5
-1

I will try and let you know how I get on. Thanks for the help!! :oops:

Edited by gazeranco
  • Moderators
Posted

gazeranco,

I just need it to read from text.txt and write from text.txt to the current file

It is always better to explain exactly what you want in the beginning - saves a lot of time and wasted effort from those who try to help. :bye:

Are you saying that you want to add the contents of the .txt file to the end of each *.spf* file? If so then you can use FileWrite directly. :doh:

#include <File.au3>

If MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?") = 7 Then
    MsgBox(0, "SPF-Editor", "Buh Bye!")
        Exit
EndIf

MsgBox(0, "Running", "The program will prompt you when it has finished!")

; Read the text you want to add
Local $sText = FileRead("text.txt")

; Check if file opened for reading OK
If $sText = "" Then
    MsgBox(0, "Error", "Unable to open file!!")
    Exit
EndIf

; Start finding files for writing.
; List the filenames of all *.spf* files in the current directory
Local $aList = _FileListToArray(@ScriptDir, "*.spf*", 1)

; Check if the search was successful
If Not IsArray($aList) Then
    MsgBox(0, "Error", "Eeeeeroar!!")
    Exit
EndIf

; Now loop through the files
For $i = 1 To $aList[0]
    ; Open the .spf file for appending data - parameter = 1
    $hFile = FileOpen(@ScriptDir & "" & $aList[$i], 1)
    ; Add the data
    FileWrite($hFile, $sText)
    ; And close it
    FileClose($hFile)
Next

Again ask if you have any questions. :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

How is this thread any different from your You will need to include a copy of the .spf file so we can figure out EXACTLY what's in that file to replace.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

Brewman,

different in that last time the loop confused me, then we digressed and you solved most of my problems.

But this time specifically, I didn't understand what I was doing with the readarray/writearray and couldn't get it working, as the last title was loop in a loop i decided a new post was appropriate.

Melba,

Thats excellent, seems to work perfectly for my files. I will try and understand it all and possibly fire a few questions at you if I don't understand anything!

Cheers,

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...