Change a line in a Text file

(imported topic written by Muntzie91)

I would like to change one line in this text file. I what to change

Username=User

to the currenly logged on user. What would be the easiest way? The name and location of the file is

C:\Program Files\Cisco Systems\VPN Client\PROFILES\USA primary.pcf

Thanks

main

Description=

Host=

AuthType=1

GroupName=grpuser

GroupPwd=

enc_GroupPwd=

EnableISPConnect=0

ISPConnectType=0

ISPConnect=Blackberry

ISPCommand=

Username=User

SaveUserPassword=0

UserPassword=

enc_UserPassword=

NTDomain=INFRA

EnableBackup=1

BackupServer=

EnableMSLogon=1

MSLogonType=0

EnableNat=1

TunnelingMode=0

TcpTunnelingPort=10000

CertStore=0

CertName=

CertPath=

CertSubjectName=

CertSerialHash=

SendCertChain=0

PeerTimeout=90

EnableLocalLAN=0

(imported comment written by BenKus)

Generally the way to do this type of activity is to create an action that reads the lines of the files, changes the appropriate line(s) and then re-write the files… for a particularly complicated example, see here: http://forum.bigfix.com/viewtopic.php?id=1483

Here is my shot at an action that would do this… I didn’t test it at all so be careful and test well:

// store the file location
parameter “filename” = “C:\Program Files\Cisco Systems\VPN Client\PROFILES\USA primary.pcf”

// iterate through the file replacing lines as necessary
appendfile {concatenation “%0d%0a” of ( if (it starts with “Username=”) then (preceding text of first “Username=” of it) & “Username=” & (name of current user) else it ) of lines of file(parameter “filename”)}
// backup the old file (first delete any old backup files)
delete "{parameter “filename”}.bak"
move “{parameter “filename”}” “{parameter “filename”}.bak”
// replace with the new file
move __appendfile “{parameter “filename”}”

Hope it helps,

Ben

(imported comment written by Muntzie91)

Ben

Thanks for the reply. I had looked at the more complicated example before making my post and I couldn’t figure out how to modify it for my own use. One other question. Can I substitute

(name of current user)

with

(if exists current user then name of current user else ((value “DefaultUserName” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon” of registry) as string))

in case a user is not logged in.

Thanks

(imported comment written by BenKus)

Yes… that looks solid to me… Of course you want to double check it to make sure it works properly…

Ben