2014-01-21 56 views
1

我需要保存,移动和删除文件。但是当我这样做时,我想让它将文件保存在登录用户的文档中。如何在VBscript中使用变量用户获取路径

这里是我的代码:

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

什么* *正好与此代码的问题?你卡在哪里? – Helen

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

谢谢海伦!我错误地将变量声明为一个常量,并且我正在使用'+'来连接而不是'&' – joshgoldeneagle

相关问题