Steve
Steve

Reputation: 75

How to backup a text file with VBS

I am getting permission denied error on line 22 but I assume this is because I am not admin, I have updated this with the advice below.

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" 

Upvotes: 0

Views: 256

Answers (1)

aphoria
aphoria

Reputation: 20189

The FileExists method needs parentheses around the argument.

Your line 6 is this:

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

It needs to be this:

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

You have other lines that have the same problem.

Upvotes: 1

Related Questions