我想要写一个VBS脚本,首先从文件path.txt中读取到一个路径 ,比如读取到"C:\Program Files (x86)\System Protect Software\" 然后,我要在这个路径下的子目录ShutdownScript下创建一个文件remotecomputer.txt,并写入字串test我的代码如下,但是为何老提示路径错误呢?是不是路径中有双引号呢?pathname="path.txt"
Set fso1 = CreateObject("scripting.FileSystemObject")
if (fso1.fileexists(pathname)) then
set ts1=fso1.opentextfile(pathname,1,true)
end if
strpath=ts1.readline
ts1.Closefilename=strpath+"\ShutdownScript\remotecomputer.txt"Set fso = CreateObject("scripting.FileSystemObject")
if (fso.fileexists(filename)) then
set ts=fso.opentextfile(filename,8,true)
ts.WriteLine ("test")
ts.Close
else
Set ts = fso.CreateTextFile(filename, True)
ts.WriteLine ("test")
ts.Close
end if

解决方案 »

  1.   

    你的目录是不是不存在,CreateTextFile 好像不会创建目录的
      

  2.   

    先把 pathname="path.txt" 这个路径写全试试看
      

  3.   

    把你的代码保存为a.vbs,再建立一个path.txt,其中的内容为:
    "C:\download\"
    在DOS下,用
    cscript //d a.vbs
    来调试你的代码,错误在这一行:
    Set ts = fso.CreateTextFile(filename, True)
    错误的文件名或号码
    主要是:
    filename ""C:\download\"\ShutdownScript\remotecomputer.txt" String剩下的应该不用说了吧
      

  4.   

    长路径短路径的问题
    vbs中对于有空格的路径需要转换成短路径
    WScript.Echo CreateObject("Scripting.FileSystemObject").GetFolder("C:\Program Files\Java").ShortPath
      

  5.   

    filename输出显示"C:\Program Files\System Protect Software\"shutdownScript\remotecomputer"
    我看不出filename有什么问题呀我支持
    但是结果确实“路径未找到”
      

  6.   

    那我换种问法,如何可以去掉我获取到的strpath路径中的双引号呢?
    strpath中,获取到的路径是"C:\Program Files\System Protect Software\"filename=strpath+“ShutdownScript\remotecomputer.txt”
    就是这句话中,如何先去掉strpath中的双引号,然后再相加
      

  7.   

    楼主,你的代码没有问题,能够读取文件中的目录,然后建立文件,写入内容。我试了没有出错信息。
    你检查strpath,也就是path.txt文件中第一行,不应该两端有双引号。
      

  8.   

    楼上,不行哦,我这个 path.txt的路径是直接从注册表读取的路径
    是自动的读取,我读取的就加了双引号所以我想问,如何能去掉这个双引号
      

  9.   

    呵呵。我google了一下,用下面的方法解决了,去掉了双引号,运行了一下,没有问题了
    strpath=mid(strpath,2,len(strpath)-2)