VBScript错误:找不到路径

时间:2012-03-12 08:42:03

标签: vbscript hta

我在VBScript中遇到问题。运行此脚本时出错。它说“未找到路径”。

<SCRIPT Language="VBScript"> 
Option Explicit    
dim filesys, filetxt 
Const ForReading = 1, ForWriting = 2, ForAppending = 8  
Set filesys = CreateObject("Scripting.FileSystemObject") 
If not filesys.FileExists("%USERPROFILE%\CAD_Kunde\CAD_Kunde.txt") Then 
    Set filetxt = filesys.OpenTextFile("%USERPROFILE%\CAD_Kunde\CAD_Kunde.txt", ForWriting, True)  
    filetxt.WriteLine ("1")
 filetxt.Close 
End If

</script>

1 个答案:

答案 0 :(得分:0)

解决此问题的步骤。 (我可以在oneliner中给出解决方案,但是 fun 在那里学习什么?)

  1. 显然,Scripting.FileSystem COM无法识别%USERPROFILE%。所以我们必须用适当的值手动替换它。
  2. %USERPROFILE%是environment variable。可以通过Shell对象的ExpandEnvironmentStrings方法检索系统变量。因此,您必须使用以下内容创建shell对象:Set WShell = CreateObject("Wscript.Shell")
  3. 现在,您可以在Wshell对象上调用ExpandEnvironmentsStrings方法。它返回用户配置文件字符串
  4. 使用路径中返回的字符串。