Page 1 of 1 1
Topic Options
#181152 - 2007-10-05 11:09 AM Delete linie from textfile
Funnythings Offline
Lurker

Registered: 2005-08-08
Posts: 3
Hi, I'm new to Kix scripting, so this may be a simple question.

I've made a script, that runs everytime the user logs on, and collects PCname and MAC-address and writes it to a tekstfile, so I can use the file to shutdown (uses the PC name) and Wake-on-Lan (uses the MACaddress) all our machines. Sometimes we reinstall our PC's and rename so I can't be sure that once the MAC-address is writen, the corresponding PCname is correct. Therefore I want the script to read the tekstfile, check if the MAC-address is allready there. If found then check if the PCname is correct and delete the line if its not correct. Offcourse the script will write a line if the MAC-address isen't found.
I managed to everything except delete the line in the file. How can i do that.
I have not solved the problem with automatical removing old entries from old revomed pc's
Is there an easier way to solve my problem?
The ini.txt file holds lines from each Pc in the format: Ipaddress,PCname,Macaddress

If Open(3, c:\ini.txt) = 0
$x=ReadLine(3)
While @ERROR = 0
$p=Split($x, ",")

If $p[2]="MAC-address"
If $p[1]="PCname"
$=Close(3)
Goto "end"
Else
"Delete line?"
$=Close(3)
Goto "write"
EndIf
EndIf
$x=ReadLine(3)
Loop
$=Close(3)
EndIf
Goto "end"

:write
If Open(4, c:\ini.txt,4) = 0
$ = WriteLine (4, Ipaddress,PCname,Macaddress + @CRLF)
$ = Close(4)
EndIf

:end

Top
#181153 - 2007-10-05 12:23 PM Re: Delete linie from textfile [Re: Funnythings]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Use INI files instead - like a "poor man's database". Here's the general idea:
 Code:
; these values are gathered from the machine
$MAC="00-11-22-33-44-55'
$PC='pcname'
$IP='ip.add.res.s'

; Create or Update this PCs record
$ = WriteProfileString('some\path\PCData.ini', $MAC, 'PC', $PC)
$ = WriteProfileString('some\path\PCData.ini', $MAC, 'IP', $PC)
$ = WriteProfileString('some\path\PCData.ini', $MAC, 'All', $IP + ',' + $PC + ',' + $Mac)

; other code for your app...

; delete the record
$ = WriteProfileString('some\path\PCData.ini', $MAC)

This creates a file with one entry per pc that looks like this:

[00-11-22-33-44-55]
PC=SomePC
IP=ip.add.res.s
DATA=ip.add.res.s,SomePC,00-11-22-33-44-55

The last field could be the only field with few changes to your code, or you could eliminate it and change your code to read individual fields. There are UDFs to return a list of all records in the file, so you won't have to read the file line by line.

Writing an empty value deletes the record (and writing an empty value deletes the field). Using the MAC address as the record ID means that if a PC gets a new address from DHCP, it updates the same record with new data. Same deal if you rebuild/rename the PC - the MAC address doesn't change.

Look for the EnumINI UDFs here, and read about the ReadProfileString/WriteProfileString functions in the manual for more info.

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

Top
#181154 - 2007-10-05 12:54 PM Re: Delete linie from textfile [Re: Glenn Barnas]
Kaiminator Offline
Fresh Scripter

Registered: 2006-08-28
Posts: 17
I have another newbie question, but i cant solve this with an seperate ini file for each wkst

i have a ascii file, wich is filled like this

 Quote:
User;Workstation;IP;Mac;DCHP;OS;WordVersion; ExcelVersion;OutlookVersion


A script will fill in this file with the writeline function on every user logon. The Problem is, that i if the user logs on every day, i get one entry every day. What i wanted to do is, that the script searches for the user line and overwrites the line with the same user.

Top
#181155 - 2007-10-05 01:18 PM Re: Delete linie from textfile [Re: Kaiminator]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
The INI is the answer again - you need to place it on a file share where all users have rights to write to the file. The example I presented above uses a single INI file to manage the data for all workstations. I assume you want to WOL those systems from some server that's running all the time. That server could read the INI file like this to get a list of all records and then enumerate them to send the WOL command:
 Code:
; Load array of MAC records from central INI file
$MACs = Split(ReadProfileString('some\path\PCData.ini'), Chr(10))
; Enumerate array to get IP address
For Each $Mac in $MACs
 ; read the IP address from the current MAC address in the table
 $IP = ReadProfileString('some\path\PCData.ini', $Mac, 'IP')
 $Cmd = 'poweroff.exe WOL -ip ' + $IP + ' -subnet 255.255.255.0 -mac ' + $Mac
 Shell $Cmd
Next

My example used the POWEROFF.EXE utility - your procedure (and mileage) may vary.

An INI file allows you to manipulate indivdual records. Short of actually using a database, it's the best tool we have for these kind of things.

What you're asking for is possible - load the file into an array and find/delete the array record. It's just a lot of coding compared to using the ini file. I have several scripts that use a "self-trimming log" where they write 2 lines, but remove the oldest 2 lines each time. It's a special case, not something I'd do on a regular basis, especially if I had control of the log file design/writing process.

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

Top
#181156 - 2007-10-05 01:38 PM Re: Delete linie from textfile [Re: Glenn Barnas]
Kaiminator Offline
Fresh Scripter

Registered: 2006-08-28
Posts: 17
Hmm

