Solved

I am trying to delete all files in folders and subfolders older than 7 days from a share on the network

Posted on 2008-06-13
6
494 Views
Last Modified: 2011-10-03
We have just implemented a SAN solution and I am trying to run a script (that works on the local server) that looks to a share across the network to delete all files in folders and subfolders and I am receiving an error message stating:
Line:        16
Char:       11
Error:      Permission denied
Code:      800A0046
Source:   Microsoft VBScript runtime error

I am logged in as a domain admin and have full rights to the server that I am trying to kick it off on.

I have attached the script, however, if I run it on the local server and point to the drive letter it works fine.

Any help would be greatly appreciated.
Dim fso, startFolder, OlderThanDate
 

Set fso = CreateObject("Scripting.FileSystemObject")

startFolder = "\\sadvnfs01\Scan-DVN\" ' folder to start deleting (subfolders will also be cleaned)

OlderThanDate = DateAdd("d", -7, Date)  ' 7 days (adjust as necessary)
 

DeleteOldFiles startFolder, OlderThanDate
 

Function DeleteOldFiles(folderName, BeforeDate)

   Dim folder, file, fileCollection, folderCollection, subFolder
 

   Set folder = fso.GetFolder(folderName)

   Set fileCollection = folder.Files

   For Each file In fileCollection

      If file.DateLastModified < BeforeDate Then

          fso.DeleteFile(file.Path)

      End If

   Next
 

    Set folderCollection = folder.SubFolders

    For Each subFolder In folderCollection

       DeleteOldFiles subFolder.Path, BeforeDate

    Next

End Function

Open in new window

0
Comment
Question by:bob_kochanski
  • 2
  • 2
  • 2
6 Comments
 
LVL 2

Expert Comment

by:grahammj
ID: 21783735
Can you delete them by hand?  Make sure it isn't a share permission.  I would just give everyone full control for share permissions, and let NTFS really handle the other permissions.  If you can delete them by hand, then it might be a script issue (the error above seems to be a script issue).  Below you will find another 7 day delete script that I've given others on here and seem to work well.  Just change to how many days you want, and the path.  Please note that the code snippet app makes large areas appear to be commented it out, that is not the case if you cut and paste this script it should work fine.  See below:


Option Explicit

on error resume next

        Dim oFSO

        Dim sDirectoryPath

        Dim oFolder

        Dim oFileCollection

        Dim oFile

        Dim iDaysOld

 

'Customize values here to fit your needs

        iDaysOld = 7

        Set oFSO = CreateObject("Scripting.FileSystemObject")

        sDirectoryPath = "c:\files"

        set oFolder = oFSO.GetFolder(sDirectoryPath)

        set oFileCollection = oFolder.Files

 

'Walk through each file in this folder collection. 

'If it is older than 'x' days, then delete it.

        For each oFile in oFileCollection

                If oFile.DateLastModified < (Date() - iDaysOld) Then

                        oFile.Delete(True)

                End If

        Next

 

'Clean up

        Set oFSO = Nothing

        Set oFolder = Nothing

        Set oFileCollection = Nothing

        Set oFile = Nothing

Open in new window

0
 

Author Comment

by:bob_kochanski
ID: 21785013
Alright so I changed the script to match the days old and the path.  When I execute the script I don't get any errors, however, it's not doing anything.  I saved it as a .vbs script, right?  I have attached the changes that I made.
Option Explicit

on error resume next

        Dim oFSO

        Dim sDirectoryPath

        Dim oFolder

        Dim oFileCollection

        Dim oFile

        Dim iDaysOld

 

'Customize values here to fit your needs

        iDaysOld = 7

        Set oFSO = CreateObject("Scripting.FileSystemObject")

        sDirectoryPath = "\\sadvnfs01\Scan-DVN"

        set oFolder = oFSO.GetFolder(sDirectoryPath)

        set oFileCollection = oFolder.Files

 

'Walk through each file in this folder collection. 

'If it is older than '7' days, then delete it.

        For each oFile in oFileCollection

                If oFile.DateLastModified < (Date() - iDaysOld) Then

                        oFile.Delete(True)

                End If

        Next

 

'Clean up

        Set oFSO = Nothing

        Set oFolder = Nothing

        Set oFileCollection = Nothing

        Set oFile = Nothing

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 125 total points
ID: 21788242
Hi, from what I can tell, there's nothing wrong with your original script....perhaps there are more restrictive "share" permissions, over the "NTFS" permissions.  Usually, you just set the "share" permissions to Everyone at Full Control, and then restrict the access via the "NTFS" permissions on the folders' security tab.

Anyway, after checking that, I would also change this line:
         fso.DeleteFile(file.Path)

to this
         fso.DeleteFile(file.Path), True

which adds the second parameter to attempt to "force" the deletion.

Regards,

Rob.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:grahammj
ID: 21788535
Yeah, it sounds like a permissions issue then. Can you go to explorer and explore to \\sadvnfs01\Scan-DVN and try and delete the files manually?  Does that work correctly?  If not, then it is a permissions issue somewhere.  If you can, how are you running these vbs scripts?  By double clicking on them? or by scheduled job?  If by scheduled job then you'll need to put in a 'run as' account information into the scheduled job.
0
 

Author Closing Comment

by:bob_kochanski
ID: 31467097
That worked perfectly.  

Thanks again,
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21790164
No problem. Thanks for the grade.  Did you have to modify any permissions entries?

Regards,

Rob.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now