Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Please!!! I need help editing files with VBscript

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Please!!! I need help editing files with VBscript
  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 >>
 Please!!! I need help editing files with VBscript - 4/19/2006 10:57:22 AM   
  ZenIT

 

Posts: 6
Score: 0
Joined: 4/19/2006
Status: offline
Is there a way of editing files (i.e. windows .ini files) using VBscripts? For example, we have a program that store a person's email address in an INI file as such:

[MAIL]
FromAddress=job.blow@domain.com
SMTP: mail.domain.com

Our current method is by way of Novell Zenworks. The Novell Zenworks application object is set to change the FromAddress every time a new user runs the program. This way enables each user to have their own email address inserted into the program's INI file and overwrites the previous user's setting before the program actually runs. Since we are replacing Novell with Microsoft AD (ugh!), we have to find another solution.

Our first try was a VBscript that opens a default INI file containing the line "FromAddress=%EMAIL%". The VBscript opens the file and replaces the %EMAIL% with the user's email address and then saves the file to the local hard drive. The problem is that there are other settings in the INI file that a user can change over time. The VBscript to change the FromAddress line would have to run at every logon and that would overwrite the other settings that a user modifies... This would not be good.

My hope is that there's a way of using a VBscript that will replace the whole FromAddress=what.ever@domain.com with FromAddress="user's email address".

If anyone has suggestions, I would really appreciate it.

Thanks in advance.

 
 
Post #: 1
 
 RE: Please!!! I need help editing files with VBscript - 4/19/2006 4:53:59 PM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
you could pull that user's email addy from AD (ugh?) and insert it into the ini file

you are contradicting yourself by the way,
quote:


This way enables each user to have their own email address inserted into the program's INI file and overwrites the previous user's setting before the program actually runs.

contradicts:
quote:


The problem is that there are other settings in the INI file that a user can change over time.

since it sounds like you are trying to get a per user ini setting, why not save each person's ini as a separate file, like username.ini, then just rename it to the original ini each time?

that way each user can maintain their own preferences.

you could probably even do this with just a batch file as well.

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to ZenIT)
 
 
Post #: 2
 
 RE: Please!!! I need help editing files with VBscript - 4/20/2006 1:28:44 AM   
  ZenIT

 

Posts: 6
Score: 0
Joined: 4/19/2006
Status: offline
Let me be clearer:

This enables each user to have their own email address inserted into the program's INI file and overwrites the previous user's email address before the program actually runs.


The current VBscript contains the following:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Program\Default.ini", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "<&&EMAILADDRESS&&>", strUserEmail)
Set objFile = objFSO.OpenTextFile("C:\Program\settings.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close


The problem is that a user might change another setting within setting.ini which would eventually be overwritten by the Default.ini file.

What I'm looking for is a script that can find a line starting with "FromAddress=" and replace the whole line (including the current email address) with FromAddress="user's email address pulled from AD".

(in reply to ZenIT)
 
 
Post #: 3
 
 RE: Please!!! I need help editing files with VBscript - 4/20/2006 1:52:30 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ZenIT,

you can use a RegExp:


      

(in reply to ZenIT)
 
 
Post #: 4
 
 RE: Please!!! I need help editing files with VBscript - 4/20/2006 1:59:04 AM   
  Country73


Posts: 733
Score: 10
Joined: 8/25/2004
From: USA
Status: offline
I believe what kirrilian was getting at was:

You can create a vbscript to alter the email address for the individual that logs into the machine, but you also stated that other settings inside the ini may be altered by each individual. If another line in the ini is altered by USERB, then when you replace USERB's email address when USERC logs in, all the "altered" settings that USERB created will also stay for USERC.

If a seperate INI was created for each individual, any changes each USER() makes will be stored in their own INI file.

Clear as mudd?

(in reply to ZenIT)
 
 
Post #: 5
 
 RE: Please!!! I need help editing files with VBscript - 4/23/2006 12:18:24 AM   
  ZenIT

 

Posts: 6
Score: 0
Joined: 4/19/2006
Status: offline
Thanks ehvbs!

I will try it out to see if it will do the trick. As for the suggestion from others, creating different ini files for 300+ users is not an option. Thanks to everyone for their input.

ZenIT

Zenworks (R.I.P)

(in reply to Country73)
 
 
Post #: 6
 
 RE: Please!!! I need help editing files with VBscript - 4/27/2006 1:01:01 AM   
  ZenIT

 

Posts: 6
Score: 0
Joined: 4/19/2006
Status: offline
Got it from one of the local script gods at work....


On error resume next
Const ForReading = 1
Const ForWriting = 2
Set objADSI  = CreateObject("AdSystemInfo")
Set objUserInfo  = GetObject("LDAP://" & objADSI.UserName)
strUserSAM   =  objUserInfo.samAccountName
strUserEmail   =  objUserInfo.EmailAddress
strFromAddress  = "FromAddress="
Set objFSO   =  CreateObject("Scripting.FileSystemObject")

Set objRegEx   =   New RegExp
With objRegEx
.Pattern  =  strFromAddress & ".*\r\n"
.IgnoreCase =  True
.Global  =  True
End With
Set objFile   =  objFSO.OpenTextFile("C:\Program_DIR\File.ini", ForReading)
strText = objFile.ReadAll
If objRegEx.Test(strText) Then
IF strUserEmail = "" Then
strUserEmail = "default@domain.com"
End If
strNewText = objRegEx.Replace(strText, strFromAddress & strUserEmail & vbcrlf)
End if
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\Program_DIR\File.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close
objFile.Close
wscript.quit


(in reply to ZenIT)
 
 
Post #: 7
 
 RE: Please!!! I need help editing files with VBscript - 10/10/2006 12:49:04 PM   
  ZenIT

 

Posts: 6
Score: 0
Joined: 4/19/2006
Status: offline
Update:

The script above works just fine. There is the odd time, now and then (one person per week), when a user would call in with a problem. It turns out the the problem is due to the ini file being empty. Before applying the script, I added an extra if statement to check to see if the ini file exists first before trying to edit it. In any case, this problem just started happening a couple of months ago. Anyone know why this would happen? I'm thinking that the user is opening the program (and the program reads the ini file) while the script is running and this causes it to write an empty file. Any thoughts?

Thanks in advance.

ZenIT

(in reply to ZenIT)
 
 
Post #: 8
 
 RE: Please!!! I need help editing files with VBscript - 10/10/2006 3:49:31 PM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ZenIT,

remove the evil "On Error Resume Next" from your script, start "the program", run the script. If
the script crashes at the

   Set objFile   =  objFSO.OpenTextFile("C:\Program_DIR\File.ini", ForReading)

line, do a local error handling like:

   On Error Resume Next
   Set objFile   =  objFSO.OpenTextFile("C:\Program_DIR\File.ini", ForReading)
   If 0 < Err.Number Then
       On Error GoTo 0
        MsgBox "can't edit ini file"
    Else
       On Error GoTo 0
       ' your code to edit the ini file
    End If

I don't expect that the script crashes at the 

   Set objFile = objFSO.OpenTextFile("C:\Program_DIR\File.ini", ForWriting)

line, but I may be wrong.

If the script doesn't crash at all, you are out of luck (wrong hypothesis)

Good luck!

(in reply to ZenIT)
 
 
Post #: 9
 
 
 
  

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 >> Please!!! I need help editing files with VBscript 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