Use a variable in file path in .vbs

前端 未结 4 1964
醉话见心
醉话见心 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:12

    I guess you meant a script variable. Try this:

    logger = Inputbox("What is ID?","Domain ID")
    Call update("C:\Users\"& logger &"\AppData\Local\stat.ini", strOld, strNew)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 04:27

    You can use command line arguments with vbs. See the following technet site: http://technet.microsoft.com/en-us/library/ee156618.aspx

    using the example vbs at the bottom, you can have Ping.vbs reply based on the computer name entered after the script name when its called (C:\scripts\Ping.vbs Hostname)

    Here's more info on WScript.Aurguments https://www.google.com/search?q=WScript.Arguments&sourceid=ie7&rls=com.microsoft:en-us:IE-Address&ie=&oe=


    'Ping.vbs:
    Dim arArguments, strArgument
    Set arArguments = WScript.Arguments
    WScript.Echo WScript.Arguments.Count
    For Each strArgument in arArguments
    If Ping(strArgument) Then
        WScript.Echo strArgument & " is available."
    Else
        WScript.Echo strArgument & " is not available."
    End If  
    Next 
    Function Ping( myHostName )
    Dim colPingResults, objPingResult, strQuery
    strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & myHostName & "'"
    Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
    For Each objPingResult In colPingResults
        If Not IsObject( objPingResult ) Then
            Ping = False
        ElseIf objPingResult.StatusCode = 0 Then
            Ping = True
        Else
            Ping = False
        End If
    Next
    Set colPingResults = Nothing
    End Function
    

    0 讨论(0)
  • 2020-12-21 04:36

    If I understand what you're after correctly, you're either going to need to do a string concatenation where you build a string like "string part 1" & logger & "string part 2" or use the replace function to replace %Logger% (e.g. Replace(templateString, "%Logger%", logger)) with your logger variable. There's not a direct equivalent to the %1 sort of format used in batch files.

    0 讨论(0)
提交回复
热议问题