Search code examples
vbscriptasp-classicdelete-filefilesystemobject

fs.DeleteFile(var) not deleting file


On a few of our sites we are running a backup script that saves a MSSQL database as a .bak file in a that directory.

Every day we run a Git style backup so that we have any changes to the site. Due to variance in backup timing We don't really want to delete today's backup, or yesterday's, so we will delete 3 days past.

The following .asp page runs fine if the DeleteFile(line 30) is commented out. if the file exists it follows the correct branch, so it can see the file, but if the delete line is uncommented it breaks.

<%@LANGUAGE="VBScript" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Purge</title>
</head>
<body>
    <%
        'Var init
        dim Month, Day, Year, Dir, File, Absolutepath, Period, fs
        Set fs=Server.CreateObject("Scripting.FileSystemObject")

        'setting date of backup for deletion to three days ago
        Period = dateadd("D", -3, Date)
        Month  = datepart("M", Period)
        Day    = datepart("D", Period)
        Year   = datepart("YYYY", Period)

        'location setting
        Dir = Server.MapPath(".") & "\"
        File= "dbBackup-" & Month & "-" & Day & "-" & Year & "_1.bak"
        Absolutepath = Dir & File

        'Actual attempt to delete
        If fs.FileExists(Absolutepath) Then
            fs.DeleteFile(Absolutepath)
            Response.Write("&#34;" & File & "&#34; deleted <br/>") 
        Else
            Response.Write("404: &#34;" & File & "&#34; missing <br/>") 
        End If
    %>
    Execution complete.
</body>
</html>

I have tried

'fs.DeleteFile(Absolutepath)

and

'fs.DeleteFile Absolutepath

and

'fs.DeleteFile(File)

and

'fs.DeleteFile File

To no avail. any ideas?

edit:

local server says 500 =

Microsoft VBScript compilation error '800a0414' 

Cannot use parentheses when calling a Sub 

/test/index.asp, line 23 

fs.CreateTextFile(Absolutepath,True)

Solution

  • The Answer ended up being that fs.DeleteFile does not take parenthesis

    fs.DeleteFile(Absolutepath)
    

    ended up being

    fs.DeleteFile Absolutepath