0

I'm trying to create a directory in windows using CMD but the directory names need to be dynamic.

I have a batch script (.bat file) that runs database dumps into a given folder everyday, for instance C:\Users\name\Documents\dump-destination which then contains a bunch of .sql files. Now I need to move all those files into a directory that corresponds to the day's dump date, for instance db-31-12-2020

How can I dynamically create the db-31-12-2020 directory mentioned above (with the day's date) so I can use it below?

move C:\Users\name\Documents\dump-destination\* D:\new-dump-destination\?

Clint_A
  • 428
  • 1
  • 8
  • 21
  • Does this answer your question? [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – T3RR0R Jan 20 '20 at 09:21
  • For a host of other Similarly themed questions, Search: [batch-file] "date" – T3RR0R Jan 20 '20 at 09:23

2 Answers2

1

This assumes your local date format is dd/mm/yyyy. If it is mm/dd/yyyy, you'll have to swap bits round.

set destdir=db-%date:~0,2%-%date:~3,2%-%date:~6,4%
md D:\new-dump-destination\%destdir%
move C:\Users\name\Documents\dump-destination\* D:\new-dump-destination\%destdir%\

You could use the db-%date:~0,2%-%date:~3,2%-%date:~6,4% string twice instead of using the destdir variable, but if you ran it close to midnight and the clock rolls over between the two calls, it breaks.

Buzby
  • 305
  • 1
  • 11
0

Use this code:

@Echo off
:: Save the day, month and year in variables
For /F "Tokens=2,3,4 Delims= \" %%a in ('Date /T') Do Set day=%%a & Set month=%%b & Set year=%%c
:: Create directory
MkDir "D:\new-dump-destination\db-%day%-%month%-%year%"
:: Move files
move "C:\Users\name\Documents\dump-destination\*" "D:\new-dump-destination\db-%day%-%month%-%year%\"
programmer365
  • 12,641
  • 3
  • 7
  • 28
  • I'm getting `%%a was unexpected at this time` on the second statement – Clint_A Jan 20 '20 at 09:35
  • Edited my answer no works without any flaw – programmer365 Jan 20 '20 at 09:36
  • @WasifHasan: you are implementing some unwanted spaces. Switch to preferred `set` syntax: `... Do Set "day=%%a" & Set "month=%%b" & Set "year=%%c"` to avoid them. Also `tokens` and `delims` have to be adapted to the localization (for me it would have to be `tokens=1-3 delims=.") – Stephan Jan 20 '20 at 13:53
  • Your answer, although edited, will probably still exhibit an error, _(`/" was unexpected at this time.`)_, so is likely to still have a flaw. You have also assumed that the OP's `Date /T` output is already in `d\M\y` order, _which is most unlikely_. – Compo Jan 20 '20 at 21:31
  • But the OP Did not face any error(Also me), @Compo. – programmer365 Jan 22 '20 at 08:56
  • Wasif Hasan, if I change the **`\ `** delimiter to **`/`** and tokens **`2,3,4`**, to **`1,2,3`**, (as necessary for my usual locale), my experience is that I would always receive the error message, I provided previously; _(using your code without change it would be **`\" was unexpected at this time.`**)_. Please also action the other flaw highlighted by @Stephan, which was introduced by your attempted edit to fix the issue originally indicated by the OP. BTW, the OP has not provided any indication that they didn't face a problem, _I assume they fixed it themselves, as it is a very easy fix_. – Compo Jan 22 '20 at 10:44