Sign in to follow this  
Followers 0
slartie

Trying to find a line and conditionally replace it

5 posts in this topic

I have a script that gathers BIOS and registry information for the PC's in my company network, writes the information to a text file. Now I'd like to expand the script, so it maintains the list of records, but updates a line of information with new data if that happens to be the case.

In this case I'd like to update the records to reflect that a new user is using a given machine.

I've found _FileWriteToLine which would seem to be the way to go, but it's been a bit too long since I last messed with AutoIt, so I'm actually not sure how to go about it. Any help/tips would be greatly appreciated.

$servicetag = ""
$manufacturer = ""
$cpuspeed = ""
$cpu = ""
$model = ""
$modeldell = ""
servicetag()
registryinfo()
writemachineinfo()

Func servicetag()
$objshell = ObjCreate("WScript.Shell")
$objfs = ObjCreate("Scripting.FileSystemObject")
$strcomputer = "."
$owmi = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strcomputer & "\root\cimv2")
$colsmbios = $owmi.execquery("Select * from Win32_SystemEnclosure")
For $objsmbios In $colsmbios
$servicetag = $objsmbios.serialnumber
$manufacturer = $objsmbios.manufacturer
Next
EndFunc

Func registryinfo()
$cpuspeed = RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "~MHz")
$cpu = RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "ProcessorNameString")
$model = RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS", "SystemFamily")
$modeldell = RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS", "SystemProductName")
EndFunc

Func writemachineinfo()
$filetoopen = FileOpen("\\srv11\MachineInfo\records.txt", 0)
$textfilename = FileRead($filetoopen)
FileClose($filetoopen)
$texttofile = @ComputerName & "," & @UserName & "," & $servicetag & "," & $manufacturer & "," & $model & "," & $modeldell & "," & @OSVersion & "," & $cpu & @CRLF
If StringInStr($textfilename, @ComputerName, 0) Then
Else
$filetoopen = FileOpen("\\srv11\MachineInfo\records.txt", 2)
$temp = $textfilename & $texttofile
FileWrite($filetoopen, $temp)
FileClose($filetoopen)
EndIf
EndFunc

Share this post


Link to post
Share on other sites



i think u should try Ini's

using them if the keyname is the same then the data of the key is overwritten

This should be the simplest way [ in accordance to me]

Try IniWrite and IniRead in place of FileWrite and FileRead respectively

Regards

Phoenix XL


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

While I can see how the .Ini approach would work, it would make the records a slight nightmare to import into a database elsewhere.

So for now I'll have to find a way to manipulate the records.txt file "as is" rather than changing the format.

Share this post


Link to post
Share on other sites

While I can see how the .Ini approach would work, it would make the records a slight nightmare to import into a database elsewhere.

So why not look into AutoIt's SQLite options in the help file, and use a database to begin with? Then you won't have to worry about correctly importing from a flat text file later on :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

So why not look into AutoIt's SQLite options in the help file, and use a database to begin with? Then you won't have to worry about correctly importing from a flat text file later on :)

Now there's something I didn't know about. I'll be looking into that immediately!

Thanks for the heads up!

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  
Followers 0