Windows Vista Forums

Delete based on date

  1. #1


    Woody UK Guest

    Delete based on date

    Hi

    I need a powershell script that can do the following :

    Look at a folder including subfolders and delete any .ppt files that
    are more then 7 days old. I need to delete the files plus the folders
    (if empty). I have a vbscript at the moment but have got stuck on how
    to only delete .ppt or delete the folder if empty. It works otherwise.

    Can anyone help ?

    Thanks


      My System SpecsSystem Spec

  2. #2


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    RE: Delete based on date

    I did not test it, because I don't collect .ppt files, but I think this
    should do the trick.

    Get-ChildItem -Recurse |
    Where-Object { ( ($_.LastWriteTime.Date -le (Get-Date).Date.AddDays(-7))
    -and ( ($_.name -like "*.ppt") -or ($_.PSisContainer -eq $true) ) )} |
    Remove-Item -Confirm

    You might also want to have a look at the official PoweShell blog:
    http://blogs.msdn.com/powershell/arc...Functions.aspx

    --
    greetings
    dreeschkind

    "Woody UK" wrote:

    > Hi
    >
    > I need a powershell script that can do the following :
    >
    > Look at a folder including subfolders and delete any .ppt files that
    > are more then 7 days old. I need to delete the files plus the folders
    > (if empty). I have a vbscript at the moment but have got stuck on how
    > to only delete .ppt or delete the folder if empty. It works otherwise.
    >
    > Can anyone help ?
    >
    > Thanks
    >
    >


      My System SpecsSystem Spec

  3. #3


    Woody UK Guest

    Re: Delete based on date

    Hi

    > Get-ChildItem -Recurse |
    > Where-Object { ( ($_.LastWriteTime.Date -le (Get-Date).Date.AddDays(-7))
    > -and ( ($_.name -like "*.ppt") -or ($_.PSisContainer -eq $true) ) )} |
    > Remove-Item -Confirm


    Thank you!, pleased you replied as I was no where near close to getting
    this correct. :-)
    I need to buy a book and do some reading on this one evening, just have
    to get something working today. Is there a good book / PDF out there
    which I can read to give me a total newbie guide?

    Powershell looks like it kicks arse, was a bit batch file person until
    seeing this.

    Four questions :-)

    a) I need modified date rather than LastWriteTime. Is there a
    LastModified param?
    b) I created the following structure to test

    root
    Folder1
    myfile1.ppt
    myfile2.ppt
    myfile1.ppt
    myfile2.ppt

    I ran your script and it deleted all .ppt however it did not delete
    folder1 even though it was empty. I do not want to delete the folder
    if other files are in there, only if empty.

    c) Is there a switch to stop it asking for confirmation on delete? When
    I run it asks me to confirm.

    d) What is the best way to create this as a shortcut? Copy your code to
    a text file and create a new shortcut which opens that text file using
    powershell ? It will not be a technical person - just want to create a
    "cleanup" shortcut on the desktop.

    e) Can I lock this script to only run in a specifaed path. ie c:\my
    folder\ At the moment if I ran this on C:\ by accident it would cause
    big problems :-)

    Thank you for your help.


      My System SpecsSystem Spec

  4. #4


    =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= Guest

    Re: Delete based on date

    "Woody UK" wrote:

    > Hi
    >
    > > Get-ChildItem -Recurse |
    > > Where-Object { ( ($_.LastWriteTime.Date -le (Get-Date).Date.AddDays(-7))
    > > -and ( ($_.name -like "*.ppt") -or ($_.PSisContainer -eq $true) ) )} |
    > > Remove-Item -Confirm

    >
    > Thank you!, pleased you replied as I was no where near close to getting
    > this correct. :-)
    > I need to buy a book and do some reading on this one evening, just have
    > to get something working today. Is there a good book / PDF out there
    > which I can read to give me a total newbie guide?


    A good starting point is wikipedia, it lists some books that will be
    released later this year. http://en.wikipedia.org/wiki/Windows_PowerShell
    You can download some parts of the other books for free.
    The MSH book by Andy Oakley is good to understand the basic concept of
    PowerShell but many of the exampels are outdated since the book is based on
    an older version of PowerShell.
    Some Microsoft pages:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    http://www.microsoft.com/windowsserv...l/default.mspx
    http://channel9.msdn.com/wiki/defaul...PowerShellWiki

    I already mentioned the official PowerShell blog, there are many more good
    blogs online:
    http://mow001.blogspot.com/
    http://monadblog.blogspot.com/
    http://mshforfun.blogspot.com/

    > Powershell looks like it kicks arse, was a bit batch file person until
    > seeing this.
    >
    > Four questions :-)
    >
    > a) I need modified date rather than LastWriteTime. Is there a
    > LastModified param?

    Get-Item C:\myfile.txt or Get-ChildItem C:\
    gives you objects which contain properties or members. One of those members
    is LastWriteTime, which is an object itself. There is also a LastAccessTime
    property which is also a datetime object. You can see all members (properties
    and methods) of an object by piping it to Get-Member:

    PS> Get-Item C:\myfile.txt | Get-Member
    PS> Get-ChildItem C:\ | gm #gm is an alias for Get-Member

    To get the only date of the LastWriteTime, you need to access the date
    member of the datetime object:

    PS> (Get-Item C:\myfile.txt).LastWriteTime.Date
    PS> (Get-Item C:\myfile.txt).LastWriteTime.Day
    PS> (Get-Item C:\myfile.txt).LastWriteTime.Year

    To see all members, again, just pipe the datetime object to Get-Member

    PS> (Get-Item C:\myfile.txt).LastWriteTime | gm
    PS> Get-Date | Get-Member

    > b) I created the following structure to test
    >
    > root
    > Folder1
    > myfile1.ppt
    > myfile2.ppt
    > myfile1.ppt
    > myfile2.ppt
    >
    > I ran your script and it deleted all .ppt however it did not delete
    > folder1 even though it was empty. I do not want to delete the folder
    > if other files are in there, only if empty.


    Oh I see, I think you will need to adjust the script.
    You can test if a directory is empty by comparing the number of ChildItems
    with zero.

    PS> @(Get-ChildItem C:\windows).count -eq 0
    False

    (The @() syntax is used to force PowerShell to treat the result of
    Get-ChildItem as an array, even if only one or no items are returned.)

    > c) Is there a switch to stop it asking for confirmation on delete? When
    > I run it asks me to confirm.


    I added the "-confirm" parameter to the Remove-Item cmdlet to protect you
    from your self ;-) If you are sure that you know what you're doing then
    just remove it.

    > d) What is the best way to create this as a shortcut? Copy your code to
    > a text file and create a new shortcut which opens that text file using
    > powershell ? It will not be a technical person - just want to create a
    > "cleanup" shortcut on the desktop.


    By default ps1 scripts are not connected with powershell.exe for security
    reasons. You can do that manually. The easiest way may be to start your
    script from inside an batch file by calling powershell.exe and providing the
    script to launch as a parameter:

    #############
    runscript.bat
    #############
    C:\blabla\powershell.exe -command "C:\do-foo.ps1"
    #############

    Type powershell -? to see all available parameters.

    > e) Can I lock this script to only run in a specifaed path. ie c:\my
    > folder\ At the moment if I ran this on C:\ by accident it would cause
    > big problems :-)


    Have a look at the online help:
    PS> Get-ChildItem -? | more
    Many cmdlets have a -Path parameter which you can use to specify the
    directory. By default this is usually the current directory "."

    Get-ChildItem -Path C:\mydir -recurse

    > Thank you for your help.
    >


    No problem.

    --
    greetings
    dreeschkind

      My System SpecsSystem Spec


Delete based on date
Similar Threads
Thread Forum
Delete files based on dup names PowerShell
powershell script help - recursive delete based on modified date PowerShell
Deleteing Files in a folder based on date! Vista file management
Copy files based upon Create Date PowerShell
Delete files based on last accessed date? PowerShell