Search code examples
for-loopvbscripteditiniappdata

Use a VBS to edit ini for users %appdata% folder


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%....."


Solution

  • 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)
    ...