-1

I have script that edits the a line in the ini file, which sits on users %Appdata% folder i.e C:\Users\<>\AppData\Roaming. The current script which I have only edits a file pointing to proper file location, but I would like to have script which can edit the file on every logged on users folder
I have a vbs below which look like this , but I am not able to use a variable %appdata% to edit the file under folder when the user is logged on

Const ForReading = 1
Const ForWriting = 2
Dim strUserName, CurrDir

Set objFSO = CreateObject("Scripting.FileSystemObject")

strUserName = InputBox("Please enter your email address below in the following format:" & Vbnewline & "firstname_lastname@test.com" & Vbnewline & Vbnewline &  "HINT - If you are unsure, you can look up your name", "Add internet email address")

If strUserName = "" Then
    Wscript.Quit
End If

Set objTextFile = objFSO.OpenTextFile("H:\appdata\Linkpoint360\LinkPointConfig.ini", ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline

    intLineFinder = InStr(strNextLine, "UserEMailAddress")
    If intLineFinder <> 0 Then
        strNextLine = "UserEMailAddress=" & strUserName
    End If

    strNewFile = strNewFile & strNextLine & VbCrLf
Loop

objTextFile.Close

Set objTextFile = objFSO.OpenTextFile("H:\appdata\Linkpoint360\LinkPointConfig.ini", ForWriting)

objTextFile.WriteLine strNewFile
objTextFile.Close

I am not scripting expert, but I have tried best to find a suitable solution over the internet and I have no luck If someone can please edit this vbs and give a proper script, that will be really appreciated

@ Ansgar Wiechers, can't post the image as i don't have 10 repuataion, but here is what I get in pop box:

  • Script: << Location of file >>
  • Line: 13
  • Char: 1
  • Error: Path not found
  • Code: 800A004C
  • Scource: Microsoft VBScript runtime error

the error I get when is use %appdata% in my script. from the above code I have just edited file location "H:\appdata...." to "%appdata%....."

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    Why can't you use `%APPDATA%`? Does the variable point to the wrong location? Or is the file locked when the user is logged on, so you can't modify it? – Ansgar Wiechers Jan 19 '15 at 08:43
  • I did give it a go with %APPDATA% but it gave me a error , I not scripting expert. I don't know how to define %APPDATA% in the VBS script – Sabi Swaminathan Jan 20 '15 at 20:18
  • Well, if you want someone to help you with the error, you need to *show* the error first. As far as `%APPDATA%` is concerned: you don't need to define the variable yourself. It's an automatic environment variable that the operating system maintains for you. – Ansgar Wiechers Jan 20 '15 at 22:13
  • I can't add a image as of yet, but I have placed error in bullet point above. thanks – Sabi Swaminathan Jan 21 '15 at 01:56

2 Answers2

1

FileSystemObject methods don't expand environment variables. You need to do it yourself, e.g. like this:

...
Set sh = CreateObject("WScript.Shell")
config = sh.ExpandEnvironmentStrings("%APPDATA%\Linkpoint360\LinkPointConfig.ini")
Set objTextFile = objFSO.OpenTextFile(config, ForReading)
...
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

You can't reliabily do this in vbscript.

However you can make a safe assumption (disregarding network and profile updating issues that I don't think will matter) that profiles are under Users folder and each user will have the same relative path to AppFolder.

The normal way of handling this problem type is to use logon scripts.

Serenity
  • 29
  • 1