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?

5 Answers 5

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

38
Const WindowsFolder = 0

Const SystemFolder = 1

Const TemporaryFolder = 2

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

Dim tempFolder: tempFolder = fso.GetSpecialFolder(TemporaryFolder)
2
  • 3
    Fabien's answer was correct as is this one, however, good documentation makes it easier for someone else to read.
    – Dscoduc
    Feb 7, 2009 at 16:02
  • this gets me C:\windows. Fabien's answer gets me the right folder R:\Temp
    – RASG
    Dec 3, 2013 at 14:32
14

Another possibility:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")
3
  • Thanks, Patrick, I missed pasting a line, but that is better.
    – Fionnuala
    Jan 8, 2009 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 ;) Jan 8, 2009 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 4, 2016 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

2
  • You can describe more detail.
    – aircraft
    Nov 21, 2017 at 5:32
  • 3
    Actually, the links are not very useful and this should be a comment.
    – wp78de
    Nov 21, 2017 at 5:46
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.