如何使用VBS备份文本文件

时间:2015-05-28 15:34:03

标签: vbscript

我在第22行收到权限被拒绝错误,但我认为这是因为我不是管理员,我已根据以下建议对此进行了更新。

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" 

1 个答案:

答案 0 :(得分: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

您还有其他行有同样的问题。