Remove older files by last access flag

EdWin 121 Reputation points
2020-10-21T15:35:12.887+00:00

Hi everyone,

I need a script (or comand, whatever) that remove old files from a File Server. But, I have to delete these files by "last accessed files" from before a specific date (before 01/01/2019 for example). I've used forfiles command to achieve this task, but forfiles exclude files just by their last modified date. The picture below shows the dates of a file:

34101-capture1.jpg

I'd like to know whether it's possible to remove files by last accessed date or not. If so, which tool can I use for this?

Thank you

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
11,737 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 27,831 Reputation points Microsoft Vendor
    2020-10-22T08:16:33.003+00:00

    Hi,

    You could do it with powershell script like this

    [System.DateTime]$date="10/22/2020 16:10:23"  
    $YourPath = "X:\YourPath"  
    Get-ChildItem -Path $YourPath -Recurse | Where-Object {$_.LastAccessTime -lt $date} | Remove-Item  
    

    This will remove all the files last accessed before 10/22/2020 16:10:23 in X:\YourPath including files in subfolders.

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments