错误的文件名或数字:vbs错误

时间:2014-02-27 15:38:19

标签: vbscript

对于编程人员来说,他们是个新人。我试图制作一个vbscript来处理从当前位置到系统启动文件夹的vbs文件。但是我得到错误的文件名或号码。但是当我手动给出路径时,它就像一个魅力。我的代码自行选择的路径也是正确的。不明白是什么问题。请帮我。这是我的代码。

Set objShell = Wscript.CreateObject("Wscript.Shell")
strMyPath = objShell.SpecialFolders("Startup")
wscript.echo strPath
wscript.echo strMyPath
'Const strMyPath = "C:\Users\Bilal\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
Const SourceFile = "abc.vbs"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then
    'Check to see if the file is read-only
    If Not fso.GetFile(strMyPath).Attributes And 1 Then 
        'The file exists and is not read-only.  Safe to replace the file.
        fso.CopyFile SourceFile, strMyPath, True
    Else 
        'The file exists and is read-only.
        'Remove the read-only attribute
        fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
        'Replace the file
        fso.CopyFile SourceFile, strMyPath, True
        'Reapply the read-only attribute
        fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
    End If
Else
    'The file does not exist in the destination folder.  Safe to copy file to this folder.
    fso.CopyFile SourceFile, myStrPath, True
End If
Set fso = Nothing

2 个答案:

答案 0 :(得分:1)

制作

If fso.FileExists(strMyPath) Then

'work',strMyPath必须包含有效的文件规范。据我所知,在你的代码中它包含了(目标?)文件夹的路径。

使用正确命名的变量(名称)来明确它们是否包含文件夹或文件规范。

答案 1 :(得分:1)

确定。所以看起来应该是这样的:

   Set objShell = Wscript.CreateObject("Wscript.Shell")
   strPath = objShell.SpecialFolders("Startup")
   strMyPath = strPath&"\"  
   Const SourceFile = "abc.vbs"
   strMyPath = strMyPath & SourceFile
   Set fso = CreateObject("Scripting.FileSystemObject")

    'Check to see if the file already exists in the destination folder
    If fso.FileExists(strMyPath) Then

    'Check to see if the file is read-only
    If Not fso.GetFile(strMyPath).Attributes And 1 Then 
        'The file exists and is not read-only.  Safe to replace the file.
        fso.CopyFile SourceFile, strMyPath, True
    Else 
        'The file exists and is read-only.
        'Remove the read-only attribute
        fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
        'Replace the file
        fso.CopyFile SourceFile, strMyPath, True
        'Reapply the read-only attribute
        fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
    End If
Else
    'The file does not exist in the destination folder.  Safe to copy file to this folder.
    fso.CopyFile SourceFile, strMyPath, True
End If
Set fso = Nothing