I have a powershell script below that takes a configuration file and deletes files older than x days matching the regular expression.
Configuration file:
path,pattern,days,testrun C:\logs\,^data_access_listener.log,7,false
However, here is the output:
Would have deleted 000a19f6-a982-4f77-88be-ca9cc51a2bcbuu_data_access_listener.log Would have deleted 00189746-2d46-4cdd-a5bb-6fed4bee25a7uu_data_access_listener.log
I expect the output to include the full path to the file, since I am using the .FullName attribute, so I expect the result as such:
Would have deleted C:\logs\000a19f6-a982-4f77-88be-ca9cc51a2bcbuu_data_access_listener.log Would have deleted C:\logs\00189746-2d46-4cdd-a5bb-6fed4bee25a7uu_data_access_listener.log
If I use $ x.FullName, why am I not getting the full name using the path (C: \ logs)?
Thanks Brad
$LogFile = "C:\deletefiles.log" $Config = import-csv -path C:\config.txt function DeleteFiles ([string]$path, [string]$pattern, [int]$days, [string]$testrun){ $a = Get-ChildItem $path -recurse | where-object {$_.Name -notmatch $pattern} foreach($x in $a) { $y = ((Get-Date) - $x.LastWriteTime).Days if ($y -gt $days -and $x.PsISContainer -ne $True) { if ($testrun -eq "false") { write-output "Deleted" $x.FullName >>$LogFile } else { write-output "Would have deleted $x" >>$LogFile } } } } foreach ($line in $Config) { $path = $line.path $pattern = $line.pattern $days = $line.days $testrun = $line.testrun DeleteFiles $path $pattern $days }
source share