Change line 27.
$files = Get-ChildItem -Recurse $folder -File
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 :
What i want :
I don't want to display the folder names, only the file names per folder.
Can you help me ?
Change line 27.
$files = Get-ChildItem -Recurse $folder -File
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)!
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 :
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 :
Thank you