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?

Answer Summary:
0 Comments   [ + ] Show Comments


Please log in to comment

Answer Chosen by the Author

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

Answered 08/19/2017 by: rad33k
Second Degree Green Belt

  • ...Yes. Yes I should have included the full path. Thank you.
Please log in to comment


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.
Answered 08/22/2017 by: VBScab
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity