# Solved: VBScript Delete File



## computerman29642 (Dec 4, 2007)

I have a VBScript that copies and pastes a file when clicked. When the file is pasted, the name of the file changes to something like "FileName_CurrentDate" (CurrentDate would be the today's date). If I run the VBScript tomorrow, and new file is created while the old file remains.

Is there a way to modify teh VBScript to delete the old file?


----------



## computerman29642 (Dec 4, 2007)

Here is the code that I have so far


```
Dim fso
Dim strDate
Dim strOldFile

set fso = CreateObject("Scripting.FileSystemObject")

strOldFile = "C:\Test\"

For Each file In fso.GetFolder(strOldFile).Files
file.delete
Next
set fso = nothing

strDate = Right("0" & DatePart("m",Date), 2) & "-" & Right("0" & DatePart("d",Date), 2) &  "-" & DatePart("yyyy",Date)

set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\Databasename.mdb","C:\Backup\DatabaseName_" & strDate & ".mdb"
set fso = nothing
```
Is there a better way to perform this operation?


----------



## StumpedTechy (Jul 7, 2004)

So your saying from the example you posted you want C:\backup to only contain the latest mdb? Then realy the only thing you have to do is change C:\Test to C:\Backup.


----------



## computerman29642 (Dec 4, 2007)

Thanks.


----------

