How to append a date in batch files

后端 未结 14 2041
夕颜
夕颜 2020-12-01 03:25

I have the following line in a batch file (that runs on an old Windows 2000 box):

7z a QuickBackup.zip *.backup

How do I append the date to

相关标签:
14条回答
  • 2020-12-01 04:13

    Building on Joe's idea, here is a version which will build its own (.js) helper and supporting time as well:

    @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%
    
    0 讨论(0)
  • 2020-12-01 04:14

    Bernhard's answer needed some tweaking work for me because the %DATE% environment variable is in a different format (as commented elsewhere). Also, there was a tilde (~) missing.

    Instead of:

    set backupFilename=%DATE:~6,4%%DATE:~3,2%%DATE:0,2%

    I had to use:

    set backupFilename=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

    for the date format:

    c:\Scripts>echo %DATE%

    Thu 05/14/2009

    0 讨论(0)
  • 2020-12-01 04:14

    If you know your regional settings won't change you can do it as follows:

    • if your short date format is dd/MM/yyyy:

      SET MYDATE=%DATE:~3,2%%DATE:~0,2%%DATE:~8,4%

    • if your short date format is MM/dd/yyyy:

      SET MYDATE=%DATE:~0,2%%DATE:~3,2%%DATE:~8,4%

    But there's no general way to do it that's independent of your regional settings.

    I would not recommend relying on regional settings for anything that's going to be used in a production environment. Instead you should consider using another scripting language - PowerShell, VBScript, ...

    For example, if you create a VBS file yyyymmdd.vbs in the same directory as your batch file with the following contents:

    ' 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)
    

    then you will be able to call it from your batch file thus:

    FOR /F %%i IN ('cscript "%~dp0yyyymmdd.vbs" //Nologo') do SET MYDATE=%%i
    echo %MYDATE%
    

    Of course there will eventually come a point where rewriting your batch file in a more powerful scripting language will make more sense than mixing it with VBScript in this way.

    0 讨论(0)
  • 2020-12-01 04:16

    Sairam With the samples given above, I have tried & came out with the script which I wanted. The position parameters mentioned in other example gave different results. I wanted to create one Batch file to take the Oracle data backup (export data) on daily basis, preserving distinct DMP files with date & time as part of file name. Here is the script which worked well:

    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%
    
    0 讨论(0)
  • 2020-12-01 04:17

    As has been noted, parsing the date and time is only useful if you know the format being used by the current user (eg. MM/dd/yy or dd-MM-yyyy just to name 2). This could be determined, but by the time you do all the stressing and parsing, you will still end up with some situation where there is an unexpected format used, and more tweaks will be be necessary.

    You can also use some external program that will return a date slug in your preferred format, but that has disadvantages of needing to distribute the utility program with your script/batch.

    there are also batch tricks using the CMOS clock in a pretty raw way, but that is tooo close to bare wires for most people, and also not always the preferred place to retrieve the date/time.

    Below is a solution that avoids the above problems. Yes, it introduces some other issues, but for my purposes I found this to be the easiest, clearest, most portable solution for creating a datestamp in .bat files for modern Windows systems. This is just an example, but I think you will see how to modify for other date and/or time formats, etc.

    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
    
    0 讨论(0)
  • 2020-12-01 04:18
    @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!' 
    

    Output:

    DATE=2015-05-20, TIME= 1:30:38.59
    HOUR=01 MIN=30 SEC=38
    YR=2015 MON=05 DAY=20
    DATECODE= '201505200130'
    
    0 讨论(0)
提交回复
热议问题