Delete old files task

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Delete old files task

Aleš Vojáček
Hi all,
Is it possible to use some nant task to do this:
I need delete files with specific name NameOfFile*.zip which are older
then 1 month and in dir must be at least 3 files of that name.
My english is not so good so here is example.

1. I have 30 files of specific name pattern. 25 are older then one month
- that 25 file will be deleted, because there still will be 5 files
2. I have 30 files of specific name pattern. 29 are older then one month
- 27 oldest files will be deleted, because I need at least 3 files of
that pattern.

Hopefully you can understand me

Best Regards Ales

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
NAnt-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nant-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Delete old files task

Wilson, Brian
Hey,

I think you would need to use the <script> task. You can write the script to all the files in the directory, sort them by create date, and loop through them starting with the 4th item. Then check to see if the create date is 1 month or less from the current date. If it is 1 month or less, then skip it, else, delete it.



Brian Wilson



-----Original Message-----
From: Aleš Vojáček [mailto:[hidden email]]
Sent: Thursday, September 06, 2012 5:29 AM
To: [hidden email]
Subject: [NAnt-users] Delete old files task

Hi all,
Is it possible to use some nant task to do this:
I need delete files with specific name NameOfFile*.zip which are older then 1 month and in dir must be at least 3 files of that name.
My english is not so good so here is example.

1. I have 30 files of specific name pattern. 25 are older then one month
- that 25 file will be deleted, because there still will be 5 files 2. I have 30 files of specific name pattern. 29 are older then one month
- 27 oldest files will be deleted, because I need at least 3 files of that pattern.

Hopefully you can understand me

Best Regards Ales

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
NAnt-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nant-users

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
NAnt-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nant-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Delete old files task

Aleš Vojáček
Thank you,
I create a powerShell script which do that,
I hoped that there is some NANT task which can do thinks like that.
Anyway PowerShell script works fine.
Thank you
A.
Dne 6.9.2012 15:09, Wilson, Brian napsal(a):

> Hey,
>
> I think you would need to use the <script> task. You can write the script to all the files in the directory, sort them by create date, and loop through them starting with the 4th item. Then check to see if the create date is 1 month or less from the current date. If it is 1 month or less, then skip it, else, delete it.
>
>
>
> Brian Wilson
>
>
>
> -----Original Message-----
> From: Aleš Vojáček [mailto:[hidden email]]
> Sent: Thursday, September 06, 2012 5:29 AM
> To: [hidden email]
> Subject: [NAnt-users] Delete old files task
>
> Hi all,
> Is it possible to use some nant task to do this:
> I need delete files with specific name NameOfFile*.zip which are older then 1 month and in dir must be at least 3 files of that name.
> My english is not so good so here is example.
>
> 1. I have 30 files of specific name pattern. 25 are older then one month
> - that 25 file will be deleted, because there still will be 5 files 2. I have 30 files of specific name pattern. 29 are older then one month
> - 27 oldest files will be deleted, because I need at least 3 files of that pattern.
>
> Hopefully you can understand me
>
> Best Regards Ales
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> NAnt-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/nant-users
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> NAnt-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/nant-users


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
NAnt-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nant-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Delete old files task

Bob Archer
In reply to this post by Aleš Vojáček
> Hi all,
> Is it possible to use some nant task to do this:
> I need delete files with specific name NameOfFile*.zip which are older then 1
> month and in dir must be at least 3 files of that name.
> My english is not so good so here is example.
>
> 1. I have 30 files of specific name pattern. 25 are older then one month
> - that 25 file will be deleted, because there still will be 5 files 2. I have 30 files
> of specific name pattern. 29 are older then one month
> - 27 oldest files will be deleted, because I need at least 3 files of that pattern.
>
> Hopefully you can understand me
>
> Best Regards Ales

Here is my hazel target. I thought of creating it as a task... but have been too lazy. But, if someone wants to add the hazel task to Nantcontrib I don't have a problem with that. This clears leaving behind a certain number of files, but it could easily be changed to use age of file instead.

<target name="hazel">
    <property name="hazel.searchPattern" value="*" overwrite="false" />
    <script language="C#">
      <code><![CDATA[
        public static void ScriptMain(Project project)
        {
            string path = project.Properties["hazel.dirToClean"];
                        string searchPattern = project.Properties["hazel.searchPattern"];
                        string [] folders = System.IO.Directory.GetDirectories(path,searchPattern,SearchOption.TopDirectoryOnly);

            System.Collections.ArrayList dirlist = new System.Collections.ArrayList();
            foreach (string folder in folders)
            {
                dirlist.Add(new DirectoryInfo(folder));
            }
            dirlist.Sort(new DirectoryInfoDateComparer());
            dirlist.Reverse();

            for (int i = 5; i < dirlist.Count; i++)
            {
                string foldername = ((DirectoryInfo)dirlist[i]).Name;;
                try
                {
                    ((DirectoryInfo)dirlist[i]).Delete(true);
                    project.Log(Level.Info, String.Format("Deleted folder {0}", foldername));

                }
                catch (Exception e) { project.Log(Level.Warning, String.Format("Unable to delete folder {0}, {1}", foldername, e.Message)); }
            }
        }

        internal class DirectoryInfoDateComparer : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                DirectoryInfo di1 = (DirectoryInfo)x;
                DirectoryInfo di2 = (DirectoryInfo)y;

                return di1.CreationTime.CompareTo(di2.CreationTime);
            }
        }
          ]]></code>
    </script>
  </target>

Here is an example of how I would call it:

        <property name="hazel.dirToClean"    value="${InstallDir}" />
        <property name="hazel.searchPattern" value="CD ${version.major}_${version.minor}_${version.build}_${version.revision}_*" />
        <call target="hazel" />

BOb


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
NAnt-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nant-users
Loading...