Search code examples
windowsbatch-filecmdmkdir

Windows CMD create dynamically named directory


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\?


Solution

  • 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%\"