Remove Older Files script error

Warzou 101 Reputation points
2022-05-27T23:33:03.14+00:00

Hi,

I have an error in my script :

$Date=Get-date -format "yyyyMMdd-HHmmss"  
    $logfile = "C:\PSScripts\$(get-date -f yyyy-MM-dd).log"  
    $Daysback = "-10"  
      
    Function Log($String) {  
        "[$([DateTime]::Now)]: $string" | Out-File -FilePath $logfile -Append  
    }  
      
    $folder = "C:\Users\xxxx\Downloads\_test"  
    $CurrentDate = Get-date  
    $Subfolder = Get-ChildItem $folder  
    foreach ($row in $Subfolder) {  
        #write-host $row.name  
          
        $CleanFolder = get-childitem $folder2  
        $folder3 = $folder+"\"+$row2.name   
        foreach ($row2 in $Cleanfolder) {  
                                   
                $folder3 = $folder+"\"+$row2.name   
                write-host "Processing Folder $row" -ForegroundColor Red  
                Log "Process Folder $row"  
                write-host "Retention for this compay : $Daysback" -ForegroundColor Green  
                Log "Retention for this compay : $Daysback"  
                Log "----------------------------------"  
                #Actions on files  
                $DatetoDelete = $CurrentDate.AddDays($Daysback)  
                $files = Get-ChildItem -Recurse $folder #| Where-Object { $_.LastWriteTime -lt $DatetoDelete } #| Remove-Item  
                foreach ($file in $files) {  
                    if ($file.LastWriteTime -lt $DatetoDelete) {  
                        #File need to be deleted  
                        $Message = $file.name+" - "+$file.LastWriteTime  
                        Log $Message  
                        write-host $file.name "-" $file.LastWriteTime  
                        remove-item -recurse $folder3  
                        #start-sleep 10  
         }  
                }  
                Log "----------------------------------"  
            }  
        }   
  

The result :

206286-print01.png

What i want :

206296-print02.png

I don't want to display the folder names, only the file names per folder.

Can you help me ?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,361 questions
0 comments No comments
{count} votes

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2022-05-28T14:38:14.643+00:00

    There's more wrong in that script than what @MotoX80 pointed out.

    If you only want subfolders in $folder then line 11 needs the -Dir switch. Without it you'll also get any files in the $folder directory.
    Line 15 references $folder2, but that variable doesn't exist (and it's only mentioned once in the script).
    Line 16 references a variable named $row2, but that variable has yet to be initialized (at least on the 1st iteration of the foreach loop begun on line 12). It's also duplicated on line 19 where it makes sense.
    Line 27 is using $folder (i.e. the TOP-LEVEL folder, not the current SUBfolder)!

    0 comments No comments

  2. Warzou 101 Reputation points
    2022-05-29T10:11:51.453+00:00

    Thank you for your help @MotoX80 & @Rich Matheisen

    I made some changes with your replys :

     $Date=Get-date -format "yyyyMMdd-HHmmss"  
         $logfile = "C:\PSScripts\Delete\$(get-date -f yyyy-MM-dd).log"  
         $Daysback = "-10"  
              
         Function Log($String) {  
             "[$([DateTime]::Now)]: $string" | Out-File -FilePath $logfile -Append  
         }  
              
         $folder = "c:\Users\xxxx\Downloads\_test\"  
         $CurrentDate = Get-date  
         $Subfolder = Get-ChildItem $folder -Directory  
         foreach ($row in $Subfolder) {  
                                           
                      
                     write-host "Processing Folder $row" -ForegroundColor Red  
                     Log "Process Folder $row"  
                     write-host "Retention for this compay : $Daysback" -ForegroundColor Green  
                     Log "Retention for this compay : $Daysback"  
                     Log "----------------------------------"  
                     
                     $DatetoDelete = $CurrentDate.AddDays($Daysback)  
                     $files = Get-ChildItem -Recurse $folder -File  
                        
                     foreach ($file in $files) {  
                         if ($file.LastWriteTime -lt $DatetoDelete) {  
                             #File need to be deleted  
                             $Message = $file.name+" - "+$file.LastWriteTime  
                             Log $Message  
                             write-host $file.name "-" $file.LastWriteTime  
                             #remove-item -recurse $file  
                               
              }  
                     }  
                     Log "----------------------------------"  
                 }  
    

    The result now :

    206471-print-03.png

    The folder names are correct.

    I have now an issue with the files, it doesn't list the files of the folders and display the wrong date :

    206387-print-04.png

    Thank you


  3. Warzou 101 Reputation points
    2022-05-30T08:45:46.113+00:00

    Thank you, @MotoX80

    Everything is working now

    0 comments No comments