Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Reading and changing text in file

Status
Not open for further replies.

roebern

IS-IT--Management
Joined
Oct 24, 2002
Messages
16
I need some help opening a file (pcaudit.txt)
that looks like this:
7/5/2004 1:10:09 PM, 6X27KN8ZG081, ROEBERN, roebern, ROEB071804
and if the date time has changed then only changing that particular piece of text with the new date and time
I have tried several differant things and finally figured out how to get the text into an array but I am unser if this is correct way to go about doing what I need to do...could really use some pointers in the right direction..

Thanks
Nathan
 
if the date time has changed
How the script is aware of this change ?
I have tried several differant things
Can you please post what you have so far, explaining why it's not working ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Forget everything I said in the original post... I was just looking to get some quick examples.
Now here goes from the start...
I have an audit script that will generate a txt file on a users H drive with select information regarding users pc.
this is the script:

'On Error Resume Next
' Built 07/05/04 Revision 10
'WScript.Sleep 250000
Dim oFSO, oShell
Dim thefile, strSearch, fso, foundit, infile, inline
Dim logintime, sn, nwuname, uname, compname, totmem, cpu, osver, macadd, ipadd
Dim strline, colMatches

Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set oShell = WScript.CreateObject( "WScript.Shell" )
Set oFSO = CreateObject ("Scripting.FileSystemObject")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set getSN = objWMIService.ExecQuery ("Select SerialNumber from Win32_BIOS")
Set getItems = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
Set getPRC = objWMIService.ExecQuery ("Select Name from Win32_Processor")
Set getOS = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
Set colwdrv = objWMIService.ExecQuery("Select * from Win32_NetworkConnection WHERE LocalName = 'W:'",,48)
Set ipConfigSet = objWMIService.ExecQuery ("SELECT Caption, IPAddress, MACAddress FROM Win32_NetworkAdapterConfiguration WHERE IPenabled=True")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

'Set Audit File Variables
Logintime = Date & " " & time

'Get Model of PC
For Each objItem in colItems
pcmod = Trim (objItem.Model)
Next

' Get Network configuration
Count = 0

for each IPConfig in IPConfigSet
Count = Count + 1
Next
ReDim sName(Count - 1)
ReDim sIP(Count - 1)
ReDim sMAC(Count - 1)

Count = 0

for each IPConfig in IPConfigSet
sName(0) = IPConfig.Caption(0)
sIP(0) = IPConfig.IPAddress(0)
sMAC(0) = IPConfig.MACAddress(0)

Count = Count + 1
strOutput = strOutput & sName(0) & " " & sIp(0) & " " & sMac(0)
Next

'Get Machine Serial Number
For each objBIOS in getSN
sn = RTrim (objBios.SerialNumber)
Next

'Get usernames
For each objItem in getOS
If objItem.Caption = "Microsoft Windows 2000 Professional" Then
uname = oShell.ExpandEnvironmentStrings("%NWUSERNAME%")
msuname = oShell.ExpandEnvironmentStrings("%USERNAME%")
Elseif objItem.Caption = "Microsoft Windows XP Professional" Then
uname = oShell.ExpandEnvironmentStrings("%NWUSERNAME%")
msuname = oShell.ExpandEnvironmentStrings("%USERNAME%")
Elseif objItem.Caption = "Microsoft Windows" Then
w98 = objItem.Caption
End If
Next

For Each objitem in getItems
If w98 = "Microsoft Windows" Then
uname = oShell.RegRead("HKEY_LOCAL_MACHINE\Network\Logon\NetWareUsername")
msuname = "null"
Elseif uname = "%NWUSERNAME%" Then
uname = objItem.Username
msuname = "null"
End If
Next

'Get PC Name and Memory
For each objItem In getItems
compname = objItem.Name
totmem = Trim (objItem.TotalPhysicalMemory)
Next

'Get Processor Type
For each objName in getPRC
cpu = Trim (objName.Name)
Next

'Get Trend Pattern File
For each objItem in getOS
osver = objItem.Caption
If objItem.Caption = "Microsoft Windows 2000 Professional" Then
tmpat = oShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc.\PatternVer")
Elseif objItem.Caption = "Microsoft Windows XP Professional" Then
tmpat = oShell.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc.\PatternVer")
Elseif objItem.Caption = "Microsoft Windows" Then
osver = objitem.caption & " 98"
Set objFile = oFSO.OpenTextFile("C:\Program Files\Trend Micro\OfficeScan Client\AUBin\Patch.lst", 1)
Do Until objFile.AtEndOfStream
strskLine = objFile.Skipline
tmpat = Right (objFile.Readline, 3)
Loop
objFile.Close
End if
Next

If oFSO.FileExists("h:\pcaudit.txt") Then
oFSO.DeleteFile ("h:\pcaudit.txt")
End If

zaffile = ("h:\pcaudit.zaf")
If oFSO.FileExists (ZAFFile) Then
Call readfile
Else
Call CreateFile
End IF

'================================================================
Sub CreateFile 'Creates Audit File if no Audit file exists

Set objTextFile = oFSO.CreateTextFile (zaffile, 2)
objTextFile.Write Date & " " & Time & ", " & sn & ", " & uname & ", " & msuname _
& ", " & compname & ", " & pcmod & ", " & totmem & ", " & cpu & ", " & osver & ", " & tmpat & ", " & strOutput
objtextfile.close
End Sub
WScript.Quit

'================================================================
Sub Readfile 'Append new pc records on a new line to Audit File
Set oFSO = CreateObject ("Scripting.FileSystemObject")
Set objTextFile = oFSO.OpenTextFile ("h:\pcaudit.zaf", 1)
Do Until objTextFile.Atendofstream
' strcontents = objTextFile.Readall
strline = objtextfile.readline
WScript.Echo strline
Loop
If (InStr (strline, sn)) Then
WScript.Echo "This computer exists exists in file"
Else
Call AppendFile
End if
End Sub
WScript.Quit

'================================================================
Sub Appendfile 'Append new pc records on a new line to Audit File
Set objTextFile = oFSO.OpenTextFile (zaffile, 8)
strcontents = objTextFile.WriteBlankLines (1)
objTextFile.Write logintime & ", " & sn & ", " & uname & ", " & msuname _
& ", " & compname & ", " & pcmod & ", " & totmem & ", " & cpu & ", " & osver & ", " & tmpat & ", " & strOutput
objTextFile.Close
End Sub
WScript.Quit

'================================================================

This is the output I get with the script
7/5/2004 2:07:26 PM, 6X27KN8ZG081, ROEBERN, roebern, ROEB071804, Evo D510 CMT, 259506176, Intel(R) Pentium(R) 4 CPU 2.00GHz, Microsoft Windows 2000 Professional, 192300,(network adaptor info edited for posting)
7/5/2004 2:09:07 PM, 6123DYSZC361, ROEBERN, roebern, ROEB071802, Compaq Deskpro, 266780672, Intel Pentium III processor, Microsoft Windows 2000 Professional, 192800,(network adaptor info edited for posting)

I realize this is probably an extreamly messing script but it is my first major script and our environment is completetly and totally f****D up... not to mention the way my boss wants to do this thing makes no sense whatsoever but thats all beside the point...

basically the last thing I need it to do is look at the text contained in the file and have it update just the time with whatever the time the pc logged in
for instance
If the pc 6123DYSZC361 logs in at 7/5/2004 2:09:07 PM and then again say 10 minutes later it will change only the 7/5/2004 2:09:07 PM part of the line to 7/5/2004 2:19:07 PM
I will also need it to change the username if it does not match as well but i figure if i can figure out how to change just the date and time i should be able to figure the rest out on my own....

Thanks again for your help!
 
One way I know to update a text file with the fso is to read the whole file, make the update in memory, close the file, reopen the file for output and write back the modified contents.
Another way, if file is very big, is to play with 2 files, one input and another output and at end replacing the input file with the output file.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top