I would like to create a Windows batch script that allows me to write filenames of a given directory to a text file only if the modification date is of a specific month and a specific year.
For example, how can I print all the files modified in May 2018 of the C:\tmp directory?

At the moment I wrote this instruction:

FORFILES /P C:\tmp /C "cmd /c if @isdir==FALSE set [email protected]& (call echo @path) >> file.txt" >NUL 2>&1

I should add these conditions:

if %fdate:~3,2%==05 if %fdate:~6,4%==2018

1 Answers

0
Compo On

I have decided to delete my comments and post the pertinent parts as an answer:

If you wish to restrict your task to using , you could try something like this from your , (which would write the file.txt to the same directory as your batch file):

@CD /D "C:\tmp"
@FORFILES /D 01/05/2018 /C "cmd /c if @isdir==FALSE FORFILES /D -31/05/2018 /M @file /C 0x22cmd /c if @isdir==FALSE Echo @fdate @ftime @path0x22" 2>nul >>"%~dp0file.txt"

If you wanted to make your command more efficient, may be a better option, (the following writing file.txt to the current directory):

@For /F "Tokens=*" %%A In ('RoboCopy "C:\tmp" NULL /L /MaxAge:20180501 /MinAge:20180531 /NDL /NJH /NJS /NC /NS /TS')Do @Echo(%%A>>"file.txt"

The For loop is only required to remove the leading space padding from the output, if you don't care about that, you can simply use:

@RoboCopy "C:\tmp" NULL /L /MaxAge:20180501 /MinAge:20180531 /NDL /NJH /NJS /NC /NS /TS>"file.txt"