Hier ist eine Antwort, die auf der Antwort von Tester101 basiert (und meiner Meinung nach eine Verbesserung darstellt), ausgedrückt als Unterprogramm, mit der CopyFile-Zeile nur einmal statt dreimal, und darauf vorbereitet, den Dateinamen zu ändern, während die Kopie erstellt wird (kein fest kodiertes Zielverzeichnis). Ich habe auch festgestellt, dass ich die Zieldatei vor dem Kopieren löschen musste, damit das funktioniert, aber das könnte ein Problem von Windows 7 sein. Die WScript.Echo-Anweisungen sind, weil ich keinen Debugger hatte und können natürlich entfernt werden, wenn gewünscht.
Sub CopyFile(SourceFile, DestinationFile)
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
Dim wasReadOnly
wasReadOnly = False
If fso.FileExists(DestinationFile) Then
'Check to see if the file is read-only
If fso.GetFile(DestinationFile).Attributes And 1 Then
'The file exists and is read-only.
WScript.Echo "Removing the read-only attribute"
'Remove the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
wasReadOnly = True
End If
WScript.Echo "Deleting the file"
fso.DeleteFile DestinationFile, True
End If
'Copy the file
WScript.Echo "Copying " & SourceFile & " to " & DestinationFile
fso.CopyFile SourceFile, DestinationFile, True
If wasReadOnly Then
'Reapply the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
End If
Set fso = Nothing
End Sub