Use a variable in file path in .vbs

前端 未结 4 1972
醉话见心
醉话见心 2020-12-21 04:05

Is it possible to usa variable in a path in .vbs. My basic situation is I have a vbs script that will often be run on a computer with one person logged in and run by an admi

4条回答
  •  感动是毒
    2020-12-21 04:22

    This worked for me:

    Dim fs, ws, Path
    Set ws = CreateObject( "WScript.Shell" )
    Path = ws.ExpandEnvironmentStrings( "%UserProfile%\testfile.txt" )
    ws = Nothing
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.CreateTextFile (Path, True)
    f.WriteLine("This is a test")
    f.Close()
    f = Nothing
    

    Don't assume "C:\Users" will be valid on every system. There are times when you may want to use a different location for user profiles. I also looked at the %AppData% environment variable, but in my case that pointed to AppData\Roaming, and you want AppData\Local.

提交回复
热议问题