2015-05-28 15 views
0

我在第22行获得权限被拒绝的错误,但我认为这是因为我不是管理员,我已使用以下建议对此进行了更新。如何使用VBS备份文本文件

Const DestinationFile = "C:\Users\newtons\Desktop\Mock programs\Mock Backup" 
Const SourceFile = "C:\Users\newtons\Desktop\Text.config" 

Set fso = CreateObject("Scripting.FileSystemObject") 
'Check to see if the file already exists in the destination folder 
If fso.FileExists("C:\Users\newtons\Desktop\mockbackup") Then 
'Check to see if the file is read-only 
If Not fso.GetFile ("C:\Users\newtons\Desktop\Mock programs\MockBackup").Attributes And 1 Then 
    'The file exists and is not read-only. Safe to replace the file. 
    fso.CopyFile SourceFile, "C:\Users\newtons\Desktop\Mock programs\Mock Backup", True 
Else 
    'The file exists and is read-only. 
    'Remove the read-only attribute 
    fso.GetFile("C:\Users\newtons\Desktop\mockbackup.txt").Attributes = fso.GetFile(DestinationFile).Attributes - 1 
    'Replace the file 
    fso.CopyFile SourceFile, ("C:\Users\newtons\Desktop\Mock programs\Mock Backup"), True 
    'Reapply the read-only attribute 
    fso.GetFile(DestinationFile).Attributes = fso.GetFile("C:\Users\newtons\Desktop\mockbackup.txt").Attributes + 1 
End If 
Else 
'The file does not exist in the destination folder. Safe to copy file to this folder. 
fso.CopyFile SourceFile, ("C:\Users\newtons\Desktop\Mock programs\Mock Backup"), True 
End If 



MsgBox "Backup Created" ,0, "Backup Status" 
+0

什么_“我无法工作”_代表什么? – JosefZ

+0

编辑 - 我的歉意 – Steve

回答

1

FileExists方法需要围绕参数的括号。

你的第6行是这样的:

If fso.FileExists"C:\Users\newtons\Desktop\Mock programs\Mock Backup" Then 

它需要是这样的:

If fso.FileExists("C:\Users\newtons\Desktop\Mock programs\Mock Backup") Then 

您有多个具有相同问题的其他线路。

+0

此外,如果源是一个文件,但目标是一个文件夹,目标路径*必须*具有尾部反斜杠。 –

+0

当然它 - 谢谢+++尊重 – Steve