Jump to content
Sign in to follow this  
max23ed

Can't write to ini file

Recommended Posts

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Debug.au3>

#Region ### START Koda GUI section ### Form=c:\users\ali\downloads\!testproject\form1.kxf
Global $Form1_1 = GUICreate("Form1", 956, 667, -1, -1)
Global $Browsbtn = GUICtrlCreateButton("Browse a setup", 776, 64, 123, 25)
Global $locationtxt = GUICtrlCreateInput("", 584, 64, 169, 21)
Global $showbtn = GUICtrlCreateButton("Add", 776, 104, 123, 25)
Global $silent_list = GUICtrlCreateList("", 32, 48, 241, 552)
Global $record_list = GUICtrlCreateList("", 320, 48, 249, 552)
Global $aaaaaa = GUICtrlCreateLabel("Silent installs", 40, 16, 64, 17)
Global $Label1 = GUICtrlCreateLabel("Non-Silent installs", 328, 16, 87, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
global $filename
global $abc
global $file_usage
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Browsbtn

            GUICtrlSetData($locationtxt, FileOpenDialog("Select the program to analyze", '', "Supported files (*.exe;*.msi;*.reg;*.inf)|Executable Files (*.exe)|Microsoft Installer files (*.msi)|Registry files (*.reg)|Inf files (*.inf)", 3))
            Sleep(100)
            $abc = GUICtrlRead($locationtxt)
            $filename = StringTrimLeft($abc, StringInStr($abc, '\', 0, -1))


        Case $showbtn
             IniWrite("configg.ini", $filename, "switch", "abc")
            Run("ussf.exe " & $abc, "", @SW_HIDE)
            WinWait('USSF', 'Universal Silent Switch Finder',-1)
            WinSetState("USSF", "Universal Silent Switch Finder", @SW_HIDE)
            ; ############################################## Config.ini values ##########################################
            ; Looping to get the ini values
            Local $file_extension
            WinWait("USSF")
            Do
                $file_extension = ControlGetText("USSF", "", "Edit2")
                $file_type = ControlGetText("USSF", "", "Edit3")
                $file_usage = ControlGetText("USSF", "", "Edit4")
            Until $file_extension <> "Loading..." And $file_extension <> "" And $file_type <> "Loading..." And $file_type <> "" And $file_usage <> "Loading..." And $file_usage <> ""
           sleep(5000)
           ;Here IniWrite is not working idk why 
             IniWrite("config.ini", $filename, "switch", $file_usage)
             

    EndSwitch
WEnd

Hi, i don't know why its not writing. i need it in ini format please help thanks.

Share this post


Link to post
Share on other sites

Couple of possible issues I can see --

1. The variable $filename is only set when clicking the Browse button. What happens if you click the Show button without previously clicking the Browse button

2. One of you IniWrite lines is writing to "configg.ini" and the other to "config.ini"

Share this post


Link to post
Share on other sites

You do not have any error checking for the IniWrite() function, but my guess would be that the INI file is created in the directory which was selected by the  FileOpenDialog() statement as that will change the workdir.
Change the IniWrite statement to:

IniWrite(@scriptdir & "\config.ini", $filename, "switch", $file_usage)

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thanks Jos, when i provide the full path to the ini file, its working fine. i did try what you have suggested but still didn't work.

 

Share this post


Link to post
Share on other sites
Just now, max23ed said:

Thanks Jos, when i provide the full path to the ini file, its working fine. i did try what you have suggested but still didn't work.

 

mmm ...  this is a very confusing post: It works ...but is doesn't?

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I have the script and conifg.ini in the same folder, no subfolders. I want to get the script dir because if i want to run the script from another pc, it will still work

Share this post


Link to post
Share on other sites

I am still confused whether you are asking a question or making a statement here and don't really understand it ..... sorry. :) 

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I'm sorry i poor at explaining. 

This is working, it did write into the ini ---> 

IniWrite("C:\Users\Desktop\projectC\config.ini", $filename, "switch", $file_usage)

however this did not work 

IniWrite(@scriptdir & "\config.ini", $filename, "switch", $file_usage)

can i somehow get the full path so if i run this project in any machine it will still work.

Share this post


Link to post
Share on other sites
Just now, max23ed said:

idk, tried the full path again and didn't work, maybe a bug?

no ...  there is a problem with the script or rights to access the file.
You need to use some debugging to check what is happening, so try running this version and tell us what the shown information is in the MessageBox:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Debug.au3>

#Region ### START Koda GUI section ### Form=c:\users\ali\downloads\!testproject\form1.kxf
Global $Form1_1 = GUICreate("Form1", 956, 667, -1, -1)
Global $Browsbtn = GUICtrlCreateButton("Browse a setup", 776, 64, 123, 25)
Global $locationtxt = GUICtrlCreateInput("", 584, 64, 169, 21)
Global $showbtn = GUICtrlCreateButton("Add", 776, 104, 123, 25)
Global $silent_list = GUICtrlCreateList("", 32, 48, 241, 552)
Global $record_list = GUICtrlCreateList("", 320, 48, 249, 552)
Global $aaaaaa = GUICtrlCreateLabel("Silent installs", 40, 16, 64, 17)
Global $Label1 = GUICtrlCreateLabel("Non-Silent installs", 328, 16, 87, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $filename
Global $abc
Global $file_usage
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Browsbtn

            GUICtrlSetData($locationtxt, FileOpenDialog("Select the program to analyze", '', "Supported files (*.exe;*.msi;*.reg;*.inf)|Executable Files (*.exe)|Microsoft Installer files (*.msi)|Registry files (*.reg)|Inf files (*.inf)", 3))
            Sleep(100)
            $abc = GUICtrlRead($locationtxt)
            $filename = StringTrimLeft($abc, StringInStr($abc, '\', 0, -1))


        Case $showbtn
            IniWrite("configg.ini", $filename, "switch", "abc")
            Run("ussf.exe " & $abc, "", @SW_HIDE)
            WinWait('USSF', 'Universal Silent Switch Finder', -1)
            WinSetState("USSF", "Universal Silent Switch Finder", @SW_HIDE)
            ; ############################################## Config.ini values ##########################################
            ; Looping to get the ini values
            Local $file_extension
            WinWait("USSF")
            Do
                $file_extension = ControlGetText("USSF", "", "Edit2")
                $file_type = ControlGetText("USSF", "", "Edit3")
                $file_usage = ControlGetText("USSF", "", "Edit4")
            Until $file_extension <> "Loading..." And $file_extension <> "" And $file_type <> "Loading..." And $file_type <> "" And $file_usage <> "Loading..." And $file_usage <> ""
            Sleep(5000)
            ;Here IniWrite is not working idk why
            $rc = IniWrite(@ScriptDir & "\config.ini", $filename, "switch", $file_usage)
            MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Ini file:' & @CRLF & @ScriptDir & "\config.ini" & @CRLF & @CRLF & 'RC:' & $rc & @CRLF & '@Error:' & @error) ;### Debug MSGBOX
    EndSwitch
WEnd

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

ini file: 

C:\Users\Desktop\projectC\config.ini

RC:1

@ Error:0

sometime it works, maybe i need to put a sleep after dialog file open

Share this post


Link to post
Share on other sites

wow, actually its my bad. i didn't know that ini file doesnt have the same [sectionName] twice. and so i tried adding another and it worked. sorry i bothered you.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By CodeName_SexBobomb
      #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Test () Func Test() $Form1 = GUICreate("Form1", 414, 212, 1449, 1197) $Group1 = GUICtrlCreateGroup("Group1", 0, 0, 201, 161) Global $Combo1 = GUICtrlCreateCombo("", 0, 16, 201, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) $Data = GUICtrlSetData($Combo1, "Combo 1|Combo 2|Combo 3") Global $Input1 = GUICtrlCreateInput("", 0, 48, 201, 21) GUICtrlCreateGroup("", -99, -99, 1, 1) $Group2 = GUICtrlCreateGroup("Group2", 210, 0, 200, 160) Dim $LABELS[20] Local $DIRECTORY = @ScriptDir & "\Test.ini" Local $SECTION = IniReadSection($DIRECTORY, "Section 1") Local $RESULTS = "" For $r = 1 To $SECTION[0][0] $RESULTS &= $SECTION[$r][1] & @CRLF Next $RESULTS = StringTrimRight($RESULTS, 1) For $N = 0 To 10 $LABELS[$N] = GUICtrlCreateLabel($RESULTS, 220, 15, 200, 100) Next GUICtrlCreateGroup("", -99, -99, 1, 1) $Button1 = GUICtrlCreateButton("Save", 80, 170, 121, 33) $Button2 = GUICtrlCreateButton("Exit", 210, 170, 121, 33) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _WriteData () Case $Button2 Exit EndSwitch WEnd EndFunc _WriteData () Func _WriteData () Local $TestData = GUICtrlRead($Combo1) Local $TestInput = GUICtrlRead($Input1) Local $Directory = @ScriptDir & "\Test.ini" IniWrite ($Directory, "Section 1", "", "" & $TestData & " " & $TestInput & @CRLF & "") EndFunc Hello Friends,
        Perhaps one of you can assist me. Currently I have a semi-working script. The goal of this script is to write the values of the combo box into an INI file and read said INI files. So far it does this.. but the issue is, It writes over line 1. The goal is to write on a new line each time to create a continuous log of submitted data.
    • By GordonFreeman
      Hi, i'm trying to save data in an ini key that contains @LF and @CRLF (to use in an edit control). But @LF and @CRLF cause error in ini files (because it breaks the line). Then im trying to find a workaround. Here's the script thanks in advance:
       
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 438, 192, 124) $hEDIT = GUICtrlCreateEdit("",11,11,600,400) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### IniWrite("JustATest.ini","TestSec","TestKey","TestValue " & @CRLF & "Testing" & @LF & "test") $sData = IniRead("JustATest.ini","TestSec","TestKey","") GUICtrlSetData($hEDIT,$sData) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd  
    • By zemkor
      Hello, i have question. How to save and read data from "GUICtrlCreateEdit" to ini file ?. Problem is: IniWrite write only first line of text and IniRead read only first line text.
      How to do it?
      Thanks for answer.
      $text = GUICtrlCreateEdit("Text", 20, 165, 120, 50) Func save() Local $ini_file, $workingdir ; save workingdir $workingdir = @WorkingDir ; save file dialog $ini_file = FileSaveDialog('Save', @ScriptDir, 'Ini (*.ini)|All (*.*)', 10, 'Config.ini', $Form1) ; check if return is valid If @error Or $ini_file == '' Then FileChangeDir($workingdir) Return SetError(1, 0, '') EndIf ; write to ini file IniWrite($ini_file, "Data", "Text", GUICtrlRead($Text)) EndFunc $text = GUICtrlCreateEdit("Text", 20, 165, 120, 50) Func load() ;natiahne nastavenia z .ini Local $ini_file, $workingdir ; save workingdir $workingdir = @WorkingDir ; open file dialog $ini_file = FileOpenDialog('Open', @ScriptDir, 'Ini (*.ini)|All (*.*)', 1, 'Config.ini', $Form1) ; check if return is valid If @error Or $ini_file == '' Then FileChangeDir($workingdir) Return SetError(1, 0, '') EndIf ; read from ini file GUICtrlSetData($text, IniRead($ini_file, "Data", "Text", "")) EndFunc This not working, read and write only firt line
    • By MHz
      These functions handle ANSI and unicode inifiles similar to IniRead, IniWrite and IniDelete.
      _WinAPI_WritePrivateProfileStringW _WinAPI_GetPrivateProfileStringW So you can read from unicode inifiles created from other programs or perhaps read and write to your own inifiles.
      I was unable to figure out how the API function of WritePrivateProfileStringW can create a unicode file initially so I instead used FileOpen to create a unicode file and write the 1st entry to achieve this. Further use uses WritePrivateProfileStringW is ok to handle the unicode entries. ANSI file creation is done by WritePrivateProfileStringW by default.
      Deletion of keys and sections may need use of Null which is in AutoIt 3.3.9.0 and later. I put comments with the code so hopefully understandable to you.
      The functions use a similar parameter syntax to IniRead and IniWrite. _WinAPI_WritePrivateProfileStringW has an additional parameter to handle the flag passed to FileOpen for the initial creation of the inifile. _WinAPI_GetPrivateProfileStringW has an additional parameter in case you want the buffer that holds the return value within the UDF to be larger.
      The example has some russian text in it so you need to save it in a unicode script for correct testing.
      Example
      ; show message if script is not unicode. Unicode text in this script requires it to be UTF encoded. ; if needed in Scite, use menu bar, File -> Encoding -> UTF (any UTF type that suits you) and save the script. If Not FileGetEncoding(@ScriptFullPath) Then MsgBox(0, @ScriptName, 'Script is not unicode') ; write to ini file $return = _WinAPI_WritePrivateProfileStringW("test.ini", 'russian', 'Open', 'Открыть', 0x21) _Notify('Write', $return) ; read from ini file $return = _WinAPI_GetPrivateProfileStringW("test.ini", 'russian', 'Open', 'Default') _Notify('Read', $return) #cs AutoIt 3.3.9.0 or later using Null keyword ; setting $sValue parameter with Null keyword will delete a key using AutoIt 3.3.9.x or later. $return = _WinAPI_WritePrivateProfileStringW("test.ini", 'russian', 'Open', Null) ; setting $sKey parameter with Null keyword will delete a section using AutoIt 3.3.9.x or later. $return = _WinAPI_WritePrivateProfileStringW("test.ini", 'russian', Null, Null) ; setting $sSection parameter with Null keyword will flush using AutoIt 3.3.9.x or later. $return = _WinAPI_WritePrivateProfileStringW("test.ini", Null, Null, Null) #ce ; More info of an API that mimics these functions and explains use of Null at http://code.google.com/p/privateprofilestring/ Func _Notify($title, $return = 0, $error = @error, $extended = @extended) ; notify results from other function calls MsgBox(StringRegExpReplace($error, '-{0,1}([1-9])[0-9]*', '0x30'), $title, _ '@error = ' & $error & @CRLF & _ '@extended = ' & $extended & @CRLF & _ '$return = ' & $return _ ) EndFunc User defined functions
      ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_WritePrivateProfileStringW ; Description ...: Write to ANSI and unicode encoded ini files ; Syntax.........: _WinAPI_WritePrivateProfileStringW($sFileName, $sSection, $sKey, $sValue[, $iMode = 1]) ; Parameters ....: $sFileName - The filename of the .ini file ; $sSection - The section name in the .ini file ; $sKey - The key name in the in the .ini file ; $sValue - The value to write/change ; $iMode - Refer to FileOpen mode parameter ; Return values .: Success - Not 0 ; Failure - 0 ; @error 1 to 5 - Refer to DllCall ; @error 6 - FileOpen failed to open a handle to create the file ; Author ........: MHz ; Modified.......: ; Remarks .......: Similar to IniWrite but uses unicode API calls. If $sValue is Null then $sKey is deleted. If $sKey is Null, ; then $sSection is deleted. If $sSection, $sKey and $sValue are all Null, then $sFileName is ; flushed. *** Null is a keyword that only exists in AutoIt3 versions 3.3.9.x and later *** ; Related .......: DllCall, FileOpen, IniWrite ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms725501%28v=vs.85%29.aspx ; http://msdn.microsoft.com/en-us/library/windows/desktop/bb773660%28v=vs.85%29.aspx ; Example .......: Yes ; =============================================================================================================================== Func _WinAPI_WritePrivateProfileStringW($sFileName, $sSection, $sKey, $sValue, $iMode = 1) Local $handle_write, $ret ; check if path is relative and make it absolute if it is relative $ret = DllCall('Shlwapi.dll', 'bool', 'PathIsRelativeW', 'wstr', $sFileName); lpszPath If Not @error And $ret[0] Then $sFileName = @WorkingDir & '\' & $sFileName ; create a unicode encoded file if needed and write the entry If Not FileExists($sFileName) And $iMode > 0x20 Then $handle_write = FileOpen($sFileName, $iMode) If $handle_write = -1 Then Return SetError(6, 0, 0) FileWrite($handle_write, '[' & $sSection & ']' & @CRLF & $sKey & '=' & $sValue & @CRLF) FileClose($handle_write) Return 1 EndIf ; write to the ini file. $ret[0] will contain nonzero if successful $ret = DllCall('Kernel32.dll', 'bool', 'WritePrivateProfileStringW', _ 'wstr', $sSection, _ 'wstr', $sKey, _ 'wstr', $sValue, _ 'wstr', $sFileName _ ); lpAppName, lpKeyName, lpString, lpFileName If @error Then Return SetError(@error, @extended, 0) Return SetExtended(0, $ret[0]) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_GetPrivateProfileStringW ; Description ...: Read from ANSI and unicode encoded ini files ; Syntax.........: _WinAPI_GetPrivateProfileStringW($sFileName, $sSection, $sKey[, $sDefault = ''[, $iBufferSize = 65536]]) ; Parameters ....: $sFileName - The filename of the .ini file ; $sSection - The section name in the .ini file ; $sKey - The key name in the in the .ini file ; $sDefault - The default value to return if the requested key is not found ; $iBufferSize - Adjustable buffer size which contains the chars from the API call ; Return values .: Success - Returns the requested key value ; Failure - Returns the default string if requested key not found ; @error 1 to 5 - Refer to DllCall ; Author ........: MHz ; Modified.......: ; Remarks .......: Similar to IniRead but uses unicode API calls ; Related .......: IniRead ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx ; http://msdn.microsoft.com/en-us/library/windows/desktop/bb773660%28v=vs.85%29.aspx ; Example .......: Yes ; =============================================================================================================================== Func _WinAPI_GetPrivateProfileStringW($sFileName, $sSection, $sKey, $sDefault = '', $iBufferSize = 65536) Local $buffer, $ret ; check if path is relative and make it absolute if it is relative $ret = DllCall('Shlwapi.dll', 'bool', 'PathIsRelativeW', 'wstr', $sFileName); lpszPath If Not @error And $ret[0] Then $sFileName = @WorkingDir & '\' & $sFileName ; create a buffer to hold the returned string $buffer = DllStructCreate('wchar[' & $iBufferSize & ']') ; read from the ini file. $ret[0] will contain number of chars returned $ret = DllCall('Kernel32.dll', 'dword', 'GetPrivateProfileStringW', _ 'wstr', $sSection, _ 'wstr', $sKey, _ 'wstr', $sDefault, _ 'ptr', DllStructGetPtr($buffer), _ 'dword', DllStructGetSize($buffer), _ 'wstr', $sFileName _ ); lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName If @error Then SetError(@error, @extended, $sDefault) Return SetExtended($ret[0], DllStructGetData($buffer, 1)) EndFunc Thanks to AZJIO for some russian text for use in the example.
      Edit:
      Updated with summary correction by guiness in post #2
    • By tes5884
      Hi guys,
      I'm trying to add a startup script using autoit.

      One of the steps is writing to the "C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini" file.

      Problem is even though I run the code it doesn't change the actual file, and it returns "1" meaning it was successful.


      $ini = "C:\Windows\System32\GroupPolicy\Machine\Scripts\scripts.ini" $val1 = IniWrite($ini, "Shutdown","0CmdLine", "C:\Scripts\USBReminder.exe") $val2 = IniWrite($ini, "Shutdown","0Parameters", "") MsgBox(0, "", $val1 & @CRLF & $val2)
      Thanks!!
×
×
  • Create New...