Link to home
Start Free TrialLog in
Avatar of WellingtonIS
WellingtonIS

asked on

Trying to delete files with a script using forefiles.

I'm trying to delete files older than a day.  I'm using forfiles with the following command:  Forefiles -p "X:\directory" -s -m *.* -d 1 -c "CMD /C Del @FILE : Date -1 days"  I know I have files older than a day but it's telling me No files found with the specified search criteria.  I have files in there starting on the 13th.  What am I doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$limit = (Get-Date).AddDays(-30)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse


That's a powershell script, change the 30 to the correct date and the location
Avatar of WellingtonIS
WellingtonIS

ASKER

THanks I didn't try the powershell one because the forfiles worked.  But thanks everyone.