Windows Vista Forums

Interesting file deletion requirements
  1. #1


    hibbarcd Guest

    Interesting file deletion requirements

    I have been looking for a vb script that would satisfy these requirements but
    I'm told powershell will perform this task easier:

    1. Find all folders named Archive in a volume.
    2. Of all the folders founds, find all files within those folders that are
    60 days old.
    3. Delete the 60-day old files but preserve the rest of the files and the
    folders.
    4. File types are not all the same i.e. there are .xml, .log, etc.



    Any help is highly appreciated.

      My System SpecsSystem Spec

  2. #2


    Kirk Munro Guest

    Re: Interesting file deletion requirements

    This is definitely a job for PowerShell!

    $now = Get-Date
    Get-ChildItem -Filter Archive -Recurse | Where-Object {$_.PSIsContainer} |
    Get-ChildItem | Where-Object {($_.PSIsContainer -eq $false) -and (($now -
    $_.LastWriteTime) -ge 60)} | Remove-Item -WhatIf

    Here's what it does:
    1. It gets the current date and time and stores it in the now variable.
    2. It recursively walks through the directory tree starting at the current
    directory. It only returns objects (files or folders) with the name
    Archive. For each of those files or folders, it filters out any that are
    not containers (the files). With whatever is left, it calls Get-ChildItem
    to get the items in the folder (files and folders). For each of those, it
    filters out any that are containers (the folders) and any that are not
    containers but that are less than 60 days old. Finally, with the files that
    are left (which are more than 60 days old), it pretends to remove them and
    tells you what it would do if -whatif wasn't there. To actually remove
    them, just get rid of the -whatif at the end.

    Run this as is first, then validate the data to make sure it's going to do
    what you want (manually check the files it would remove). Then remove the
    whatif and let it do your work for you.

    And all in two little lines of script!
    -
    Kirk Munro
    Poshoholic
    http://poshoholic.com

    "hibbarcd" <hibbarcd@xxxxxx> wrote in message
    news:9C612E8C-03E3-4A12-A793-CD076227350A@xxxxxx

    >I have been looking for a vb script that would satisfy these requirements
    >but
    > I'm told powershell will perform this task easier:
    >
    > 1. Find all folders named Archive in a volume.
    > 2. Of all the folders founds, find all files within those folders that are
    > 60 days old.
    > 3. Delete the 60-day old files but preserve the rest of the files and the
    > folders.
    > 4. File types are not all the same i.e. there are .xml, .log, etc.
    >
    > Any help is highly appreciated.


      My System SpecsSystem Spec

  3. #3


    Kirk Munro Guest

    Re: Interesting file deletion requirements

    I just re-read what I posted earlier and need to make two minor adjustments.
    Here's the revised script:

    $now = Get-date
    Get-ChildItem -Path C:\ -Filter Archive -Recurse | Where-Object
    {$_.PSIsContainer} | Get-ChildItem | Where-Object {($_.PSIsContainer -eq
    $false) -and (($now - $_.LastWriteTime).Days -ge 60)} | Remove-Item -whatif

    The adjustments were as follows:
    1. Added -Path parameter usage to original Get-ChildItem call so that you
    don't have to be in the directory you want to start in to use it. Just
    replace C:\ with your path and you're good to go.
    2. I mistakenly left out .Days in the DateTime arithmetic. You wanted to
    know which items were more than 60 days old, so the script needs to compare
    the result value .Days with 60.

    Hopefully the omission of the .Days usage didn't cause you too much
    confusion.

    -
    Kirk Munro
    Poshoholic
    http://poshoholic.com

    "Kirk Munro" <sorry@xxxxxx> wrote in message
    news:u%23ZxqKNHIHA.1324@xxxxxx

    > This is definitely a job for PowerShell!
    >
    > $now = Get-Date
    > Get-ChildItem -Filter Archive -Recurse | Where-Object {$_.PSIsContainer} |
    > Get-ChildItem | Where-Object {($_.PSIsContainer -eq $false) -and (($now -
    > $_.LastWriteTime) -ge 60)} | Remove-Item -WhatIf
    >
    > Here's what it does:
    > 1. It gets the current date and time and stores it in the now variable.
    > 2. It recursively walks through the directory tree starting at the current
    > directory. It only returns objects (files or folders) with the name
    > Archive. For each of those files or folders, it filters out any that are
    > not containers (the files). With whatever is left, it calls Get-ChildItem
    > to get the items in the folder (files and folders). For each of those, it
    > filters out any that are containers (the folders) and any that are not
    > containers but that are less than 60 days old. Finally, with the files
    > that are left (which are more than 60 days old), it pretends to remove
    > them and tells you what it would do if -whatif wasn't there. To actually
    > remove them, just get rid of the -whatif at the end.
    >
    > Run this as is first, then validate the data to make sure it's going to do
    > what you want (manually check the files it would remove). Then remove the
    > whatif and let it do your work for you.
    >
    > And all in two little lines of script!
    > -
    > Kirk Munro
    > Poshoholic
    > http://poshoholic.com
    >
    > "hibbarcd" <hibbarcd@xxxxxx> wrote in message
    > news:9C612E8C-03E3-4A12-A793-CD076227350A@xxxxxx

    >>I have been looking for a vb script that would satisfy these requirements
    >>but
    >> I'm told powershell will perform this task easier:
    >>
    >> 1. Find all folders named Archive in a volume.
    >> 2. Of all the folders founds, find all files within those folders that
    >> are
    >> 60 days old.
    >> 3. Delete the 60-day old files but preserve the rest of the files and the
    >> folders.
    >> 4. File types are not all the same i.e. there are .xml, .log, etc.
    >>
    >> Any help is highly appreciated.
    >
    >


      My System SpecsSystem Spec

  4. #4


    hibbarcd Guest

    Re: Interesting file deletion requirements

    This is what I'm prompted with then executing the script:

    PS I:\> .\archive.ps1

    cmdlet Where-Object at command pipeline position 2
    Supply values for the following parameters:
    FilterScript:

    What should I be entering for this value?

    I set the execution policy to remote signed.

    Thanks for your help.

    "Kirk Munro" wrote:

    > I just re-read what I posted earlier and need to make two minor adjustments.
    > Here's the revised script:
    >
    > $now = Get-date
    > Get-ChildItem -Path C:\ -Filter Archive -Recurse | Where-Object
    > {$_.PSIsContainer} | Get-ChildItem | Where-Object {($_.PSIsContainer -eq
    > $false) -and (($now - $_.LastWriteTime).Days -ge 60)} | Remove-Item -whatif
    >
    > The adjustments were as follows:
    > 1. Added -Path parameter usage to original Get-ChildItem call so that you
    > don't have to be in the directory you want to start in to use it. Just
    > replace C:\ with your path and you're good to go.
    > 2. I mistakenly left out .Days in the DateTime arithmetic. You wanted to
    > know which items were more than 60 days old, so the script needs to compare
    > the result value .Days with 60.
    >
    > Hopefully the omission of the .Days usage didn't cause you too much
    > confusion.
    >
    > -
    > Kirk Munro
    > Poshoholic
    > http://poshoholic.com
    >
    > "Kirk Munro" <sorry@xxxxxx> wrote in message
    > news:u%23ZxqKNHIHA.1324@xxxxxx

    > > This is definitely a job for PowerShell!
    > >
    > > $now = Get-Date
    > > Get-ChildItem -Filter Archive -Recurse | Where-Object {$_.PSIsContainer} |
    > > Get-ChildItem | Where-Object {($_.PSIsContainer -eq $false) -and (($now -
    > > $_.LastWriteTime) -ge 60)} | Remove-Item -WhatIf
    > >
    > > Here's what it does:
    > > 1. It gets the current date and time and stores it in the now variable.
    > > 2. It recursively walks through the directory tree starting at the current
    > > directory. It only returns objects (files or folders) with the name
    > > Archive. For each of those files or folders, it filters out any that are
    > > not containers (the files). With whatever is left, it calls Get-ChildItem
    > > to get the items in the folder (files and folders). For each of those, it
    > > filters out any that are containers (the folders) and any that are not
    > > containers but that are less than 60 days old. Finally, with the files
    > > that are left (which are more than 60 days old), it pretends to remove
    > > them and tells you what it would do if -whatif wasn't there. To actually
    > > remove them, just get rid of the -whatif at the end.
    > >
    > > Run this as is first, then validate the data to make sure it's going to do
    > > what you want (manually check the files it would remove). Then remove the
    > > whatif and let it do your work for you.
    > >
    > > And all in two little lines of script!
    > > -
    > > Kirk Munro
    > > Poshoholic
    > > http://poshoholic.com
    > >
    > > "hibbarcd" <hibbarcd@xxxxxx> wrote in message
    > > news:9C612E8C-03E3-4A12-A793-CD076227350A@xxxxxx

    > >>I have been looking for a vb script that would satisfy these requirements
    > >>but
    > >> I'm told powershell will perform this task easier:
    > >>
    > >> 1. Find all folders named Archive in a volume.
    > >> 2. Of all the folders founds, find all files within those folders that
    > >> are
    > >> 60 days old.
    > >> 3. Delete the 60-day old files but preserve the rest of the files and the
    > >> folders.
    > >> 4. File types are not all the same i.e. there are .xml, .log, etc.
    > >>
    > >> Any help is highly appreciated.
    > >
    > >
    >
    >
    >

      My System SpecsSystem Spec

  5. #5


    hibbarcd Guest

    Re: Interesting file deletion requirements

    Nevermind. I put all the commands on one line and it worked fine.

    Thank you very much for your quick response.


    "hibbarcd" wrote:

    > This is what I'm prompted with then executing the script:
    >
    > PS I:\> .\archive.ps1
    >
    > cmdlet Where-Object at command pipeline position 2
    > Supply values for the following parameters:
    > FilterScript:
    >
    > What should I be entering for this value?
    >
    > I set the execution policy to remote signed.
    >
    > Thanks for your help.
    >
    > "Kirk Munro" wrote:
    >

    > > I just re-read what I posted earlier and need to make two minor adjustments.
    > > Here's the revised script:
    > >
    > > $now = Get-date
    > > Get-ChildItem -Path C:\ -Filter Archive -Recurse | Where-Object
    > > {$_.PSIsContainer} | Get-ChildItem | Where-Object {($_.PSIsContainer -eq
    > > $false) -and (($now - $_.LastWriteTime).Days -ge 60)} | Remove-Item -whatif
    > >
    > > The adjustments were as follows:
    > > 1. Added -Path parameter usage to original Get-ChildItem call so that you
    > > don't have to be in the directory you want to start in to use it. Just
    > > replace C:\ with your path and you're good to go.
    > > 2. I mistakenly left out .Days in the DateTime arithmetic. You wanted to
    > > know which items were more than 60 days old, so the script needs to compare
    > > the result value .Days with 60.
    > >
    > > Hopefully the omission of the .Days usage didn't cause you too much
    > > confusion.
    > >
    > > -
    > > Kirk Munro
    > > Poshoholic
    > > http://poshoholic.com
    > >
    > > "Kirk Munro" <sorry@xxxxxx> wrote in message
    > > news:u%23ZxqKNHIHA.1324@xxxxxx

    > > > This is definitely a job for PowerShell!
    > > >
    > > > $now = Get-Date
    > > > Get-ChildItem -Filter Archive -Recurse | Where-Object {$_.PSIsContainer} |
    > > > Get-ChildItem | Where-Object {($_.PSIsContainer -eq $false) -and (($now -
    > > > $_.LastWriteTime) -ge 60)} | Remove-Item -WhatIf
    > > >
    > > > Here's what it does:
    > > > 1. It gets the current date and time and stores it in the now variable.
    > > > 2. It recursively walks through the directory tree starting at the current
    > > > directory. It only returns objects (files or folders) with the name
    > > > Archive. For each of those files or folders, it filters out any that are
    > > > not containers (the files). With whatever is left, it calls Get-ChildItem
    > > > to get the items in the folder (files and folders). For each of those, it
    > > > filters out any that are containers (the folders) and any that are not
    > > > containers but that are less than 60 days old. Finally, with the files
    > > > that are left (which are more than 60 days old), it pretends to remove
    > > > them and tells you what it would do if -whatif wasn't there. To actually
    > > > remove them, just get rid of the -whatif at the end.
    > > >
    > > > Run this as is first, then validate the data to make sure it's going to do
    > > > what you want (manually check the files it would remove). Then remove the
    > > > whatif and let it do your work for you.
    > > >
    > > > And all in two little lines of script!
    > > > -
    > > > Kirk Munro
    > > > Poshoholic
    > > > http://poshoholic.com
    > > >
    > > > "hibbarcd" <hibbarcd@xxxxxx> wrote in message
    > > > news:9C612E8C-03E3-4A12-A793-CD076227350A@xxxxxx
    > > >>I have been looking for a vb script that would satisfy these requirements
    > > >>but
    > > >> I'm told powershell will perform this task easier:
    > > >>
    > > >> 1. Find all folders named Archive in a volume.
    > > >> 2. Of all the folders founds, find all files within those folders that
    > > >> are
    > > >> 60 days old.
    > > >> 3. Delete the 60-day old files but preserve the rest of the files and the
    > > >> folders.
    > > >> 4. File types are not all the same i.e. there are .xml, .log, etc.
    > > >>
    > > >> Any help is highly appreciated.
    > > >
    > > >
    > >
    > >
    > >

      My System SpecsSystem Spec

  6. #6


    Kirk Munro Guest

    Re: Interesting file deletion requirements

    My pleasure. I'm always happy to help.

    One last thing I should note on this thread. If you want hidden folders
    named Archive and hidden files older than 60 days in those folders as well
    as the non-hidden folders and files you will be processing, you need to use
    The Force! Sorry, I couldn't resist!

    Get-ChildItem has a parameter called force that forces it to show hidden
    files and folders as well. The script, when modified to use the force
    parameter, would look like this:

    $now = Get-date
    Get-ChildItem -Path C:\ -Filter Archive -Recurse -Force | Where-Object
    {$_.PSIsContainer} | Get-ChildItem -Force | Where-Object
    {($_.PSIsContainer -eq $false) -and (($now - $_.LastWriteTime).Days -ge 60)}
    | Remove-Item -whatif

    -
    Kirk Munro
    Poshoholic
    http://poshoholic.com


    "hibbarcd" <hibbarcd@xxxxxx> wrote in message
    newsBB1F681-15C4-431C-9AB8-FC9236F1F953@xxxxxx

    > Nevermind. I put all the commands on one line and it worked fine.
    >
    > Thank you very much for your quick response.
    >
    >
    > "hibbarcd" wrote:
    >

    >> This is what I'm prompted with then executing the script:
    >>
    >> PS I:\> .\archive.ps1
    >>
    >> cmdlet Where-Object at command pipeline position 2
    >> Supply values for the following parameters:
    >> FilterScript:
    >>
    >> What should I be entering for this value?
    >>
    >> I set the execution policy to remote signed.
    >>
    >> Thanks for your help.
    >>
    >> "Kirk Munro" wrote:
    >>

    >> > I just re-read what I posted earlier and need to make two minor
    >> > adjustments.
    >> > Here's the revised script:
    >> >
    >> > $now = Get-date
    >> > Get-ChildItem -Path C:\ -Filter Archive -Recurse | Where-Object
    >> > {$_.PSIsContainer} | Get-ChildItem | Where-Object
    >> > {($_.PSIsContainer -eq
    >> > $false) -and (($now - $_.LastWriteTime).Days -ge 60)} |
    >> > Remove-Item -whatif
    >> >
    >> > The adjustments were as follows:
    >> > 1. Added -Path parameter usage to original Get-ChildItem call so that
    >> > you
    >> > don't have to be in the directory you want to start in to use it. Just
    >> > replace C:\ with your path and you're good to go.
    >> > 2. I mistakenly left out .Days in the DateTime arithmetic. You wanted
    >> > to
    >> > know which items were more than 60 days old, so the script needs to
    >> > compare
    >> > the result value .Days with 60.
    >> >
    >> > Hopefully the omission of the .Days usage didn't cause you too much
    >> > confusion.
    >> >
    >> > -
    >> > Kirk Munro
    >> > Poshoholic
    >> > http://poshoholic.com
    >> >
    >> > "Kirk Munro" <sorry@xxxxxx> wrote in message
    >> > news:u%23ZxqKNHIHA.1324@xxxxxx
    >> > > This is definitely a job for PowerShell!
    >> > >
    >> > > $now = Get-Date
    >> > > Get-ChildItem -Filter Archive -Recurse | Where-Object
    >> > > {$_.PSIsContainer} |
    >> > > Get-ChildItem | Where-Object {($_.PSIsContainer -eq $false) -and
    >> > > (($now -
    >> > > $_.LastWriteTime) -ge 60)} | Remove-Item -WhatIf
    >> > >
    >> > > Here's what it does:
    >> > > 1. It gets the current date and time and stores it in the now
    >> > > variable.
    >> > > 2. It recursively walks through the directory tree starting at the
    >> > > current
    >> > > directory. It only returns objects (files or folders) with the name
    >> > > Archive. For each of those files or folders, it filters out any that
    >> > > are
    >> > > not containers (the files). With whatever is left, it calls
    >> > > Get-ChildItem
    >> > > to get the items in the folder (files and folders). For each of
    >> > > those, it
    >> > > filters out any that are containers (the folders) and any that are
    >> > > not
    >> > > containers but that are less than 60 days old. Finally, with the
    >> > > files
    >> > > that are left (which are more than 60 days old), it pretends to
    >> > > remove
    >> > > them and tells you what it would do if -whatif wasn't there. To
    >> > > actually
    >> > > remove them, just get rid of the -whatif at the end.
    >> > >
    >> > > Run this as is first, then validate the data to make sure it's going
    >> > > to do
    >> > > what you want (manually check the files it would remove). Then
    >> > > remove the
    >> > > whatif and let it do your work for you.
    >> > >
    >> > > And all in two little lines of script!
    >> > > -
    >> > > Kirk Munro
    >> > > Poshoholic
    >> > > http://poshoholic.com
    >> > >
    >> > > "hibbarcd" <hibbarcd@xxxxxx> wrote in message
    >> > > news:9C612E8C-03E3-4A12-A793-CD076227350A@xxxxxx
    >> > >>I have been looking for a vb script that would satisfy these
    >> > >>requirements
    >> > >>but
    >> > >> I'm told powershell will perform this task easier:
    >> > >>
    >> > >> 1. Find all folders named Archive in a volume.
    >> > >> 2. Of all the folders founds, find all files within those folders
    >> > >> that
    >> > >> are
    >> > >> 60 days old.
    >> > >> 3. Delete the 60-day old files but preserve the rest of the files
    >> > >> and the
    >> > >> folders.
    >> > >> 4. File types are not all the same i.e. there are .xml, .log, etc.
    >> > >>
    >> > >> Any help is highly appreciated.
    >> > >
    >> > >
    >> >
    >> >
    >> >


      My System SpecsSystem Spec

Interesting file deletion requirements problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
File Deletion Lavert Vista General 2 28 Feb 2009
File deletion Apache -=CW=- Vista General 2 25 Dec 2008
why can't I protect a file from deletion ? keepout Vista General 2 04 Jan 2008
File deletion bug - and then what?!?! Lasse Live Folder Share 2 03 Jan 2008
Network file deletion Wally Vista file management 2 24 May 2007