How do I modify this powershell script so it creates a log of the date time and names of all files that were deleted or previously deleted? -
i wanted make powershell script deleted type of file in folder when 7 days old. problem having creating log file date, time, , names of files deleted or deleted when script ran.
i wondering if there way of modifying answer found on page: https://stackoverflow.com/questions/12326171/powershell-script-to-delete-sub-folders-and-files-if-creation-date-is-7-days-bu?lq=1
i instead of sending email report create 1 log file of files deleted names, time, , dates in folder storing script (please note i'd append log file each time not overwrite it). sorry because not know how code have been stuck trying myself long time , have been asking questions , such still can't seem work. yea if can modify script appreciated! thank you!
here did script (if helps) , doesn't work (again not know how code):
$report_items = @() # set folder path $dump_path = "c:filedeleter\autodeletefilesinthisfolder" # set min age of files $max_days = "-7" # current date $curr_date = get-date # determine how far go based on current date $del_date = $curr_date.adddays($max_days) # sub directories $sub_dirs = get-childitem $dump_path | where-object {!$_.psiscontainer } $sub_dirs | % { if (get-childitem $_.fullname -recurse | where-object { $_.lastwritetime -gt $del_date } ) { $report_items += new-object -type psobject -property @{ time = get-date -f "hh:mm:ss" message = "skipping " + $_.fullname + " because contains items newer " + $del_date } } else { remove-item $_.fullname -recurse $report_items += new-object -type psobject -property @{ time = get-date -f "hh:mm:ss" message = "deleting " + $_.fullname + " because contains no items newer " + $del_date } } } $report_items | out-file "c:\filedeleter\pruningreport.txt"
this should it:
get-childitem -path "c:\filedeleter\autodeletefilesinthisfolder" -filter *.torrent -recurse | { $_.creationtime -lt (get-date).adddays(-7) } | %{remove-item -force; if ($?) {"$(get-date -format 'mm-dd-yy hh:mm:ss.ff') $_.name" | add-content 'c:\filedeleter\pruningreport.txt'}}
(the reason if ($?)
block write log entry if file deletion successful - don't want assume was.)
Comments
Post a Comment