Sorry, Glenn...i am a totaly newbie to that :-(

if i have an ini file, i need sections like [Sektion1], [Sektion2]..etc, right?

But i dont want thism because of better Import into Excel

I have an ascii file, created with this

 Code:
IF Open( 3 , "$Logfile" , 5 ) = 0
     $x = WriteLine( 3 , @WUserid + ";" + @Wksta + ";" + @IPaddress0 + ";" + $Mac + ";" + $DHCP + ";" +  @ProductType + ";" + "Word " + $WordProductID + ";" + "Excel " + $ExcelProductID + ";" + "Outlook " + $OLProductID  + @CRLF)
	 Close(3)


That gives me a file like this

 Quote:
User1;PC1;192.168.1. 64;00:1F:1D:1F:1A:11;True;Windows XP Professional;Word 2003;Excel 2003;Outlook 2003
User2;PC2;192.168.1. 86;00:02:22:AC:F4:48;True;Windows 2000 Professional;Word 2003;Excel 2003;Outlook 2003
User1;PC1;192.168.1. 64;00:1F:1D:1F:1A:11;True;Windows XP Professional;Word 2007;Excel 2007;Outlook 2007
User3;PC3;192.168.1. 46;00:25:AC:D4:22;True;Windows 2000 Professional;Word 2003;Excel 2003;Outlook 2003


You see, User1 is duplicate...want i want to do is, that the scrip searches for Userid or Wkst an overwrites the line. in this example, the script should search for User1 and overwrites the founded line

Thanx in advance \:D


Edited by Kaiminator (2007-10-05 01:43 PM)

Top
#181159 - 2007-10-05 02:28 PM Re: Delete linie from textfile [Re: Kaiminator]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
As Glenn mentioned a INI file is still the best way to go. Then you can always write (use some of Glenns prewritten excel UDF's) to convert it into a excel spreadsheet.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#181163 - 2007-10-05 03:31 PM Re: Delete linie from textfile [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
We all clearly understand what you're asking. What we're saying is that from our experience, what you are trying to do is somewhat complex, and difficult for someone who might be new to scripting.

There are UDFs to convert from INI files to arrays. Once you have an array, you can use the CSV udf to directly write a CSV record, that Excel can read without modification or user interaction. You are using semicolons for delimiters, which requires that you identify them to excel during an import process.

Also, the UDF I published last week will take the array and write it directly to an Excel spreadsheet.

UDFs are not difficult - they are treated as "black boxes". You place the content of a UDF in your file (simplest method) or use the CALL 'file.udf' to load it (slightly more advanced). Once the UDF is in your script, the function is available for use, and you use it like any other Kixtart function that is built in. Thus, you can leverage the efforts of others with minimal effort on your own part. It also exposes you to new ideas and methods as you move from "black box" concept and start reviewing the process in the UDF, often adapting it to other purposes of your own.

Finally, a quick way to experiment with your data structure using INI files is to write your data in exactly the same format that you do now, except to use the "user" name as the INI section name. You can quickly get the data records, read the one field from each record, and write it to a file for import into excel. The big part is it eliminates searching line by line for a record.

The biggest problem is that you can't write just that one line. You'd need to read the file into an array, search the array for the data to change, update that array element, delete the file, and completely rewrite the file with all the new data.

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

Top
#181416 - 2007-10-10 09:41 AM Re: Delete linie from textfile [Re: Glenn Barnas]
Funnythings Offline
Lurker

Registered: 2005-08-08
Posts: 3
Hello Glenn

Can I run an If-Else expression nested inside a For Each expression?
I can't figure out my problem here:

$Sections = Split(ReadProfileString('c:\pc.ini','',''), Chr(13))
For Each $Sec in $Sections
If $Sec="005056C00001"
$PCname = ReadProfileString('c:\pc.ini', $Sec, 'PCnavn')
If $PCname=$PC
? "Do nothing"
Else
$ = WriteProfileString('c:\pc.ini', $Sec, 'PCnavn', $PC)
$ = WriteProfileString('c:\pc.ini', $Sec, 'Last updated', $Lu)
$ = WriteProfileString('c:\pc.ini', $Sec, 'IP', $IP)
EndIf
EndIf
Next

When I try this, the array is not being enumerated. Why?
Also - where can i find the many UDF's you are referring to?

Top
#181417 - 2007-10-10 10:07 AM Re: Delete linie from textfile [Re: Funnythings]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
UDF Forum

Or here

http://www.kixtart.org/udf

Top
#181424 - 2007-10-10 01:35 PM Re: Delete linie from textfile [Re: Funnythings]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Use the EnumINI UDF in your code, just pass it the name of the file to get an array of sections, or file+section to get an array of value names.

You'll see by examining the UDF code that the data returned is not delimited with a carriage-return. ;\)

In fact, your first modification should be to add
UBound($Sections) + 1 ' records were returned by Split!' ?
after the split command, just for debugging and enlightenment.

Aside from not really having an array to work with, your logic looks OK (but) if you know the section name, why enumerate the INI entire file? You can enumerate that section itself by specifying the file and section name values. This part of your logic would make sense only if you were searching for section names that contained a string value.

Also, you can improve your logic by negating it:
 Code:
If NOT $PCName = $PC ; or - If $PCName <> $PC
 do stuff
EndIf

This eliminates the dreaded "Empty If Syndrome" ;\)

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

Top
#181442 - 2007-10-10 04:32 PM Re: Delete linie from textfile [Re: Glenn Barnas]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
$Sections = Split(ReadProfileString('c:\pc.ini','',''), Chr(13))
should be
$Sections = Split(ReadProfileString('c:\pc.ini','',''), Chr(10))

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.113 seconds in which 0.076 seconds were spent on a total of 13 queries. Zlib compression enabled.

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