Page 1 of 3 123>
Topic Options
#196835 - 2009-11-25 08:53 PM another project any guidance would be appreciated
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
Hey guys I am off doing another little project

this is my algorithm that I want to perform. there is more to it but i have that down pat this is what i want to do in for each loop:

 Code:
for each $Element in $PClist
  open file "fsp.ini"
  look for string line "pcAlias=" and swap with it "pcAlias=$Element"
  close file "fsp.ini"
next 



what string manip function or syntax does kix provide any examples
thank you
happy thanksgiving to you all
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196836 - 2009-11-25 09:08 PM Re: another project any guidance would be appreciated [Re: itdaddy]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
You could read the file into an array with the ReadFile() UDF, get the location of "pcAlias=" with ASCAN, change the value, and re-write the file with the WriteFile() UDF:

 Code:
for each $Element in $PClist
    $array = ReadFile("fsp.ini")
    $loc = AScan($array, "pcAlias=",,,1)
    if $loc <> -1
       $array[$loc] = "pcAlias="+$Element
       $ = WriteFile("fsp.ini",$array)
    endif
next

However, the logic you have mapped out would ultimately just replace the same line in the same file repeatedly until the end of the array, so I'm a little confused as what you're really trying to do...


Edited by eriqjaffe (2009-11-25 09:08 PM)

Top
#196838 - 2009-11-25 09:31 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
thank you for replying
eriqjaffe
do i really need the if/endif condition? since
when it finds it it is going to write to and move
on due to the for each/next loop? just curious wha you think?

 Code:
for each $Element in $PClist
    $array = ReadFile("fsp.ini")
    $loc = AScan($array, "pcAlias=",,,1)
    $array[$loc] = "pcAlias="+$Element
    $ = WriteFile("fsp.ini",$array)
    copy fsp.ini to \\$Element\c$\Windows\    ;my psuedocode
next


eriqjaffe

does this makes sense?




Edited by itdaddy (2009-11-25 09:32 PM)
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196839 - 2009-11-25 09:32 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
RTFM!! 15 years writing Kix scripts and I still have a hardcopy manual on my desk that I reference regularly!

INI file manipulation is done via ReadProfileString and WriteProfileString functions - it's like using a very simple database. Use standard file I/O and you've got a headache.

Look for EnumINI UDFs to help you explore the contents of the INI file.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196841 - 2009-11-25 09:50 PM Re: another project any guidance would be appreciated [Re: Glenn Barnas]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
okay glenn will do thanks yeah a reference is very handy ;\)
-Robert


Edited by itdaddy (2009-11-25 09:50 PM)
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196842 - 2009-11-25 09:55 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
eriqjaffe
how is this?


 Code:
for each $Element in $PClist
    $array = ReadFile("fsp.ini")
    $loc = AScan($array, "pcAlias=",,,1)
    $array[$loc] = "pcAlias="+$Element
    $ = WriteFile("fsp.ini",$array)
    Shell "CMD.EXE /C xcopy fsp.ini \\$Element\C$\WINDOWS\ /-Y"
next

_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196843 - 2009-11-25 10:11 PM Re: another project any guidance would be appreciated [Re: itdaddy]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
No need to shell out, you could just have WriteFile() directly write the file. Now that I see a bit more what you're going for, there really isn't any need to look for that string every time, either:

 Code:
$array = ReadFile(@SCRIPTDIR+"\fsp.ini")
$loc = AScan($array, "pcAlias=",,,1)

if $loc <> =1
  for each $Element in $PCList
     $array[$loc] = "pcAlias="+$Element
     $ = WriteFile("\\"+$Element+"\c$\windows\fsp.ini")
  next
endif



Edited by eriqjaffe (2009-11-25 10:14 PM)

Top
#196844 - 2009-11-25 10:41 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin

eriqjaffe
this is what I have it just hangs? not sure why?
can you see what I am doing wrong. It looks right
but when I execute it with the command cmd like this

kix32.exe fspreplace.kix

it juts hangs nothing happens??

 Code:
Break On

Dim $PCList
Dim $array
Dim $loc
$PCList = "isasst", "isasst", "isasst"

$array = ReadFile("fsp.ini")
$loc = AScan($array, "pcAlias=",,,1)

if $loc <> =1
  for each $Element in $PCList
     $array[$loc] = "pcAlias="+$Element
     $ = WriteFile("\\"+$Element+"\c$\windows\fsp.ini")
     ? $Element
  next
endif



;************************************
;    ReadFile() UDF
;************************************

