24

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?

Fabien
  • 6,700
  • 7
  • 35
  • 35

5 Answers5

50
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...

Fabien
  • 6,700
  • 7
  • 35
  • 35
38
Const WindowsFolder = 0

Const SystemFolder = 1

Const TemporaryFolder = 2

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

Dim tempFolder: tempFolder = fso.GetSpecialFolder(TemporaryFolder)
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 3
    Fabien's answer was correct as is this one, however, good documentation makes it easier for someone else to read. – Dscoduc Feb 07 '09 at 16:02
  • this gets me `C:\windows`. Fabien's answer gets me the right folder `R:\Temp` – RASG Dec 03 '13 at 14:32
14

Another possibility:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • Thanks, Patrick, I missed pasting a line, but that is better. – Fionnuala Jan 08 '09 at 15:00
  • No problem. I was just about to paste my own, almost identical answer, when I saw you beat me by a second...again ;) – Patrick Cuff Jan 08 '09 at 17:07
  • I like this one best because it's just as easy to use as `GetSpecialFolder`, but much less obscure. At least, for anyone who has ever worked with DOS or Windows environment variables AT ALL, this one is very intuitive and self-documenting. – John Y Feb 04 '16 at 20:07
0

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

wp78de
  • 18,207
  • 7
  • 43
  • 71
Makaveli84
  • 453
  • 6
  • 16
0

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
COG
  • 291
  • 2
  • 12