0

I have a working batch file that I'm running through task scheduler to delete files with today's date in the file name. For now I'm manually entering the date into the filename ahead of time but at least this part works. I'm looking for the best way to have this automatically insert the variable. I can change the naming scheme to default if it cannot be as specific as I have in this example.

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime%
REM 2018-07-04_1040 PM
PushD "\\DISKSTATION\backups\Google Drive" && (
  "forfiles.exe" /s /m "*2018-07-04*" /C "cmd /c del @path"
) & PopD
Squashman
  • 11,987
  • 5
  • 23
  • 33
AjSso
  • 1
  • 1
  • 1
    `07-04-18` means 4-Jul-2018 *à l'américaine*, 7-Apr-2018, or 18-Apr-2007? – AlexP Jul 05 '18 at 03:29
  • Thanks AlexP, added the variable, now just need to get it into working code. I must be searching for this wrong as I don't many specific answers to what I'm looking to do. I know there's a tendency not answer people here and tell them to find out on their own but that's easier said than done at times...why not just answer it and help people out? – AjSso Jul 05 '18 at 03:50
  • So what's wrong with `"*%mydate%*"`? – AlexP Jul 05 '18 at 03:53
  • Not one damned thing. Thank you for the help. – AjSso Jul 05 '18 at 04:03
  • 1
    Your question is essentially a duplicate question. This question probably gets asked a couple times a week on StackOverFlow. We don't need another question and answer about this. Please delete your question. – Squashman Jul 05 '18 at 04:17
  • instead of calling `for /f` and `date /t`, it's much simpler to use `%date%` [like this](https://stackoverflow.com/q/36147552/995714). But both ways are unreliable and error prone. The locale-independent way using `wmic` should be used instead – phuclv Jul 05 '18 at 05:57

1 Answers1

0
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
REM 2018-07-04_1040 PM
PushD "\\DISKSTATION\backups\Google Drive\TEST" && (
  "forfiles.exe" /s /m "*%mydate%*" /C "cmd /c del @path"
) & PopD

Working :-)

Krii
  • 895
  • 8
  • 22
AjSso
  • 1
  • 1