Help with a simple script...

This topic contains 8 replies, has 5 voices, and was last updated by  T D J 1 week, 6 days ago.

  • Author
    Posts
  • #83708

    T D J
    Participant

    I am pretty new to powershell and love it! I wrote this little snippet and it works, only under the condition you are at the console and click yes to all when it says hey you did not use the recurse option for directory X ... I just do not get where i missed adding a recurse flag at. Any insight is appreciated.

    $limit = (Get-Date).AddDays(-14)
    $paths = @("F:\specific\place\for\tomcat\temp\","F:\another\place\for\log\archives\")
    $excluded = @("*.pid", "*.status", "*.lock")
    
    # Delete files that are not $excluded and older than the $limit.
     
    foreach ($path in $paths)
        {
    
    Get-ChildItem -Path $path -Recurse -Force -Exclude $excluded | Where-Object { $_.CreationTime -lt $limit } | Remove-Item -Force
    
        }
    
  • #83710

    Alexander Holmeset
    Participant

    If you do it like this: Remove-Item -Confirm:$false
    This stops the commmand from asking for permission.

    • #83756

      T D J
      Participant

      Thanks alot that did the trick for the time being!

  • #83711

    Jeremy Murrah
    Participant

    it sounds like that is coming from remove-item. Do you have any subfolders in any of those paths? if so, your loop will send every subfolder down the pipeline, so at some point you're effectively trying to delete a subfolder. That's when Remove-item will send you that message.

    Alternately if you don't want to delete any subfolders you could add the "-file" switch parameter to your "get-childitem" command. That will retrieve only files and should avoid any questions about recursion from remove-item. It would leave empty subfolders if you have any though, so not sure if that's what you're after.

    You can also add "-confirm:$False" to "Remove-Item" but be careful with that combination. it's a good way to delete alot of stuff really quickly

    • #83747

      T D J
      Participant

      Thanks for the reply, in this scenario the "-confirm:$False" pretty much hit the nail on the head. However the application this is helping me keep maintenance on is being upgraded next month and instead of a temp directory bucket the temp files will be sorted down into individually pre-created directories for their associated plugins, so I will absolutely be using the -file switch on that new server.

  • #83720

    Curtis Smith
    Participant

    You'll want to be careful with that. You didn't tell Get-ChildItem to only return files, so if it finds a directory that matches your Where-Object criterium then you will delete it and its contents even if the files inside of it do not match the criterium.

    • #83746

      T D J
      Participant

      Thanks for the reply, with the redundancy protection we have in place and this being a temp directory over two weeks old I was not too worried about blowing out everything. But yeah not an option id put in place just anywhere.

  • #83723

    postanote
    Participant

    Historically. I've used this approach for such efforts...
    Based on the sample provided, I'd would do stuff like this...

    foreach ($path in $paths)
    {

    ForEach ($TargetFilename in (Get-ChildItem -Path $path -Recurse -Force -Exclude $excluded |
    Where-Object { $_.CreationTime -lt $limit }).FullName)
    {
    If (($TargetFilename) -notmatch $excluded)
    { Remove-Item -Path $TargetFilename -Confirm:$false -Force }
    }
    }

    Frankly, I gotten into the habit that when I have to do such things like the query here. I use Move-Item to a folder named ReviewBeforeDeletion.

    foreach ($path in $paths)
    {

    ForEach ($TargetFilename in (Get-ChildItem -Path $path -Recurse -Force -Exclude $excluded |
    Where-Object { $_.CreationTime -lt $limit }).FullName)
    {
    If (($TargetFilename) -notmatch $excluded)
    {
    # Remove-Item -Path $TargetFilename -Confirm:$false -Force
    Move-Item -Path $TargetFilename -Destination 'H:\ReviewBeforeDeletion' -Confirm:$false -Force
    }
    }
    }

    So I still get rid of the goods in a given folder that I do not want there, but have a temp backup before finalizing the delete. This ensures I get to physically see that I am only getting files. If I get a folder, for whatever reason, I can always move that back.

    • #83743

      T D J
      Participant

      Thanks for the reply, in this instance its strictly a disk space maintenance effort and our redundancy in place keeps everything on demand for 30 days so if something that shouldn't have been deleted does get deleted I can get it back in a matter of moments. That being said I will definitely keep this approach on hand for other situations where I may want to do some eyeball confirmations.

You must be logged in to reply to this topic.