Link to home
Start Free TrialLog in
Avatar of hcdev
hcdev

asked on

Script to delete folders which are 30 days older than current date

Hi,

I need a script which shall purge folders which are older than 30 days from the current date.
Hence for example if today is 08/22/2007 then anything which is before 07/23/2007 should be deleted.
The script needs to take account of february (leap year), 31 days etc.
An actual example shall be helpful.
The script needs to be in korn shell.
The servers on which the files are located are SUN OS Servers.

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hcdev
hcdev

ASKER

Hi Tinitin,

The folders shall be containing files. Hence the folders including the files within the folder need to be deleted. The folder names are the same as that of the day they were created
Example: "08232007" which is a folder that got created on aug 23 2007. A folder is created everyday
to hold the reports which are html files. There is need to keep folders which are only 30 days old from the current date.

Hence only folders which have names 08232007 --> 07242007. Anything before that needs to be deleted.

Will your solution help delete
a. The files including the folder
b. Take care of leap years etc.

It is best to take the name of the folder to match the 30 day window. Anything other than that needs to be deleted.
Avatar of hcdev

ASKER

Hi Tintin,

One more thing, there is a folder named "xyz" which should not be deleted even if it is older than 30 days. Hence this folder needs to be filtered out in the result. It resides in the same path as the folders which need to be deleted
Tintn's suggestion improved according later added restriction;-)

find /path/to/dirs -mtime +30 -type d -a -not -name xyz -depth -exec rm -rf {} \;

# Note: -not depends on version of find, using ! (escaped as \! depending on your shell) should work always
Avatar of hcdev

ASKER

Hi Tintin and ahoffmann

Thanks for yur solutions. I am going to accept both your answers. However I have one final question.
Is there a way that we can check the name of the files which meet the criteria to be of a certain format?
Hence the name should not be = xyz and be all numbers.

This is to ensure that files do not get deleted by mistake.

Thanks in advance
How often are files written to the created directory?  If the files are only written to the directory on the day it is created, then ahoffmann's suggestion should work just fine.

Note that Solaris doesn't support the -not flag to find, so use ! instead (as mentioned by ahoffmann).

What I'd do first is verify that it picks up the correct directories by doing

find /path/to/dirs -mtime +30 -type d -a ! -name xyz -depth

If that all looks good, then add the

-exec rm -rf {} \;

to the end.
Avatar of hcdev

ASKER

It works like a charm.

However I initially asked if there is a way I can check that the syntax of the folders matches a certain format before deleting?

Hence the folder NAME needs to
1. Not = XYZ
2. Equal to all digits
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I guess you can omit the
  -a ! name XYZ
part now, for obvious reason ;-)
Good point ahoffmann.  Didn't even occur to me.  :-)
Avatar of hcdev

ASKER

Hey,

I am having trouble running this from the script. This works fine from the command line , however
when I run the script the command does not  return anything.

This is what I have in the script

echo "In delete part\n"
echo "server is $WLS_RPT_DIR\n"

OLD_RPTDIR=`find $WLS_RPT_DIR -mtime +140 -type d -name "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" -depth`

echo "Deleting file ${OLD_RPTDIR}\n"

for OLDRPTDIR in `find $WLS_RPT_DIR -mtime +140 -type d -name [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" -depth`
do

            echo "Deleting file $OLDRPTDIR\n"
            mv "$OLDRPTDIR" "${OLDRPTDIR}_bak"
done

when I run this nothing is assigned to $OLDRPTDIR
The problem is that you haven't defined WLS_RPT_DIR anywhere in the script.
Hi

Try below script . I found it in internet . I tested it and it works to delete the folders without deleting the parent folder .


On error resume next

Dim Directory
Dim Noofdays
Dim FSO
Dim FSO2
Dim LogFile
Dim Folderlist
Dim folders

Directory ="\\server\share\folder\subfolder\"
Noofdays=cint(30)
LogFile="\\server\share\folder\subfolder\deleted.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO2 = CreateObject("Scripting.FileSystemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
'
If oFSO.FileExists(Logfile) Then
Const ForAppending = 8
Set ofile = oFSO.OpenTextFile(LogFile, ForAppending, True)
Else
Set oFile = oFSO.CreateTextFile(logfile, true)
End If
ofile.writeline "Delete Folders older than 30 days Started   --> " & now()

Set Folderlist = FSO.GetFolder(Directory)

Set folders = Folderlist.SubFolders
For Each d In Folders
'          msgbox d.name
'          msgbox d.size
'          msgbox d.dateCreated
'          msgbox d.dateLastModified
'          msgbox d.dateLastAccessed
tempdirectory = Directory & d.name

If  datediff("d",d.dateCreated,now()) > Noofdays Then
FSO2.DeleteFolder(tempdirectory )
ofile.writeline "Deleting Folder...." & tempdirectory

if err.number <>0 then
ofile.writeline cstr(now()) & "    " & Err.description
err.clear
end if
End If
Next
ofile.writeline "Delete Folders older than 30 days Completed --> " &now()
ofile.writeline "--------------------------------------------"
ofile.close
gilib

How does a VB script help in this instance?