Windows CMD 创建动态命名的目录
Windows CMD create dynamically named directory
我正在尝试使用 CMD 在 windows 中创建目录,但目录名称必须是动态的。
我有一个批处理脚本(.bat 文件)每天将数据库转储到给定的文件夹中,例如 C:\Users\name\Documents\dump-destination
然后包含一堆 .sql
文件。现在我需要将所有这些文件 move
放入与当天转储日期相对应的目录中,例如 db-31-12-2020
如何动态创建上面提到的 db-31-12-2020
目录(带有日期)以便我可以在下面使用它?
move C:\Users\name\Documents\dump-destination\* D:\new-dump-destination\?
使用此代码:
@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%\"
这假设您的本地日期格式是 dd/mm/yyyy。如果是 mm/dd/yyyy,则必须交换位。
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%\
您可以使用 db-%date:~0,2%-%date:~3,2%-%date:~6,4% 字符串两次而不是使用 destdir 变量,但是如果您 运行 快到午夜了,时钟在两次通话之间翻转,它坏了。
我正在尝试使用 CMD 在 windows 中创建目录,但目录名称必须是动态的。
我有一个批处理脚本(.bat 文件)每天将数据库转储到给定的文件夹中,例如 C:\Users\name\Documents\dump-destination
然后包含一堆 .sql
文件。现在我需要将所有这些文件 move
放入与当天转储日期相对应的目录中,例如 db-31-12-2020
如何动态创建上面提到的 db-31-12-2020
目录(带有日期)以便我可以在下面使用它?
move C:\Users\name\Documents\dump-destination\* D:\new-dump-destination\?
使用此代码:
@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%\"
这假设您的本地日期格式是 dd/mm/yyyy。如果是 mm/dd/yyyy,则必须交换位。
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%\
您可以使用 db-%date:~0,2%-%date:~3,2%-%date:~6,4% 字符串两次而不是使用 destdir 变量,但是如果您 运行 快到午夜了,时钟在两次通话之间翻转,它坏了。