2012-11-29 02:49:32 +0000 2012-11-29 02:49:32 +0000
21
21

在批处理文件中使用日期和时间来创建文件名

我正在运行一个批处理文件的程序,当它完成后会对我的MySQL数据库进行自动备份。 sql (Date format YYYY-MMY-MM-DD-HHH-MM-SS)

我在网上查了一些资料,说是尝试%DATE:~4%%Date.Year%,但我得到一个错误,说是The system cannot find the specified path.

如果我删除了时间戳的尝试,脚本工作正常,但却过度写入了之前的备份

这里是我说的代码部分:

@REM ***EXECUTION***
echo. Starting backup...
SET timestamp %DATE:~-4%%DATE:~4,2%%DATE:~7,2%%TIME%
%mysqldir%\mysqldump -u %mysqluser% -p%mysqlpassword% -h %mysqlhost% -P %mysqlport% --databases --routines --verbose gnucash_shockwave > %BackupDir%\gnucash_shockwave-%timestamp%.sql

echo.------------------------------------------------------
echo. Backup complete!

有什么建议吗?

答案 (6)

28
28
28
2012-11-29 18:00:07 +0000

日期和时间格式取决于你在地区和语言控制面板小程序中指定的内容。

运行下面的批处理文件(假定 dd/mm/yyyyhh:mm:mm:ss),然后根据需要修改子串提取(使用 :和 ~ 字符),从日期和时间字符串中得到正确的部分:

@echo off
cls
echo Date format = %date%
echo dd = %date:~0,2%
echo mm = %date:~3,2%
echo yyyy = %date:~6,4%
echo.
echo Time format = %time%
echo hh = %time:~0,2%
echo mm = %time:~3,2%
echo ss = %time:~6,2%
echo.
echo Timestamp = %date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%

更多关于子串提取的帮助,请在命令提示符中键入 set /? 或参见 本页

8
8
8
2015-03-05 20:56:02 +0000

以下是对我有效的方法

Format - mmddyyyy_HHMMSS

echo %DATE% %TIME%

Date format = Thu 03/05/2015 15:48:26.22

echo mm = %date:~4,2%

echo dd = %date:~7,2%

echo yyyy = %date:~10,4%

echo Timestamp = %date:~4,2%%date:~7,2%%date:~10,4%_%time:~0,2%%time:~3,2%%time:~6,2%

Timestamp - 03052015_154013
3
3
3
2015-06-25 20:25:48 +0000

稍作调整后,我得到了这个

@echo off
cls
echo Date format = %date%
echo dd = %date:~0,2%
echo mm = %date:~3,2%
echo yyyy = %date:~6,8%
echo.
echo Time format = %time%
echo hh = %time:~0,2%
echo mm = %time:~3,2%
echo ss = %time:~6,2%
echo.
echo Timestamp = %date:~0,2%_%date:~3,2%_%date:~6,8%-%time:~0,2%_%time:~3,2%_%time:~6,2%
ECHO "TEST..." > test-%date:~0,2%_%date:~3,2%_%date:~6,8%-%time:~0,2%_%time:~3,2%_%time:~6,2%.txt

结果:

"test-do_25_06-2015-22_21_51.txt" (Thursday_25th_June_2015-22:21.51)

2
2
2
2019-03-28 17:59:29 +0000

很抱歉延迟了…..

无论地区设置如何,唯一可靠的方法就是用foxidrive @ https://stackoverflow.com/questions/11037831/filename-timestamp-in-windows-cmd-batch-script

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
```的解决方案来获取合适的日期。
1
1
1
2018-06-15 08:44:00 +0000

我想这可以改善一下@Nick Rogers的答案。

EX: 德国人的locate (de\DE)在10以下的小时有一个前导空格。"echo - !^hh! -这个例子应该可以匹配更多的locale.

0
0
0
2018-03-02 11:24:58 +0000

最短(最通用的?)的答案是如下TimeStamp.cmd脚本,只包含三行代码(不包括注释等)…..

@ECHO ON
@REM use FOR /F to 'break out' the components of %DATE% and %TIME%, assuming 'yyyy/mm/dd' format date i.e.

    for /F "tokens=1-6* delims=.:-/ " %%i IN ("%DATE% %TIME%") DO Set "YYYY=%%i"& Set "MM=%%j"& Set "DD=%%k"& Set "HH=%%l"& Set "MI=%%m"& Set "SS=%%n"

@REM Switch the year and day if appropriate

    IF NOT "X%DD:~2%" == "X" Set "YYYY=%DD%"& Set "DD=%YYYY%"

    Set "TimeStamp=%YYYY%%MM%%DD%%HH%%MI%%SS%"

注意,在批处理文件中的FOR语句中,需要使用%%而不是%作为变量的变量

…执行结果(空行和注释去掉)…..

C:\>timestamp.cmd
C:\>for /F "tokens=1-6* delims=.:-/ " %i IN ("2018/03/02 11:17:43.35") DO Set "YYYY=%i" & Set "MM=%j" & Set "DD=%k" & Set "HH=%l" & Set "MI=%m" & Set "SS=%n"
C:\>Set "YYYY=2018" & Set "MM=03" & Set "DD=02" & Set "HH=11" & Set "MI=17" & Set "SS=43"
C:\>IF NOT "X" == "X" Set "YYYY=02" & Set "DD=2018"
C:\>Set "TimeStamp=20180302111743"