Jump to content
Sign in to follow this  
whitts

Array

Recommended Posts

whitts

How do i get

_FileWriteFromArray("MyFile.txt",$Saved)

To append the text to the end of an already created file. The file myfile.txt already has text in it and i dont want that overwritten i just want the text in the array to be written after the original text..

Thanks

Share this post


Link to post
Share on other sites
whitts

I just tried it the way you told me to - whee am i going wrong... Heres my code

CODE
While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Save

_ArrayAdd($Saved,'ECHO.[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports]>>"printer.reg"')

_ArrayAdd($Saved,'ECHO."\\\\' &GUICtrlRead($Input1) & "\\" &GUICtrlRead($Input2)& '"="">"printer.reg">>"printer.reg"')

GUICtrlSetData($Input1,"")

GUICtrlSetData($Input2,"")

GUICtrlSetData($Input3,"")

Case $Write

For $i=1 to $Saved[0]

FileWriteLine ("myfile.txt", $Saved[$i])

Next

EndSwitch

WEnd

Share this post


Link to post
Share on other sites
Das Ami

Could you post the entire code or the error message you're getting please?

Share this post


Link to post
Share on other sites
PsaltyDS

How do i get

_FileWriteFromArray("MyFile.txt",$Saved)

To append the text to the end of an already created file. The file myfile.txt already has text in it and i dont want that overwritten i just want the text in the array to be written after the original text..

Thanks

Unfortunately _FileWriteFromArray() doesn't do that. But I think it should accept a file handle in place of the string file path, which would allow you to open the file beforehand in any mode you wanted, include Unicode vice ANSI. This demonstrates the change to _FileWriteFromArray(), with the function named changed by adding an extra underscore to __FileWriteFromArray():

#include <file.au3>

Dim $avArray, $sSrc = "C:\Temp\Test1.txt", $sDest = "C:\Temp\Test2.txt" 
_FileReadToArray($sSrc, $avArray)

$hDest = FileOpen($sDest, 1) ; 1 = Append
; ConsoleWrite("Debug: $hDest = " & VarGetType($hDest) & @LF) ; file handle type = int
__FileWriteFromArray($hDest, $avArray, 1)
FileClose($hDest)

Run("notepad.exe " & $sDest)

;===============================================================================
;
; Description:      Write array to File.
; Syntax:           __FileWriteFromArray( $sFilePath, $aArray )
; Parameter(s):     $sFilePath - Path and filename of the file to be written, or file handle
;                   $a_Array   - The array to retrieve the contents
;                   $i_Base    - Start reading at this Array entry.
;                   $I_Ubound  - End reading at this Array entry.
;                                Default UBound($a_Array) - 1
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0 and sets @error = 1
; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
;                   Modified by PsaltyDS at the AutoIt forums.
; Note(s):          None
;
;===============================================================================
Func __FileWriteFromArray($sFilePath, $a_Array, $i_Base = 0, $i_UBound = 0)
    ; Check if we have a valid array as input
    If Not IsArray($a_Array) Then Return SetError(2, 0, 0)
    
    ; determine last entry
    Local $last = UBound($a_Array) - 1
    If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
    If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0
    
    ; Open output file for overwrite by default, or use input file handle if passed
    If IsString($sFilePath) Then
        Local $hFile = FileOpen($sFilePath, 2)
    Else
        Local $hFile = $sFilePath
    EndIf
    If $hFile = -1 Then Return SetError(1, 0, 0)
    
    FileWrite($hFile, $a_Array[$i_Base])
    For $x = $i_Base + 1 To $i_UBound
        FileWrite($hFile, @CRLF & $a_Array[$x])
    Next

    If IsString($sFilePath) Then FileClose($hFile)
    Return 1
EndFunc   ;==>__FileWriteFromArray

There isn't a function for IsFileHandle(), so this mod assumes if it's not a string, it must be a file handle.

<_<

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
whitts

Das Ami here is my code...

CODE
#include <GUIConstants.au3>

#include <Array.au3>

#include <File.au3>

Global $Saved[1];This will be an array of 2 elements, but _ArrayAdd will take care of

;making it bigger as you add data.

GUICreate("", 273, 192, 217, 152)

$Input1 = GUICtrlCreateInput("Input1", 12, 14, 245, 21)

$Input2 = GUICtrlCreateInput("Input2", 12, 47, 245, 21)

$Input3 = GUICtrlCreateInput("Input3", 12, 80, 245, 21)

$Save = GUICtrlCreateButton("Save to memory", 22, 130, 105, 45, $BS_DEFPUSHBUTTON)

$Write = GUICtrlCreateButton("Write to file", 142, 130, 105, 45, 0)

GUISetState(@SW_SHOW)

#include <File.au3>

If Not _FileCreate("myfile.txt") Then

MsgBox(4096,"Error", " Error Creating/Resetting log. error:" & @error)

EndIf

$file = FileOpen("myfile.txt", 1)

; Check if file opened for writing OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

FileWriteLine($file, 'ECHO.^<package^>>"yes.wsf"')

FileWriteLine($file, 'ECHO.>>"yes.wsf"')

FileWriteLine($file, '')

FileWriteLine($file, 'ECHO.^<job id="vbs"^>>>"yes.wsf"')

FileWriteLine($file, 'ECHO.>>"yes.wsf"')

FileWriteLine($file, 'ECHO. ^<script language="VBScript"^>>>"yes.wsf"')

FileWriteLine($file, 'ECHO.set WshShell = WScript.CreateObject("WScript.Shell")>>"yes.wsf"')

FileWriteLine($file, 'ECHO.WScript.Sleep 3000>>"yes.wsf"')

FileWriteLine($file, 'ECHO.WshShell.SendKeys "Y">>"yes.wsf"')

FileWriteLine($file, 'ECHO.^</script^>>>"yes.wsf"')

FileWriteLine($file, 'ECHO.^</job^>>>"yes.wsf"')

FileWriteLine($file, 'ECHO.^</package^>>>"yes.wsf"')

FileWriteLine($file, '')

FileWriteLine($file, 'ECHO.Windows Registry Editor Version 5.00>"printer.reg"')

FileWriteLine($file, 'ECHO.>>"printer.reg"')

FileWriteLine($file, '')

FileClose($file)

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Save

_ArrayAdd($Saved,'ECHO.[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports]>>"printer.reg"')

_ArrayAdd($Saved,'ECHO."\\\\' &GUICtrlRead($Input1) & "\\" &GUICtrlRead($Input2)& '"="">"printer.reg">>"printer.reg"')

GUICtrlSetData($Input1,"")

GUICtrlSetData($Input2,"")

GUICtrlSetData($Input3,"")

Case $Write

;~ _ArrayDisplay($Saved)

_FileWriteFromArray("MyFile.txt",$Saved)

EndSwitch

WEnd

Thanks BsaltyDS i will take a look at that

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  

×