我在批处理文件中有以下行(在旧的Windows 2000框中运行):
7z a QuickBackup.zip *.backup
如何将日期附加到QuickBackup.zip
文件。因此,如果我今天运行批处理文件,理想情况下,该文件将为QuickBackup20090514.zip
。
有办法做到这一点吗?
答案 0 :(得分:57)
Bernhard's answer需要一些调整工作,因为%DATE%环境变量采用不同的格式(如其他地方所述)。此外,还有一个波浪号(〜)缺失。
而不是:
set backupFilename=%DATE:~6,4%%DATE:~3,2%%DATE:0,2%
我不得不使用:
set backupFilename=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
表示日期格式:
c:\ Scripts> echo%DATE%
星期五05/14/2009
答案 1 :(得分:12)
如果您知道您的区域设置不会改变,您可以按照以下步骤进行操作:
如果您的短日期格式是dd / MM / yyyy:
SET MYDATE =%DATE:~3,2 %% DATE:〜0,2 %% DATE:~8,4%
如果您的短日期格式为MM / dd / yyyy:
SET MYDATE =%DATE:〜0,2 %% DATE:~3,2 %% DATE:~8,4%
但是没有通用的方法可以独立于您的区域设置。
我不建议依赖区域设置来处理将在生产环境中使用的任何内容。相反,您应该考虑使用其他脚本语言 - PowerShell,VBScript,...
例如,如果您在与批处理文件相同的目录中创建VBS文件 yyyymmdd.vbs ,则包含以下内容:
' yyyymmdd.vbs - outputs the current date in the format yyyymmdd
Function Pad(Value, PadCharacter, Length)
Pad = Right(String(Length,PadCharacter) & Value, Length)
End Function
Dim Today
Today = Date
WScript.Echo Pad(Year(Today), "0", 4) & Pad(Month(Today), "0", 2) & Pad(Day(Today), "0", 2)
然后您就可以从批处理文件中调用它了:
FOR /F %%i IN ('cscript "%~dp0yyyymmdd.vbs" //Nologo') do SET MYDATE=%%i
echo %MYDATE%
当然,最终会出现这样的情况:用更强大的脚本语言重写批处理文件比用这种方式将它与VBScript混合更有意义。
答案 2 :(得分:11)
这适用于非美国日期格式(dd/MM/yyyy
):
set backupFilename=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
7z a QuickBackup%backupFilename%.zip *.backup
答案 3 :(得分:7)
@SETLOCAL ENABLEDELAYEDEXPANSION
@REM Use WMIC to retrieve date and time
@echo off
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF NOT "%%~F"=="" (
SET /A SortDate = 10000 * %%F + 100 * %%D + %%A
set YEAR=!SortDate:~0,4!
set MON=!SortDate:~4,2!
set DAY=!SortDate:~6,2!
@REM Add 1000000 so as to force a prepended 0 if hours less than 10
SET /A SortTime = 1000000 + 10000 * %%B + 100 * %%C + %%E
set HOUR=!SortTime:~1,2!
set MIN=!SortTime:~3,2!
set SEC=!SortTime:~5,2!
)
)
@echo on
@echo DATE=%DATE%, TIME=%TIME%
@echo HOUR=!HOUR! MIN=!MIN! SEC=!SEC!
@echo YR=!YEAR! MON=!MON! DAY=!DAY!
@echo DATECODE= '!YEAR!!MON!!DAY!!HOUR!!MIN!'
输出:
DATE=2015-05-20, TIME= 1:30:38.59
HOUR=01 MIN=30 SEC=38
YR=2015 MON=05 DAY=20
DATECODE= '201505200130'
答案 4 :(得分:5)
您还可以通过变量%DATE%
测试我的系统%DATE%
时会产生ddd dd / mm / yyyy
您可以使用子字符串运算符来生成所需的格式
即。在MON 11/12/2018上使用美国区域设置运行以下内容
%DATE:~3,3% %DATE:~0,3% %DATE:~7,2%
会产生输出:
11 Mon 12
子串参数为
%*variable*:~*startpos*,*numberofchars*%
答案 5 :(得分:3)
这都是尴尬而不是独立的本地设置。这样做:
%CYGWIN_DIR%\bin\date +%%Y%%m%%d_%%H%%M% > date.txt
for /f "delims=" %%a in ('type "date.txt" 2^>NUL') do set datetime=%%a
echo %datetime%
del date.txt
是的,请使用Cygwin日期,所有问题都已消失!
答案 6 :(得分:2)
不确定
FOR %%A IN (%Date:/=%) DO SET Today=%%A
7z a QuickBackup%TODAY%.zip *.backup
这是DDMMYYYY格式。
这是YYYYDDMM:
FOR %%A IN (%Date%) DO (
FOR /F "tokens=1-3 delims=/-" %%B in ("%%~A") DO (
SET Today=%%D%%B%%C
)
)
7z a QuickBackup%TODAY%.zip *.backup
答案 7 :(得分:1)
我使用了这里介绍的环境变量技术:http://cwashington.netreach.net/depo/view.asp?Index=19
以下是该网站的代码:
::~~Author~~. Brett Middleton
::~~Email_Address~~. brettm@arches.uga.edu
::~~Script_Type~~. nt command line batch
::~~Sub_Type~~. Misc
::~~Keywords~~. environment variables
::~~Comment~~.
::Sets or clears a group of environment variables containing components of the current date extracted from the string returned by the DATE /T command. These variables can be used to name files, control the flow of execution, etc.
::~~Script~~.
@echo off
::-----------------------------------------------------------------------------
:: SetEnvDate1.CMD 6/30/98
::-----------------------------------------------------------------------------
:: Description : Sets or clears a group of environment variables containing
:: : components of the current date extracted from the string
:: : returned by the DATE /T command. These variables can be
:: : used to name files, control the flow of execution, etc.
:: :
:: Requires : Windows NT with command extensions enabled
:: :
:: Tested : Yes, as demonstration
:: :
:: Contact : Brett Middleton <brettm@arches.uga.edu>
:: : Animal and Dairy Science Department
:: : University of Georgia, Athens
::-----------------------------------------------------------------------------
:: USAGE
::
:: SetEnvDate1 can be used as a model for coding date/time routines in
:: other scripts, or can be used by itself as a utility that is called
:: from other scripts.
::
:: Run or call SetEnvDate1 without arguments to set the date variables.
:: Variables are set for the day abbreviation (DT_DAY), month number (DT_MM),
:: day number (DT_DD) and four-digit year (DT_YYYY).
::
:: When the variables are no longer needed, clean up the environment by
:: calling the script again with the CLEAR argument. E.g.,
::
:: call SetEnvDate1 clear
::-----------------------------------------------------------------------------
:: NOTES
::
:: A time variable could be added by parsing the string returned by the
:: built-in TIME /T command. This is left as an exercise for the reader. B-)
::
:: This script illustrates the following NT command extensions:
::
:: 1. Use of the extended IF command to do case-insensitive comparisons.
::
:: 2. Use of the extended DATE command.
::
:: 3. Use of the extended FOR command to parse a string returned by a
:: command or program.
::
:: 4. Use of the "()" conditional processing symbols to group commands
:: for conditional execution. All commands between the parens will
:: be executed if the preceeding IF or FOR statement is TRUE.
::-----------------------------------------------------------------------------
if not "%1" == "?" goto chkarg
echo.
echo Sets or clears date/time variables in the command environment.
echo.
echo SetEnvDate1 [clear]
echo.
echo When called without arguments, the variables are created or updated.
echo When called with the CLEAR argument, the variables are deleted.
echo.
goto endit
::-----------------------------------------------------------------------------
:: Check arguments and select SET or CLEAR routine. Unrecognized arguments
:: are ignored and SET is assumed.
::-----------------------------------------------------------------------------
:chkarg
if /I "%1" == "CLEAR" goto clrvar
goto setvar
::-----------------------------------------------------------------------------
:: Set variables for the day abbreviation (DAY), month number (MM),
:: day number (DD) and 4-digit year (YYYY).
::-----------------------------------------------------------------------------
:setvar
for /F "tokens=1-4 delims=/ " %%i IN ('date /t') DO (
set DT_DAY=%%i
set DT_MM=%%j
set DT_DD=%%k
set DT_YYYY=%%l)
goto endit
::-----------------------------------------------------------------------------
:: Clear all variables from the environment.
::-----------------------------------------------------------------------------
:clrvar
for %%v in (DT_DAY DT_MM DT_DD DT_YYYY) do set %%v=
goto endit
:endit
答案 8 :(得分:1)
有一个可用的技术配方here显示如何将其格式化为MMDDYYYY,您应该能够根据自己的需要进行调整。
echo on
@REM Seamonkey’s quick date batch (MMDDYYYY format)
@REM Setups %date variable
@REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
echo %date%
编辑:之前没有成功的原因是因为原文中的'smartquotes'。我修复了它们,如果cut&amp; amp;从这个页面粘贴。
答案 9 :(得分:1)
如前所述,解析日期和时间仅在您知道当前用户使用的格式时才有用(例如,MM / dd / yy或dd-MM-yyyy仅为名称2)。这可以确定,但是当你进行所有压力和解析时,你仍然会遇到一些使用了意外格式的情况,并且需要进行更多的调整。
您还可以使用一些外部程序,它将以您的首选格式返回日期slug,但这样做的缺点是需要使用您的脚本/批处理来分发实用程序。
还有一些以非常原始的方式使用CMOS时钟的批处理技巧,但对于大多数人来说,它太接近于裸线,并且也不总是检索日期/时间的首选位置。
以下是避免上述问题的解决方案。是的,它引入了一些其他问题,但出于我的目的,我发现这是在现代Windows系统的.bat文件中创建日期戳的最简单,最清晰,最便携的解决方案。这只是一个例子,但我想你会看到如何修改其他日期和/或时间格式等。
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyMMdd" /f
@REM the following may be needed to be sure cache is clear before using the new setting
reg query "HKCU\Control Panel\International" /v sShortDate
set LogDate=%date%
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f
答案 10 :(得分:1)
Sairam 通过上面给出的样本,我尝试过&amp;出来了我想要的剧本。其他例子中提到的位置参数给出了不同的结果。我想创建一个Batch文件来每天进行Oracle数据备份(导出数据),保留带有日期和时间的不同DMP文件。时间作为文件名的一部分。这是一个运作良好的脚本:
cls
set dt=%date:~0,2%%date:~3,2%%date:~6,4%-%time:~0,2%%time:~3,2%
set fn=backup-%dt%.DMP
echo %fn%
pause A
exp user/password file=D:\DATA_DMP\%fn%
答案 11 :(得分:1)
如果您启用了WSL(仅限Windows 10),则可以使用区域设置中立方式执行bash。
set dateFile=%TEMP%\currentDate.txt
bash -c "date +%Y%m%d" > %dateFile%
set /p today=<%dateFile%
随意替换文件重定向,并在Windows batch assign output of a program to a variable
处在其他答案中建议使用“for”循环憎恶答案 12 :(得分:0)
无论日期设置如何,我都找到了两种方法。
在我的电脑上,日期/ t返回 2009-05-27
您可以访问注册表并阅读区域设置 (HKEY_CURRENT_USER \控制面板\国际)
或使用vbscript。 这是我前段时间创建的丑陋的批处理文件/ vbscript混合....
@Echo Off
set rnd=%Random%
set randfilename=x%rnd%.vbs
::create temp vbscript file
Echo Dim DayofWeek(7) > %temp%\%randfilename%
Echo DayofWeek(1)="Sun" >> %temp%\%randfilename%
Echo DayofWeek(2)="Mon" >> %temp%\%randfilename%
Echo DayofWeek(3)="Tue" >> %temp%\%randfilename%
Echo DayofWeek(4)="Wed" >> %temp%\%randfilename%
Echo DayofWeek(5)="Thu" >> %temp%\%randfilename%
Echo DayofWeek(6)="Fri" >> %temp%\%randfilename%
Echo DayofWeek(7)="Sat" >> %temp%\%randfilename%
Echo DayofWeek(0)=DayofWeek(Weekday(now)) >> %temp%\%randfilename%
Echo Mon=Left(MonthName(Month(now),1),3) >> %temp%\%randfilename%
Echo MonNumeric=right ( "00" ^& Month(now) , 2) >> %temp%\%randfilename%
Echo wscript.echo ( Year(Now) ^& " " ^& MonNumeric ^& " " ^& Mon ^& " " _ >> %temp%\%randfilename%
Echo ^& right("00" ^& Day(now),2) ^& " "^& dayofweek(0) ^& " "^& _ >> %temp%\%randfilename%
Echo right("00" ^& Hour(now),2)) _ >> %temp%\%randfilename%
Echo ^&":"^& Right("00" ^& Minute(now),2) ^&":"^& Right("00" ^& Second(Now),2) >> %temp%\%randfilename%
::set the output into vars
if "%1" == "" FOR /f "usebackq tokens=1,2,3,4,5,6" %%A in (`start /wait /b cscript //nologo %temp%\%randfilename%`) do Set Y2KYear=%%A& Set MonthNumeric=%%B& Set Month=%%C& Set Day=%%D& Set DayofWeek=%%E& Set Time=%%F
set year=%y2kyear:~2,2%
::cleanup
del %temp%\%randfilename%
它不漂亮,但它有效。
答案 13 :(得分:0)
基于Joe的想法,这里有一个版本,它将构建自己的(.js
)助手和支持时间:
@echo off
set _TMP=%TEMP%\_datetime.tmp
echo var date = new Date(), string, tmp;> "%_TMP%"
echo tmp = ^"000^" + date.getFullYear(); string = tmp.substr(tmp.length - 4);>> "%_TMP%"
echo tmp = ^"0^" + (date.getMonth() + 1); string += tmp.substr(tmp.length - 2);>> "%_TMP%"
echo tmp = ^"0^" + date.getDate(); string += tmp.substr(tmp.length - 2);>> "%_TMP%"
echo tmp = ^"0^" + date.getHours(); string += tmp.substr(tmp.length - 2);>> "%_TMP%"
echo tmp = ^"0^" + date.getMinutes(); string += tmp.substr(tmp.length - 2);>> "%_TMP%"
echo tmp = ^"0^" + date.getSeconds(); string += tmp.substr(tmp.length - 2);>> "%_TMP%"
echo WScript.Echo(string);>> "%_TMP%"
for /f %%i in ('cscript //nologo /e:jscript "%_TMP%"') do set _DATETIME=%%i
del "%_TMP%"
echo YYYYMMDDhhmmss: %_DATETIME%
echo YYYY: %_DATETIME:~0,4%
echo YYYYMM: %_DATETIME:~0,6%
echo YYYYMMDD: %_DATETIME:~0,8%
echo hhmm: %_DATETIME:~8,4%
echo hhmmss: %_DATETIME:~8,6%