Been sometime since I've posted a question, so I hope everyone can help out.

I am trying to run a script that is running successfully on another server to delete specific files. However, when I modify the script to delete .bak and .trn files from a SQL server, it doesn't want to work. The script is below:

For Each objFile In kf
If fso.GetExtensionName(objFile) = "bak" or "trn" then
If objFile.DateCreated < date - 3 Then
WScript.Echo objFile & " " & objFile.DateCreated
fso.DeleteFile objfile
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

What am I missing here? I tried running the script with just "bak" defined and jest "trn" defined, the command window shows the script running successfully, but the files are still there. Is it because I'm trying to run the script from the C:\ drive and the files are on the E:\ drive?

Any help would be appreciated. Again, this script works great on another server deleting "txt" files older than 7 days.

Thank you for the advice so far. However, not working. The script runs, but it doesn't delete the files. And yes, I know there is built in SQL statements that will remove files older than a specific time frame. Those aren't working in these cases. So basically I'm trying to automate a process to removes those files that don't get automatically purged so I don't have to go in every couple of days and do it manually.

Keep the ideas coming.. something has to work. Maybe this is a problem on the SQL server, but that was the idea behind the script. Something else that only is supposed to care about file extensions and not what they go to.

We have a couple of SQL servers that can not talk to our NAS box because they are not on the domain. So the SQL server backs up locally. Then I have Robocopy fire up and copy the files over to the NAS box using domain credentials. Robocopy then purges any files on the NAS that are not in the SQL folder.

But because some of the maintenance plans are not purging the files correctly during the cleanup process. So that leaves either extra TRN's or BAK's on the server and the NAS as well.

Every few days I go into each server that I know doesn't cleanup properly and manually remove the files I don't need to save anymore. Then when the next Robocopy job runs it will clear out any extra files on the NAS.

So the idea behind this is to have this script run after the backup completes to remove any backups older then 3 days. Then Robocopy can run.

Hopefully this makes sense as to what I'm trying to accomplish.

So I know the syntax of the script is correct, but now we need to figure out why it won't find those files and delete them.

why don't you use the cleanup task maintenance plan to remove those files?

that's what they are there for

I mentioned in an earlier post concerning this. I do use that, but for some reason it doesn't work on these servers I am referring to. No explanation why, no clue as to why it doesn't cleanup. I have read through numerous posts and forums with possible solutions, but none of them have worked for these servers.

I thought it might have been a particular install of SQL, but it's happening with 2000 and 2005 installs. So believe me when I tell you that I have those cleanup tasks in the plans, they just don't work in all cases.

What's interesting is that in at least 1 of these servers the BAK files will cleanup, but not the TRN's. And some of those get pretty big.

Might actually have to wait a couple of days... the oldest file is 1/5, so it won't delete until Friday (unless you change the code to 2 days).

Thank you Martin. I realized that yesterday after I made the post and didn't feel like updating it. So we are in a waiting period for another day or so. Then I will report back and if all goes well, then I will close this out and give credit where credit is due.

I also had a problem with my cleanup tasks in SQL, after updateing SQL server the problem went away.

Are you on the lastest updates?

Probably not. But getting downtime to make those upgrades is very difficult in my business. I will look into that as a possibility. But for now it's only a small portion of my SQL servers that don't cleanup as they should, so it's manageable. I am just tired of doing it manually.