question

Warzou avatar image
0 Votes"
Warzou asked Warzou answered

Remove Older Files script error

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
print01.png (472.4 KiB)
print02.png (137.1 KiB)
print01.png (359.8 KiB)
print02.png (62.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

Change line 27.

 $files = Get-ChildItem -Recurse $folder -File
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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)!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Warzou avatar image
0 Votes"
Warzou answered MotoX80 commented

Thank you for your help @MotoX80 & @RichMatheisen-8856

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


print-03.png (341.2 KiB)
print-04.png (214.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Change line 22.

        $files = Get-ChildItem -Recurse $row.FullName -File

1 Vote 1 ·
Warzou avatar image
0 Votes"
Warzou answered

Thank you, @MotoX80

Everything is working now

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.