# Unable to convert text files into an array

I'm doing a short script in powershell that should delete text files that exceeds 60 days, however I'm unable to proceed because the script only deletes one random file among the bunch, I'm thinking that the reason is it's not inside an array that's why it's not traversing on all of the text files.

Here is the entire script:

cls
#clearscreen

$ilogs = get-childitem D:\IMPlogs -recurse #get all items inside IMPlogs folder$List = $ilogs | where name -like *.txt.4 #filter items having .txt.(num) #Write-Host ($List |Format-Table| Out-String)
#list all items in the terminal

$Today = (Get-Date -UFormat %s) #date to seconds$ilogsD2S = [datetime]($List).LastWriteTime #get last write time of file and conv to datetime #}$conv2s = (Get-Date $ilogsD2S -UFormat %s) #conv write time to seconds #$conv2s
#$Today$datediff = [int](($Today-$conv2s)/(3600*24))
#substracting today's date and files write time and getting only the whole number
$datediff Write-Host ($List | Format-Table| Out-String)

#$List | Foreach-Object {Write-Host$_}


Here are the text files that I need to work on. (It automatically populates the textfiles in this folder)

On Best Solutions

try Something like this:

Get-ChildItem "c:\temp" -file | where {$_.Name -like '*.txt.?*' -and$_.LastWriteTime -le (Get-Date).AddDays(-60)} | Remove-Item

On

I am not sure about your sample script, but you can achieve your objective using 2 line of script like this.

$DelFromDate = (Get-Date).AddDays(-60) Get-ChildItem -Path "X:\path\to\folder" -Recurse | Where-Object {$_.LastWriteTime -lt $DelFromDate} | Remove-Item  Hope it helps!!! On the primary problem with your script is that you are not iterating thru the list of files. [grin] the next is that you are converting datetime objects to date strings ... and those don't compare to each other gracefully. leave them as datetime objects and they DO compare very neatly. here's a demo of the idea ... it targets *.tmp files in your temp dir that are more than 10 days old. $SourceDir = $env:TEMP$Filter = '*.tmp'
$MaxDaysOld = 10$Today = (Get-Date).Date

$FileList = Get-ChildItem -LiteralPath$SourceDir -Filter $Filter -File -Recurse foreach ($FL_Item in $FileList) {$DaysOld = ($Today -$FL_Item.LastWriteTime).Days
if ($DaysOld -gt$MaxDaysOld)
{
# remove or comment out the next two lines to stop showing the target info
'=' * 30
'[ {0} ] is [ {1} ] days old & can be removed.' -f $FL_Item.Name,$DaysOld
# remove the "-WhatIf" to do this for real
Remove-Item -LiteralPath \$FL_Item.FullName -Force -WhatIf
}
}


truncated output ...

==============================
[ hd410B2.tmp ] is [ 20 ] days old & can be removed.
What if: Performing the operation "Remove File" on target "C:\Temp\hd410B2.tmp".
==============================
[ hd412FA.tmp ] is [ 17 ] days old & can be removed.
What if: Performing the operation "Remove File" on target "C:\Temp\hd412FA.tmp".
==============================

[*...snip...*]

==============================
[ hd4FB42.tmp ] is [ 12 ] days old & can be removed.
What if: Performing the operation "Remove File" on target "C:\Temp\hd4FB42.tmp".
==============================
[ hd4FF36.tmp ] is [ 18 ] days old & can be removed.
What if: Performing the operation "Remove File" on target "C:\Temp\hd4FF36.tmp".