2014-02-27 31 views
1

sry guys m新編程。我試圖製作一個vbscript來處理從當前位置到系統啓動文件夾的vbs文件。但是m得到錯誤的文件名或數字。但是當我手動給路徑時,它就像一個魅力。我的代碼自我挑選的路徑也是正確的。不能明白是什麼問題。請幫幫我。這是我的代碼。錯誤的文件名或編號: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 
+0

哪條線是發生錯誤? – aphoria

+0

最後一個複製語句。文件不存在的情況。 – Bilal

+0

我已經評論了一個const變量語句。如果我使用這種說法,它工作得很好。 – Bilal

回答

0

好的。所以它應該看起來像這樣:

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 
+0

我將其更正爲: fso.CopyFile SourceFile,strMyPath,True – Bilal

+0

嘗試上面的修改代碼。 – Rich

+0

Thnx很多配偶......現在工作得很好...... – Bilal

1

爲了

If fso.FileExists(strMyPath) Then 

'工作',strMyPath必須包含一個有效文件規範。據我所見,在你的代碼中它包含了(destination?)文件夾的路徑。

使用正確命名的變量(名稱)可以明確它們是否保留文件夾或文件規格。

+0

你的目標文件夾。 – Bilal

+0

如果它是您嘗試驗證的目標文件夾存在,爲什麼不使用「.FolderExists()」方法? – Rich

+0

正如我所說的新的。對功能沒有太多的瞭解。 – Bilal