;Function	ReadFile()  
;  
;Author		Radimus  
;  
;Contributors	Lonky... This is basically his code that I trimmed down to its  
;		simplest function  
;  
;Action		Reads a file into an array  
;  
;Syntax		$array=ReadFile($file)  
;  
;Version	1.0.1  
;  
;Date           10/21/2003  
;  
;Date Revised   10-22-2003 - dimmed vars 
;  
;Parameters 	file   
;		source filename  
;  
;Remarks	Pair this with WriteFile() to read and write an entire file easily  
;  
;Returns	@error if failed  
;   
;Dependencies 	None  
;  
;KiXtart Ver	4.02  
;   
;Example(s)	$array=ReadFile('c:\file.txt')  

Function ReadFile($file)
	Dim $lf, $f, $_, $t
	$lf=chr(10)
	$f=freefilehandle
	$_=open($f,$file)
		if @error   exit @error   endif
		do $t=$t+$lf+readline($f) until @error
		$_=close($f)
	$ReadFile=split(substr($t,2),$lf)
	EndFunction

;************************************
;  WriteFile() UDF
;************************************

;Function	WriteFile()  
;  
;Author		Radimus  
;  
;Contributors	Lonky  
;  
;Action		Writes an array as a file  
;  
;Syntax		WriteFile('filename','array')  
;  
;Version	1.1  
;  
;Date           10/21/2003  
;  
;Date Revised   11/13/2003  
;  
;Parameters 	file   
;		destination filename  
;		array   
;		an array of data to write to a file  
;  
;Remarks	Pair this with ReadFile() to read and write an entire file easily  
;  
;Returns	@error if failed  
;   
;Dependencies 	None  
;  
;KiXtart Ver	4.02  
;   
;Example(s)	$=WriteFile('c:\file.txt',$array)  

