Jump to content

Help using iniwrite with an Array


Recommended Posts

Hi All,

I'm new to AutoIt and I have a number of ini files I need to write a change to that are the same name but located in various places on a server. I have a Excel spreadsheet that has the locations of each file and I am able to read that data into an array but I when I try to use the iniwrite command to write out to each file using the element of the array as the file location I can’t seem to get it to work. The script doesn’t generate any syntax errors and runs, but nothing happens as far as I can tell. Can someone take a look at the script and tell me what I'm doing wrong?

In the Excel spreadsheet I have the file locations starting in cell A1 through cell A5

For example cell A1 contains the data

S:\dns\test1\afw.ini

With S being the drive letter

DNS being the first level directory

Test1 being the second level directory

afw.ini being the name of the ini file

The remaining cells contain the same except Test1 is replaced with Test2, Test3, Test4, Test5

I wrote a much simpler script that worked for local workstations, but I need to edit the same ini on some Citrix servers for users in remote locations and its located in a number of different directories.

The script that I wrote for local workstations that works is as follows:

; Script to update the One Staff AFW.INI file to insert the location for staff photos for One Staff

if FileExists ("c:\windows\afw.ini") Then

IniWrite ("c:\windows\afw.ini", "Common", "Photo Folder", "W:\data\Photos")

MsgBox (0, "One Staff Photo Update", "Update Complete Please Reboot your System")

Else

Msgbox (0, "File does not exist", "Please Contact the IS department")

EndIf

The script to edit the ini file using an array element for the location that does not work for some reason is:

#include <Excel.au3>

#include <Array.au3>

local $i

Local $oExcel = _ExcelBookOpen("S:\PJ - Onestaff photo\test1.xls") ; Opens Excel workbook

$aArray1 = _ExcelReadArray($oExcel, 1, 1, 5, 1) ;Direction is Vertical

for $i = 1 to 5 -1

IniWrite($aArray1[$i],"Common", "Photo Folder", "W:\data\Photos")

Next

MsgBox(0, "Exiting", "Press OK to Exit")

_ExcelBookClose($oExcel)

Any help would be greatly appreciated as I am new to AutoIt and can’t see where my error is. I tried Googling for iniwrite using variables or arrays but came up empty.

Any help would be greatly appreciated as I am trying to fishing a project with a due date for end of April and this is the last piece.

Cheers,

Gordon

Link to comment
Share on other sites

Use this slightly modified code. I just added some error checking after the IniWrite statement. Report here the result of $errorCheck please.

#include <Excel.au3>
#include <Array.au3>

If FileExists("c:\windows\afw.ini") Then
    IniWrite("c:\windows\afw.ini", "Common", "Photo Folder", "W:\data\Photos")
    MsgBox(0, "One Staff Photo Update", "Update Complete Please Reboot your System")
Else
    MsgBox(0, "File does not exist", "Please Contact the IS department")
EndIf

;~ The script To edit the ini file using an array element For the location that does not work for some reason is:
Global $oExcel = _ExcelBookOpen("S:\PJ - Onestaff photo\test1.xls") ; Opens Excel workbook
Global $aArray1 = _ExcelReadArray($oExcel, 1, 1, 5, 1) ;Direction is Vertical
Global $errorCheck

For $i = 1 To 5 - 1
    $errorCheck = IniWrite($aArray1[$i], "Common", "Photo Folder", "W:\data\Photos")

    MsgBox(0, "Error:", $errorCheck)
Next

MsgBox(0, "Exiting", "Press OK to Exit")

_ExcelBookClose($oExcel)
Edited by jaberwocky6669
Link to comment
Share on other sites

Hi Jaberwocky,

Thanks for the help and quick response, that helped a lot :idea: . I ran the modified script, just the part that uses the array and got an error code back of 0. Did a little more checking and determined that I had my path wrong, so I renamed the directory and modified the excel sheet. I then ran it again and got a 1 for the error checking. This time some of the ini files were updated, but not the first one. The [Common] section of the ini files in

S:\Onestaffphoto\DNS\Test1\afw.ini – did not update

S:\Onestaffphoto\DNS\Test2\afw.ini – updated

S:\Onestaffphoto\DNS\Test3\afw.ini – updated

S:\Onestaffphoto\DNS\Test4\afw.ini – updated

S:\Onestaffphoto\DNS\Test5\afw.ini – updated

For Test2 through Test5 it went from

[Common]

Check All Allocs=0

Data Folder=W:\data

Minimum FSR Percent=0

Database Type=Btrieve

;Data Folder=H:\ansosonestaff\data

;Data Folder=C:\ansosonestaff\DATA

To

[Common]

Check All Allocs=0

Data Folder=W:\data

Minimum FSR Percent=0

Database Type=Btrieve

Photo Folder=W:\data\Photos

;Data Folder=H:\ansosonestaff\data

;Data Folder=C:\ansosonestaff\DATA

The script I ran is:

#include <Excel.au3>

#include <Array.au3>

Global $oExcel = _ExcelBookOpen("S:\Onestaffphoto\test1.xls") ; Opens Excel workbook

Global $aArray1 = _ExcelReadArray($oExcel, 1, 1, 5, 1) ;Direction is Vertical

Global $errorCheck

For $i = 1 To 5 - 1

$errorCheck = IniWrite($aArray1[$i], "Common", "Photo Folder", "W:\data\Photos")

MsgBox(0, "Error:", $errorCheck)

Next

MsgBox(0, "Exiting", "Press OK to Exit")

_ExcelBookClose($oExcel)

At this point I am guessing it has something to do with my For loop. Also when the error checking ran it only went through 4 iterations not the expected 5. I am going to try taking out the -1 and see what that gets me.

Cheers,

Gordon

Link to comment
Share on other sites

Hi Jaberwocky,

Made another change and it finally worked as desired. The script is now:

#include <Excel.au3>

#include <Array.au3>

Global $oExcel = _ExcelBookOpen("S:\Onestaffphoto\test1.xls") ; Opens Excel workbook

Global $aArray1 = _ExcelReadArray($oExcel, 1, 1, 5, 1) ;Direction is Vertical

Global $errorCheck

For $i = 0 To 5 -1

$errorCheck = IniWrite($aArray1[$i], "Common", "Photo Folder", "W:\data\Photos")

MsgBox(0, "Error:", $errorCheck)

Next

MsgBox(0, "Exiting", "Press OK to Exit")

_ExcelBookClose($oExcel)

Thanks for the help. Now I can go to bed and stop stressing about this one. I'll run it on the Citrix servers in the AM and then I can put this one to bed :idea: I owe you one.

Cheers,

Gordon

Link to comment
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
 Share

  • Recently Browsing   0 members

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