Get the current temporary directory path in VBScript?

后端 未结 5 554
南旧
南旧 2020-12-29 06:09

The VB trick to get the path of the current temporary directory:

Private Declare Function GetTempPath Lib \"kernel32\" Alias \"GetTempPathA\" (ByVal nBufferL         


        
相关标签:
5条回答
  • 2020-12-29 06:37
    Const WindowsFolder = 0
    
    Const SystemFolder = 1
    
    Const TemporaryFolder = 2
    
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim tempFolder: tempFolder = fso.GetSpecialFolder(TemporaryFolder)
    
    0 讨论(0)
  • 2020-12-29 06:43
    WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)
    

    It took me a while to find it on Google. So for the next one looking for the same as me...

    0 讨论(0)
  • 2020-12-29 06:47

    Based entirely on AnthonyWJones' answer, here is my solution:

    Public Enum SpecialFolder
        WindowsFolder = 0
        SystemFolder = 1
        TempFolder = 2
    End Enum
    
    Public Function GetFolder(folder As Integer) As String
        Dim objFSO  As Object
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        GetFolder = objFSO.GetSpecialFolder(folder)
    End Function
    

    So, for example, you'd use GetFolder(TempFolder) to obtain the pathname of the user's temp folder.

    0 讨论(0)
  • 2020-12-29 06:59

    Another possibility:

    CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")
    
    0 讨论(0)
  • 2020-12-29 07:02

    You can also keep using the GetTempPath API. It's a bit tricky to call APIs from vbscript though. Here are some pointers on how to make Win32 API calls from vbscript:

    Reference 1

    Reference 2

    Reference 3

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