Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Updating text file!

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,60265
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Updating text file!
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Updating text file! - 5/15/2008 12:39:02 PM   
  dhossai

 

Posts: 2
Score: 0
Joined: 5/15/2008
Status: offline
Hi everyone!
I hope some one would be able to help me out here! I have create a vb script that collect a computer information(eg: serial number, model, user etc) and write that to a existing .txt file, I am also able to append the info to the file if I want to. Now, If I want to update a particular line in that .txt file rather override the whole file, is it possible? If so would some one be kindly give me some example here! For example my existing .txt file has a line" SerialID=abcde" My scripts should update the SerialID=...... part with the new collected serial number. My existing scripts collect the info in a .txt file. I have giving my code here so you can understand.....
quote:



CompName  = Trim(inputbox("Enter name or IP address of computer"))

Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objwbemLocator.ConnectServer _
   (CompName, strNamespace, strUser, strPassword)
objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy

On Error Resume Next
Set colItems = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_ComputerSystem")
Set colBIOS = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_Bios")
Set Obj = objWMIService.ExecQuery("Select * FROM Win32_OperatingSystem")
Set objFS = CreateObject("Scripting.FileSystemobject")
Set objNewFile = objFS.CreateTextFile("output.txt")
objNewFile.WriteLine "Process Report -- Date: " & Now() & vbCrLf
For Each objitem in colitems
For Each objBIOS in colBIOS
For Each object In Obj
objNewFile.writeLine "Computer Information for: " & CompName & VbCrLF & VbCrLF & _
"Manufacturer: " & objBIOS.Manufacturer & VbCrLF & _
"Bios Version: " & objBIOS.Name & VbCrLF & _
       "BIOS Version: " & objBIOS.SMBIOSBIOSVersion & VbCrLF & VbCrLF & _
"HostName: " & objItem.Name & VbCrLF & VbCrLF & _
"Model: " & objItem.Model & VbCrLF & VbCrLF & _
"Serial number: " & objBIOS.SerialNumber & VbCrLF & VbCrLF & _   
       "TotalPhysicalMemory: " & objItem.TotalPhysicalMemory & VbCrLF & VbCrLF & _
       "UserName: " & objItem.UserName & VbCrLF & VbCrLF & _
       "Domain: " & objItem.domain & VbCrLF & VbCrLF & _
"Location: " & object.Description & VbCrLF & VbCrLF & _
       objNewFile.WriteLine

    
      

Next
objNewFile.Close
'Open notepad automatically'
'''''''''''''''''''''''''''''
Set objShell = Wscript.Createobject("Wscript.Shell")
objShell.Run "output.txt"

Next
Next


 
 
Post #: 1
 
 RE: Updating text file! - 5/15/2008 3:57:10 PM   
  dm_4ever


Posts: 2664
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You would open the text file with FileSystemObject, search for the text you need replaced, replace it, and write it back to the text file.

Look at ....
FileSystemObject
OpenTextFile
Replace Function

How many computers are you collecting data for like this?  You may be better off saving this information to a small access database or xml file to make searching and updating values easier.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dhossai)
 
 
Post #: 2
 
 RE: Updating text file! - 5/16/2008 12:40:42 AM   
  dhossai

 

Posts: 2
Score: 0
Joined: 5/15/2008
Status: offline
Thanks dm_4ever for your reply but unfortunately  I am updateing some individual file for some purpose. I have figureout to update the file.For example following is the scripts......
quote:

 
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Inputfile.txt", ForReading)
Do Until objFile.AtEndOfStream
   strLine = objFile.ReadLine
   If strLine = "Fabrikam" Then
       strLine = "Contoso"
   End If
   strContents = strContents & strLine & vbCrLf
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("Inputfile.txt", ForWriting)
objFile.Write(strContents)
objFile.Close

My my trouble is... how can I retrieve hardware information from my first script and update the file in particular line. I hope you have understood my probelm. From my first script I get those say for serial number and I want to update the line in the text file where data is written as
" EditorID=66
Value=12345"
I want to update the "12345" with the new serial number I get from my script. Hope I was able to explain you.

(in reply to dm_4ever)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Updating text file! Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts