题
我需要创建一个名为“listfile.txt“在文件夹中 C: Documents and Settings 所有用户 Application Data NetApp System 所以我做了以下vbscript来acheive
Const CommonAppData = &H23& ' the second & denotes a long integer '
Const OSCPATH = "\netapp\system"
Dim fso, MyFile
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(CommonAppData)
Set objFolderItem = objFolder.Self
'MsgBox objFolderItem.Name & ": " & objFolderItem.Path
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("objFolderItem.Path & OSCPATH\listfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
但是没有发现它的投掷错误提及路径
** Windows脚本主机
脚本:c:文档和设置 puthuprf desktop test.vbs行:15 char:1错误:找不到路径,找到代码:800A004C
资料来源:Microsoft VBScript运行时错误
好的
---------------------------**
解决方案
脚本中的这一行不正确:
Set MyFile = fso.CreateTextFile("objFolderItem.Path & OSCPATH\listfile.txt", True)
要将变量和对象属性插入字符串中,您需要使用 &
操作员,这样:
Set MyFile = fso.CreateTextFile(objFolderItem.Path & OSCPATH & "\listfile.txt", True)
请注意,建议使用 BuildPath
结合路径多个部分的方法,因为它使您无法添加必要的路径分离器(\
)手动:
strFileName = fso.BuildPath(objFolderItem.Path, OSCPATH)
strFileName = fso.BuildPath(strFileName, "listfile.txt")
Set MyFile = fso.CreateTextFile(strFileName, True)
不隶属于 StackOverflow