Get count before and after cleanup
 

  How can I get just counts of the files it removes and add multiple file type's in filter.

Filter:

 *.trn and *.bak


  Output in LOG file desired after Date Time STuff.

 Transaction Logs cleaned --- #
 Backup Files Cleaned --- #

"Starting LogCleanup @ $(get-date)" | out-file "F:\SQL-Trn-Bak-Cleanup.txt"

# Get files from the directory and all sub-folders
# Filter using Where-Object to exclude folders.
$logs = get-childitem "\\sql1\Filecopy" -Recurse -Filter "*.bak" | `
  ?{ $_.PsIsContainer -eq $False }

$year = get-date -f "yyyy"
$month = get-date -f "MM"
$day = get-date -f "dd"

foreach ($file in $logs) {
  # File is an Object, not a String.
  $File | out-file "F:\SQL-Trn-Bak-Cleanup.txt" -append

  if ($file.lastwritetime -lt ($(Get-Date).AddDays(-1))) {
    remove-item $File.FullName -Force -WhatIf
  }
}

Thanks.

June 27th, 2013 4:14pm

you can do logs.count for intial, and then add $x++ in your bottom if loop, everytime it hits that loop it will increment $x, make sure to reset it everytime you run
Free Windows Admin Tool Kit Click here and download it now
June 27th, 2013 4:16pm

 I added this code, but how do I separate (.bak) and (.trn) counts? I also want to add that into
my filter (.bak and .trn).

 Thanks.

"Starting LogCleanup @ $(get-date)" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -Append

$directory_file_count = "0"

# Get files from the directory and all sub-folders
# Filter using Where-Object to exclude folders.
$logs = get-childitem "\\sql1\Filecopy" -Recurse -Filter "*.trn" | `
  ?{ $_.PsIsContainer -eq $False }

$year = get-date -f "yyyy"
$month = get-date -f "MM"
$day = get-date -f "dd"

foreach ($file in $logs) {
  
  if ($file.lastwritetime -lt ($(Get-Date).AddDays(-1))) {
    remove-item $File.FullName -Force -WhatIf
  }
}

 # File is an Object, not a String.
  $directory_file_count =$logs.Count
  " Number of Transaction Logs Cleaned Today $month$day Count:$directory_file_count" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -append
 "Ending LogCleanup @ $(get-date)" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -Append
 

 

June 27th, 2013 5:02pm

 Found some posts using:

  Measure-Object |            
          select -ExpandProperty count        

 Have not been successful trying to incorperate into script.   

 

Free Windows Admin Tool Kit Click here and download it now
June 27th, 2013 10:15pm

have two counters then

$bakCount = $null
$trnCount = $null
if($file -like "*.bak"){
$bakCount++
}
else{
$trnCount++
}


June 27th, 2013 10:37pm

Hi,

I've updated the script in order to give you elements you want :

  1. First of all, in order to include *.trn and *.bak files you can use the -include argument of get-childitem cmdlet
  2. once get-childitem ran, you can count initial number of files of each type
  3. finally each time a file is removed, we check its extension and decrement the corresponding counter

This gives the following script :

"Starting LogCleanup @ $(get-date)" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -Append

# Get files from the directory and all sub-folders
# Extension filter is processed by using -include argument
# Filter using Where-Object to exclude folders.
$logs = get-childitem -path "\\sql1\Filecopy" -Recurse -include *.bak,*.trn | ?{ $_.PsIsContainer -eq $False }

# Count Files by extension and log it
$nbbakFiles = ($logs | ?{$_.Extension -eq ".bak"}).count
$nbtrnfiles = ($logs | ?{$_.Extension -eq ".trn"}).count
("Number of .bak files before removal : "+ $nbbakFiles) | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -Append
("Number of .trn files before removal : "+ $nbtrnfiles) | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -Append

$year = get-date -f "yyyy"
$month = get-date -f "MM"
$day = get-date -f "dd"

foreach ($file in $logs) {
  
  if ($file.lastwritetime -lt ($(Get-Date).AddDays(-1))) {
    remove-item $File.FullName -Force -WhatIf
    # decrement extension file counter each time a file is removed
    if($file.Extension -eq '.bak') { $nbbakFiles-- } else {$nbtrnfiles--}
  }
}

 # File is an Object, not a String.
 " Number of Backup Files Cleaned Today $month$day Count: $nbbakFiles" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -append
 " Number of Transaction Logs Cleaned Today $month$day Count: $nbtrnfiles" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -append
 "Ending LogCleanup @ $(get-date)" | out-file "C:\SQL-Trn-Bak-Cleanup.txt" -Append

Best Regards,

Free Windows Admin Tool Kit Click here and download it now
June 29th, 2013 9:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics