#181152 - 2007-10-05 11:09 AM
Delete linie from textfile
|
Funnythings
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
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:
; 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!
|
|
Top
|
|
|
|
#181154 - 2007-10-05 12:54 PM
Re: Delete linie from textfile
[Re: Glenn Barnas]
|
Kaiminator
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
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
|
|
|
|
#181156 - 2007-10-05 01:38 PM
Re: Delete linie from textfile
[Re: Glenn Barnas]
|
Kaiminator
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
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
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
Edited by Kaiminator (2007-10-05 01:43 PM)
|
|
Top
|
|
|
|
#181163 - 2007-10-05 03:31 PM
Re: Delete linie from textfile
[Re: Gargoyle]
|
Glenn Barnas
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!
|
|
Top
|
|
|
|
#181416 - 2007-10-10 09:41 AM
Re: Delete linie from textfile
[Re: Glenn Barnas]
|
Funnythings
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
   
Registered: 2000-07-28
Posts: 11628
Loc: CA
|
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|