Search code examples
datetimebatch-filefor-loopforfiles

forfiles for specific datetime


I am trying to list files that were modified after certain datetime. For instance, june 8 20:29, but I am having trouble finding these options for forfiles. Do you know if datetime option is available for forfiles or I should use something different? my batch file is below.

NET USE b: /DELETE
NET USE b: "\\networkddrive\users\username" /PERSISTENT:YES
CHCP 1252 > NULL
forfiles /p b:\ /s /m *.txt /d +"06/08/2018 14:22" /c "cmd /c echo @path @fdate @ftime"  > "b:\listfiles.txt"

Solution

  • forfiles will not deal with the time part, only with the date. I suggest using a Powershell script:

    param (
    [string]$ds=((Get-Date).AddDays(-10)).ToString()
    )
    
    $dt=(Get-Date $ds)
    Get-ChildItem -Path "B:\" -Filter "*.txt" | Where {($_.LastWriteTime -ge $dt)} | Sort-Object LastWriteTime,Name
    

    Store this script in a *.ps1 file to run it. The (optional) parameter is a string which can be interpreted as a date, like "1.4.2017 17:00". The arbitrary default is 'now minus 10 days'.