Tim Wilmsen
Tim Wilmsen

Reputation: 33

How to get a path with the variable user in VBscript

I need to save, move and delete a file. But when I do that I want to let it save my files in the documents of the user that is logged on.

Here is my code:

Set oWS = WScript.CreateObject("WScript.Shell")
' Get the %userprofile% in a variable, or else it won't be recognized
userProfile = oWS.ExpandEnvironmentStrings( "%userprofile%" )


objDoc.SaveAs("userprofile & "\Downloads\test.doc")
objWord.Quit

Const strFolder = "userprofile & "\Downloads\System Information\", strFile = "userprofile & "\Downloads\test.doc"
Const Overwrite = True
Dim oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject")

If Not oFSO.FolderExists(strFolder) Then
  oFSO.CreateFolder strFolder
End If

oFSO.CopyFile strFile, strFolder, Overwrite

oFSO.DeleteFile("userprofile & "\Downloads\test.doc")

Upvotes: 2

Views: 7473

Answers (1)

Helen
Helen

Reputation: 97629

In VBScript, you can't use string concatenation (&) when defining constant values. Use variables instead.

Also, you have an extra quote before the userprofile variable name.

Here's the fixed code:

...
objDoc.SaveAs(userprofile & "\Downloads\test.doc")

Dim strFolder : strFolder = userprofile & "\Downloads\System Information\"
Dim strFile : strFile = userprofile & "\Downloads\test.doc"

...
oFSO.DeleteFile(userprofile & "\Downloads\test.doc")

Upvotes: 1

Related Questions