如何在VBscript中获取变量user的路径

时间:2014-01-21 12:28:08

标签: variables vbscript path

我需要保存,移动和删除文件。但是当我这样做时,我想让它将我的文件保存在登录用户的文档中。

这是我的代码:

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")

1 个答案:

答案 0 :(得分:1)

在VBScript中,在定义常量值时,不能使用字符串连接(&)。改为使用变量。

此外,您在userprofile变量名称之前还有一个额外的引用。

这里是固定代码:

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