China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-02 16:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 请问如何在批处理文件中引入系统日期作为生成的文件名? Translation: How to introduce the system date as the generated file name in a batch file? View 2,785 Replies 15
Original Poster Posted 2006-12-01 00:48 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
How to introduce the system date as the generated file name in a batch file?

I back up the MySQL database every day, typing the DOS command:
mysqldump -uroot -proot -B qyzx > c:\qyzx_mysql.sql
Then manually change the file name to a file name with the current date, such as: qyzx_mysql_20061128.sql

How to get the current system date in the batch file name and automatically add it to the command?
Further, is there a way to perform relative date calculations like getting yesterday, the day before yesterday, tomorrow, the day after tomorrow, etc.?
In addition, what syntax does the batch processing command belong to? What type of book should I read? I want to learn.
I am a beginner, I can hardly write batch processing, I can only simply fill the Dos command into the bat file.
Please give instructions from the experts, thank you! ~~~~~~~~~~
Floor 2 Posted 2006-12-01 02:30 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
mysqldump -uroot -proot -B qyzx > c:\qyzx_mysql_%date::=-%.sql
Floor 3 Posted 2006-12-01 04:03 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
Thanks, but it didn't succeed. It seems there's something wrong somewhere........

Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

D:\>mysqldump -uroot -proot -B qyzx > c:\qyzx_mysql_%date::=-%.sql
mysqldump: Got error: 1102: Incorrect database name '2006-11-30.sql' when select
ing the database

D:\>myrun1

D:\>mysqldump -uroot -proot -B qyzx 2006-11-30.sql 1>c:\qyzx_mysql_星期四
mysqldump: Got error: 1102: Incorrect database name '2006-11-30.sql' when select
ing the database

D:\>
Floor 4 Posted 2006-12-01 04:05 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
The generated file is qyzx_mysql_Thursday with no extension.
Floor 5 Posted 2006-12-01 04:10 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
I added a pair of double quotes and changed it to
mysqldump -uroot -proot -B qyzx > "c:\qyzx_mysql_%date::=-%.sql"
The obtained backup file name is:
qyzx_mysql_Thursday 2006-11-30.sql
It's basically okay, thank you!!! But is there a better way,
Remove the Chinese character "Thursday" and the subsequent spaces and even the horizontal lines between the dates?
That is, according to the date format of 20061130? Thank you

Because I want the generated file name to be more standard, which can be directly typed on the command line,
File names with Chinese characters and spaces are not very good, thank you

[ Last edited by rocketer on 2006-11-30 at 04:34 PM ]
Floor 6 Posted 2006-12-01 04:29 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
```
for /f "tokens=1-4 delims=:-" %%i in ("%date:~0,10%") do set DT=%%i%%j%%k
set DT=%DT: =0%
mysqldump -uroot -proot -B qyzx > c:\qyzx_mysql_%DT%.sql
```
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2006-12-01 05:20 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
But I tried it, and the result is
qyzx_mysql_星期四020061.sql
It seems to be a small place that's different? ? ?
Please let the expert continue to take a look? ? ?
You can replace mysqldump -uroot -proot -B qyzx
with a dir
to see if the generated file name is what is expected, thank you,
I really don't understand, learning slowly
Floor 8 Posted 2006-12-01 06:01 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I'm sorry, our time formats are different. Change ("%date:~0,10%") to ("%date:~0,13%") and try again
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 9 Posted 2006-12-01 06:55 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
This time it's
qyzx_mysql_Thursday02006113.sql
If the zero after Thursday runs to the end, it will be correct.
Oh? So there are problems with different time formats? How do I know the time format?
Is there still a possibility of chaos when running on another machine after finally getting it right?
Also, the Chinese characters can't be eliminated??????
Thanks
Floor 10 Posted 2006-12-01 07:07 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Win2000 test passed:
for /f "tokens=1-4 delims=:-" %%i in ("%date:~0,14%") do set DT=%%i%%j%%k%%l
set DT=%DT: =%
mysqldump -uroot -proot -B qyzx > qyzx_mysql_%DT%.sql
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2006-12-01 08:10 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
It worked.
qyzx_mysql_Thursday20061130.sql
Thank you!!!
That week seems relatively difficult to eliminate, but it's already very good now. Thanks again!!!
I don't understand it now, I'll learn it slowly.
Floor 12 Posted 2006-12-01 20:49 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
To remove the day of the week, change the code to:
for /f "tokens=1-4 delims=:-" %%i in ("%date:~3,14%") do set DT=%%i%%j%%k%%l
set DT=%DT: =%
mysqldump -uroot -proot -B qyzx > qyzx_mysql_%DT%.sql


You can analyze how to achieve it according to the differences between the two codes, and then you will understand.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 13 Posted 2006-12-02 09:55 ·  中国 江西 九江 电信
新手上路
Credits 18
Posts 8
Joined 2006-12-01 00:24
19-year member
UID 72201
Gender Male
Status Offline
Thank you so much!!! I have bookmarked this post and thank you again!!!
Floor 14 Posted 2006-12-03 03:54 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
xp sp2

set name=%date:~0,-4%
mysqldump -uroot -proot -B qyzx > qyzx_mysql_%name:-=%.sql

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 15 Posted 2006-12-06 06:32 ·  中国 北京 联通数据上网公共出口
新手上路
Credits 4
Posts 2
Joined 2006-12-06 05:34
19-year member
UID 72706
Gender Male
Status Offline
Can be implemented with a software. I have it here. If you need it, add my QQ 403188070 and I'll send it to you
Forum Jump: