VB脚本的等效%username%

时间:2018-04-17 16:49:58

标签: vbscript

我希望从当前用户的计算机中删除OfficeFileCache文件夹。目前我有..

Option Explicit
Dim obj : Set obj =CreateObject ("wscript.shell")
Dim fso : Set fso =CreateObject ("scripting.FileSystemObject")

obj.Run "taskkill /f /im msouc.exe.exe",0
obj.Run "taskkill /f /im msosync.exe.exe",0

fso.DeleteFolder "C:\Users\%username%\AppData\Local\Microsoft\Office\16.0\OfficeFileCache"

最后一部分是我遇到的麻烦。只是在VB脚本中寻找与用户名等价的东西。

2 个答案:

答案 0 :(得分:1)

如有疑问,请阅读documentation

  

ExpandEnvironmentStrings方法

     

返回环境变量的扩展值。

在你的情况下:

fso.DeleteFolder obj.ExpandEnvironmentStrings("C:\Users\%username%\AppData\Local\Microsoft\Office\16.0\OfficeFileCache")

答案 1 :(得分:0)

如果你正在使用像

这样的路径
C:\Users\%username%\AppData\Local\...

这样更好:

%LocalAppData%\...

所以

Fso.DeleteFolder Obj.ExpandEnvironmentStrings("%LocalAppData%\Microsoft\Office\16.0\OfficeFileCache")
如果用户将%UserProfile%设置为另一个目录,

会做得更好。