Active Setup - appending line in file in Appdata

Have the following batch file:
    cscript Myvb.vbs "C:\Users\%username%\AppData\LocalLow\file.ini" "Things" "Stuff"

vb script file:
    Const ForReading = 1
    Const ForWriting = 2

    strFileName = WScript.Arguments(0)
    strFindText = WScript.Arguments(1)
    strNewText = WScript.Arguments(1) & vbCrLf & WScript.Arguments(2)

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

    strText = objFile.ReadAll
    strNewText = Replace(strText, strFindText, stfFindText&strNewText)

    Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
    objFile.Write strNewText

    Set objFile = Nothing

and batch file to set ActiveSetup:
    @echo off
    REM copy batch file
    xcopy Mybat.bat* %SYSTEMROOT%\
    xcopy Myvb.vbs* %SYSTEMROOT%\

    REM create active setup component to run batch file
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MySetup" /v "Version" /t REG_SZ /d "1" /f
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MySetup" /v "StubPath" /t REG_SZ /d "%SYSTEMROOT%\Mybat.bat" /f

I have need to add a line at a certain location with a file stored in AppData for all users. If mybat is executed manually, the file is edited fine. However, the file does not change after logging in. I undid the changes to the file and cleared the active setup registry entry in my profile between my tests.

Is there a permissions issue I am missing? Is %username% coming out to be not what I expect it to?

0 Comments   [ + ] Show comments

Answers (2)

Answer Summary:
Posted by: rad33k 3 years ago
Fourth Degree Brown Belt

Top Answer

If it works fine started manually then I would suggest to modify Mybat.bat to include full path to the VBS file i.e.

      cscript "%~dp0Myvb.vbs" "C:\Users\%username%\AppData\LocalLow\file.ini" "Things" "Stuff"
If this does not help I would suggest to modify StubPath to: CMD.EXE /K "%SYSTEMROOT%\MyBat.bat" - then you would see the error message if any occurred.

You could also consider to modify these files without ActiveSetup, for example:

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Users")

For Each User in oFolder.Subfolders
    strFilePath = "C:\Users\" &User.Name &"\AppData\LocalLow\file.ini"
    If oFSO.FileExists(strFilePath) Then
        WriteToLog "File: " &strFilePath &" does not exist."
    End If

  • ...Yes. Yes I should have included the full path. Thank you. - ChrisRoy 3 years ago
Posted by: anonymous_9363 3 years ago
Red Belt
This demonstrates the classic rookie mistake when coding.

Essentially, any code *must* assume that not a single step will execute without failing so the code needs to trap these failures. Always assume that *nothing* will work and code accordingly. 

In the above case, don't assume that the system will be able to locate the file. You should be able to assume from that that hard-coding paths is right up there in the pantheon of Really Bad Ideas so again, your code needs to work around that.
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