0

This is my first post on here, i've started an apprenticeship and one of my first tasks is to make a script in batch to check if the last modified date of some files correspond to the current date.

If it corresponds, it saves the output into a txt file called test.txt with this part

:correct
echo. >> C:\Users\mmi\Desktop\test.txt

echo %date% >> C:\Users\mmi\Desktop\test.txt
forfiles /P C:\Users\mmi\Desktop\batch\qvtests /M *excel.xlsx* /C "cmd /c echo @file @fdate @ftime"  >> C:\Users\mmi\Desktop\test.txt
forfiles /P C:\Users\mmi\Desktop\batch\qvtests\dossier /M *.txt* /C "cmd /c echo @file @fdate @ftime"  >> C:\Users\mmi\Desktop\test.txt

If it doesn't correspond it would generate a new txt file called error_X.txt each time.

:error
echo. >> C:\Users\mmi\Desktop\erreur.txt
echo %date% >> C:\Users\mmi\Desktop\erreur.txt
forfiles /P C:\Users\mmi\Desktop\batch\qvtests /M *excel.xlsx* /C "cmd /c echo @file @fdate @ftime"  >> C:\Users\mmi\Desktop\erreur.txt

My question is how can i check wether the last modified date corresponds or not ? I've read countless topics but I just can't understand how to adapt their code to my situation

Thanks for helping me !!

  • 1
    At the top of each page of this site is a very good search facility. Use it to search for similar questions, and you'll very quickly find that you cannot use `%date%`, unless your script is only for use on that specific machine, under the same user profile, and with no future configuration changes. – Compo Sep 09 '20 at 11:55
  • 1
    [this answer of mine](https://stackoverflow.com/a/18024049/2152082) gives you the code lines for getting the lastModified Date/time of a file and the current date/time in the same format. Compare them like `if "%lastmodified:~0,8%" == "%today:~0,8%" ...` – Stephan Sep 09 '20 at 12:26
  • [`forfiles`](https://ss64.com/nt/forfiles.html) features a `/D` option that allows to specify a certain date or a relative number of days; `/D +0` means modified today or later (yes, later); since only the date is considered but not the time, this could perhaps help you: `forfiles /P "%UserProfile%\Desktop\batch\qvtests\dossier" /M "*.txt" /C "cmd /C if @isdir==FALSE echo @file @fdate @ftime"` – aschipfl Sep 09 '20 at 15:05

1 Answers1

0

To get the files modified on todays date, you need to use the /D option, as clearly shown in the help information for the command you're using. Open a Command Prompt window, type forfiles /?, press the ENTER key, and read the information presented.

Example:

@"%__AppDir__%forfiles.exe" /P "%UserProfile%\Desktop\batch\qvtests\dossier" /D 0 /M "*.txt" /C "\"%__AppDir__%cmd.exe\" /C If @IsDir==FALSE Echo @File @FDate @FTime"
@Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • damnit i didn't even know you could do that !! i've started learning batch only 5 days ago sorry ! – Maxime Michon Sep 09 '20 at 12:44
  • 1
    @MaximeMichon, I hope you took the [Tour] before you posted your question and placed your comment. Please also read [What should I do when someone answers my question](https://stackoverflow.com/help/someone-answers) – Squashman Sep 09 '20 at 13:42