none
Powershell script issue RRS feed

  • Question

  • Hello,

    I've got a script that I found online that nearly does everything I need it to do. It runs as a scheduled task on servers and looks at a specific directory for files with a specific extension and deletes them if they are older than 2 days. My problem is that I need to add another specific directory and a corresponding extension type. Any ideas what I'm doing wrong? The initial $TargetFolder works, but the second one does not. I'm not at a a scripting/PS expert, so any help would be appreciated.

    #----- define parameters -----#
    #----- get current date ----#
    $Now = Get-Date
    #----- define amount of days ----#
    $Days = "2"
    #----- define folder where files are located ----#
    $TargetFolder = "C:\Apps"
    $TargetFolder2 = "C:\Try"
    #----- define extension ----#
    $Extension = "*.log,*.TRY"
    #----- define LastWriteTime parameter based on $Days ---#
    $LastWrite = $Now.AddDays(-$Days)
     
    #----- get files based on lastwrite filter and specified folder ---#
    $Files = Get-Childitem $TargetFolder,$TargetFolder2 -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
     
    foreach ($File in $Files) 
        {
        if ($File -ne $NULL)
            {
           #write-host "Deleting File $File" -ForegroundColor "DarkRed"
            Remove-Item $File.FullName | out-null
            }
        else
            {
            #Write-Host "No more files to delete!" -foregroundcolor "Green"
            }
        }


    • Edited by nwebb98 Thursday, July 30, 2015 2:55 PM
    Thursday, July 30, 2015 2:52 PM

Answers

  • Here's a cleaner version. PowerShell can use numbers - you don't have to convert stuff to strings. You can also take advantage of the pipeline.


    $Days = 2
    
    $Paths = @(
      "C:\Apps"
      "C:\Try"
      "C:\Path3"
    )
    
    $Includes = @(
      "*.log"
      "*.try"
    )
    
    $params = @{
      "Path" = $Paths
      "Include" = $Includes
      "Recurse" = $true
    }
    
    Get-ChildItem @params | Where-Object { $_.LastWriteTime -le (Get-Date).AddDays(-$Days) } | ForEach-Object {
      Write-Host "Deleting '$($_.FullName)'" -ForegroundColor DarkRed
      Remove-Item $_.FullName
    }
    


    -- Bill Stewart [Bill_Stewart]


    Thursday, July 30, 2015 3:35 PM
    Moderator

All replies

  • You cannot assign two values for extension.  I added an additional variable for $Extension2 and added it to the include statement.  This will get the files in both folders.  It will however check for both extennsions in both folders.  This may be OK based on your fiolder contents.  If the folders contain similar files and you want to only search for one extension in each folder you will need to seperate the get-childitem searches.

    Hope this helps

     
    #----- define parameters -----#
    #----- get current date ----#
    $Now = Get-Date
    #----- define amount of days ----#
    $Days = "2"
    #----- define folder where files are located ----#
    $TargetFolder = "C:\Apps"
    $TargetFolder2 = "C:\Try"
    #----- define extension ----#
    $Extension = "*.log"
    $Extension2 = "*.TRY"
    #----- define LastWriteTime parameter based on $Days ---#
    $LastWrite = $Now.AddDays(-$Days)
     
    #----- get files based on lastwrite filter and specified folder ---#
    $Files = Get-Childitem $TargetFolder,$TargetFolder2 -Include $Extension,$Extension2 -Recurse # | Where {$_.LastWriteTime -le "$LastWrite"}
     
    foreach ($File in $Files) 
        {
        if ($File -ne $NULL)
            {
            write-host "Deleting File $File" -ForegroundColor "DarkRed"
            Remove-Item $File.FullName | out-null
            }
        else
            {
            Write-Host "No more files to delete!" -foregroundcolor "Green"
            }
        }

    • Proposed as answer by jrv Thursday, July 30, 2015 3:29 PM
    Thursday, July 30, 2015 3:23 PM
  • Check you spelling.


    \_(ツ)_/

    Thursday, July 30, 2015 3:29 PM
  • Here's a cleaner version. PowerShell can use numbers - you don't have to convert stuff to strings. You can also take advantage of the pipeline.


    $Days = 2
    
    $Paths = @(
      "C:\Apps"
      "C:\Try"
      "C:\Path3"
    )
    
    $Includes = @(
      "*.log"
      "*.try"
    )
    
    $params = @{
      "Path" = $Paths
      "Include" = $Includes
      "Recurse" = $true
    }
    
    Get-ChildItem @params | Where-Object { $_.LastWriteTime -le (Get-Date).AddDays(-$Days) } | ForEach-Object {
      Write-Host "Deleting '$($_.FullName)'" -ForegroundColor DarkRed
      Remove-Item $_.FullName
    }
    


    -- Bill Stewart [Bill_Stewart]


    Thursday, July 30, 2015 3:35 PM
    Moderator
  • That is a hilarious comment coming from you.

    LOL

    Thursday, July 30, 2015 3:40 PM