23
votes

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

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long 

fails in VBScript. So?

5

5 Answers

49
votes
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...

37
votes
Const WindowsFolder = 0

Const SystemFolder = 1

Const TemporaryFolder = 2

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

Dim tempFolder: tempFolder = fso.GetSpecialFolder(TemporaryFolder)
14
votes

Another possibility:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")
0
votes

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
votes

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.