Function WriteFile($file,$array)
	Dim $, $f, $line
	If Not VarType($Array) & 8192    Exit(1)        EndIf
	$f=freefilehandle
	$=open($f,$file,5)
		if @error   exit @error   endif
		for each $line in $array
			$=writeline($f,$line+@crlf)
			if @error   exit @error   endif
			next
		$=close($f)
	EndFunction
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196845 - 2009-11-25 11:23 PM Re: another project any guidance would be appreciated [Re: itdaddy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Robert - Please post your INI file (or part of it, starting from the [SECTION] that contains the data you're trying to manipulate.. this can probably be reduced to 1 or 2 lines and be solved in a heartbeat.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196849 - 2009-11-26 12:28 PM Re: another project any guidance would be appreciated [Re: Glenn Barnas]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Or 3...
 Code:
For Each $Element in $PClist
  $=WriteProfileString("C:\MyIni.ini","MySECTION","pcAlias",$Element)
Next

Top
#196851 - 2009-11-26 03:18 PM Re: another project any guidance would be appreciated [Re: Arend_]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I wasn't counting the "glue" code.. ;\)

I'm thinking that a ReadProfileString to determine if the value exists in the section (maybe) and a WriteProfileString to update it is all that would be necessary to change the setting. It depends on whether the value "must contain specific data IF it exists" or the value "must exist with specific data".

Enumerating an INI file via traditional file read/write methods can become convoluted by comparison to using EnumINI to get a list of sections, and a loop of Read & WriteProfileString calls to check for and update the data.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196852 - 2009-11-26 03:54 PM Re: another project any guidance would be appreciated [Re: Glenn Barnas]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
I do agree, a ReadProfileString might be needed if the value contains data that musn't be overwritten.
Top
#196933 - 2009-12-02 04:23 PM Re: another project any guidance would be appreciated [Re: Glenn Barnas]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
The reason I am doing this is because I have scripts that install
an MSI file on each machine and this program/msi uses a ini file called
fsp.ini. If I need to for any reason uninstall the msi file with a switch /u, it destroys the fsp.ini file. which means that I have to remote into 150 PCs and change one line entry "pcAlias=(blank)" to "pcAlias=(pcname)".(each pass of the fsp.ini mod will have the previous pcname
to the right of the "=" sign). There is only one entry that is exclusive and that is the pcname on the right side of "pcAlias=" and the rest of the "ini" file
is the same canned soup for each pc. The only thing that changes is the pcname so I want to auto generate it. I already use the LDAP program yo guys helped me build to fill my arrays of $Elements up so I just need to
do the simple algorithm I have outline below.

and sorry sow late getting back to you all. I have been sick as a dog.
when i get sick PC time is at a hault. but I am better now.

thank you Glenn and Apronk for your help! you dudes rock!

so what do you think after all this info????



 Code:
/* my algorithm ; trying to kee it simple */
for each $Element in $Branch

	file open
        read in line that matches "pcAlias="
        swap string "pcAlias=" with "pcAlias=$Element"
        writeline that is "pcAlias=$Element"
        file close
        copy fsp.ini to that $Element
        
Next $Element






 Code:
/* this is top of the ini file called fsp.ini */
[Ubs]
LastUser=rvand
pcAlias=pcname
PortNumber=9600
HostName=192.168.1.94
SessionName=Original Session

/* there is more but the top is all I need */


Edited by itdaddy (2009-12-02 05:10 PM)
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196936 - 2009-12-02 05:07 PM Re: another project any guidance would be appreciated [Re: itdaddy]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
Well, if the ini file is always structured the same, there's really no need to go looking for the line.

Going back to my original solution...

 Code:
for each $Element in $PClist
    $array = ReadFile("fsp.ini")
    $array[2] = "pcAlias="+$Element
    $ = WriteFile("\\"+$Element+"\C$\fsp.ini",$array)
next

Top
#196938 - 2009-12-02 06:07 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
eriqjaffe
thanks for you help. I am alittle confused.
I am going to interpret your code to the right let me know
if I am on right track.



 Code:
for each $Element in $PClist
    $array = ReadFile("fsp.ini")     /* read entire ini file into array
    $array[2] = "pcAlias="+$Element /*set value array(2) to string value
    $ = WriteFile("\\"+$Element+"\C$\fsp.ini",$array)/* write array back to file?
next

but how does this code know where to remove and replace the string
it has to remove and replace the exact line of code?????
that last line seems to copy the fsp.ini to a pc via UNC path
sure but it write the entire array back to file? how does it remove 
and replace the exact line string pcAlias=previouspcname with pcAlias=newpcname????



_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196939 - 2009-12-02 06:09 PM Re: another project any guidance would be appreciated [Re: Glenn Barnas]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
Glenn and Apronk
I will look into the UDF Enumini for sure that seems more like it
;\) thanks for your help guys
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196941 - 2009-12-02 06:23 PM Re: another project any guidance would be appreciated [Re: itdaddy]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
I see something I neglected to put in the code. This may be better:

 Code:
for each $Element in $PClist
    $array = ReadFile(@LDRIVE&"\fsp.ini")     ;read "master" ini file into array
    $array[2] = "pcAlias="+$Element ;set value array(2) to string value
    $ = WriteFile("\\"+$Element+"\C$\fsp.ini",$array) ;write array back to file on the target machine.
next

From what you've said, I assume that the INI file is static, aside from the one line. That being the case, there's no need to look for the line, or to verify what the line says.

A "master" INI file (which can be located on, say, @LDRIVE or some other network location) gets read into an array (since the INI file can get deleted from the remote systems, this make for a safety net). The value you want to change will always be the third line in the file, so I'm simply changing the value of $array[2] to reflect the correct target. Then, the array gets written to a file on the target machine with the appropriate value in there.


Edited by eriqjaffe (2009-12-02 06:24 PM)

Top
#196945 - 2009-12-02 08:05 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
eriqjaffe

you know now I see what you mean...hahah thanks
okay that is how it is array2 is the 3 element of the array
okay that makes sense to me know..

Glenn and apronk

I think for this case since my ini file is static and doesnt need to read
much and do much, I want to keep it simple, the enumini looks for more
sophisticated algorithm type girations maybe but I can see how
powerful the enumini data structure is set to manipulate the ini data
very nice coding..wow!
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196946 - 2009-12-02 08:25 PM Re: another project any guidance would be appreciated [Re: eriqjaffe]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
eriqjaffe


when i run it it doesnt like line with $array[2] in it ?
now what??

 Code:
Break on
Dim $PClist
$PClist = "ftran1", "ftran2", "ftran3"

for each $Element in $PClist
    $array = ReadFile("fsp.ini")             
    $array[2] = "pcAlias=$Element"                   
    $ = WriteFile("\\"+$Element+"\C$\WINDOWS\fsp.ini",$array) 
    ? $Element
next


Edited by itdaddy (2009-12-02 08:25 PM)
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
#196947 - 2009-12-02 08:38 PM Re: another project any guidance would be appreciated [Re: itdaddy]
itdaddy Offline
Starting to like KiXtart

Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
what a dummy i forgot the UDFs read and write
omg gosh i look at it hhahahha
right i need to have the udf added to the file correct
they dont come with kix32 engine do they??

hey do you guys know of the function to force lower case letters on a name
i need the pcAlias=tolower($Element) to do something like that?
i cant find the tolower function?? in kix

 Code:
[Ubs]
PCAlias=fcall1
HostName=192.168.1.94
PortNumber=9600
LastUser=larno
SessionName=Original Session




i was wrong see the above section? well the line PCAlias= varies
it wil always be in this section and the top section is static
but it maybe be on one pc located on the last line or any line
in this section so I need to some do a scan and find it in this block
and then edit it? how do i do that?


Edited by itdaddy (2009-12-02 09:00 PM)
_________________________
Robert
A+, CCNA, MCP
Network Admin
Credit Union Wisconsin

Top
Page 1 of 3 123>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 255 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.09 seconds in which 0.034 seconds were spent on a total of 15 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org