Search code examples
vbscript

trying to put a variable into a command


I'm trying to get the user and use it in a line of code so I can send it to my friend and they can also use it but I'm getting the error "path not found"

dim strUser
strUser = CreateObject("WScript.Network").UserName

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.CopyFile "C:\Users\[strUser]\Downloads\startup.vbs", "C:\Users\[strUser]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"

I thought that this would work so [strUser] gets seen as the user found by the code


Solution

  • Use the string concatenation operator (&) and your variable:

    filesys.CopyFile "C:\Users\" & strUser & "\Downloads\startup.vbs", "C:\Users\" & strUser & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
    

    You can also get the User folder this way:

    Set objShell = CreateObject("WScript.Shell")
    strUser = objShell.ExpandEnvironmentStrings("%USERPROFILE%")