如何用VBS备份文本文件

How to backup a text file with VBS

我在第 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" 

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

您还有其他线路有同样的问题。