Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a file but permissions denied error

Status
Not open for further replies.

DougInCanada

Technical User
Feb 1, 2004
98
CA
I'm trying to run a script that will delete files in a specific folder based on the date the file was created:

[tt]Set fso = CreateObject("scripting.filesystemobject")

'on error resume Next

dttoday = Date
tmnow = Time
dtold = DateAdd("d", -30, dttoday) 'files older than 30 days will be deleted
Set LogFile = fso_OpenTextFile("C:\scripts\FilePurge.log", 2, True)
LogFile.WriteLine ("Started --" & Date & " " & Time)
Set fldr = fso.GetFolder("C:\Users\svc_protect\AppData\Local\Temp")

Set files = fldr.Files
LogFile.WriteLine(fldr.path)

for each file in files
if file.datecreated < dtold then
LogFile.WriteLine(" DELETED - " & file.name & " Date Created: " & file.datecreated)
file.delete
end If
Next

set subfolders = nothing
set files = Nothing


LogFile.WriteLine ("Ended --" & Date & " " & Time)[/tt]

The problem is that I get a'permissions denied' error on the file.delete line.

The user that I'm running the script under has full access to the folder and the underlying files.

Is there any way to force the deletion?
 
If the file is in use by another application, then you won't be able to delete it.
 
I would do a test with the above script, but change the source and destination to my documents.

I had a similar issue, tried amending the script and nothing. Tested it in My documents and it worked.

So my issue was due to permissions / UAC and not the actual script.

Also when deleting, I think you can use the command TRUE to confirm deletion.

Example -
file.delete TRUE

Someone would have to confirm the above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top