在Windows cmd脚本中添加%TIME%变量

时间:2022-09-02 16:36:44

I realize that this could probably be done easier in any number of other scripting languages but started to do it quick in cmd and now Im curious.

我意识到这可能在任何其他脚本语言中都可以更容易地完成,但是开始在cmd中快速完成,现在我很好奇。

Looking to start a process at an offset to the time that another process started. Lets say 5 minutes to keep it simple. Is there a way to add to the %TIME% variable?

希望以相对于另一个流程开始的时间偏移开始流程。让我们说5分钟,以保持简单。有没有办法添加到%TIME%变量?

For instance:

start /b foo.exe
at %TIME% + 5 minutes bar.exe

Thanks for any assistance

谢谢你的帮助

6 个解决方案

#1


13  

I just typed set/? and discovered cmd is much better than old bat... :-)

我只是键入set /?并发现cmd比老蝙蝠好多了...... :-)

set h=%TIME:~0,2%
set m=%TIME:~3,2%
set/a m2="m+5"
set t2=%h%:%m2%
set t2

Obviously, you can get 62 minutes, etc., I let you do the extra math... :-D

显然,你可以得到62分钟等等,我让你做额外的数学...... :-D

#2


3  

Here is the extra math:

这是额外的数学:

:: Get an offset of 1 minute from the current time, accounting for edge cases
set hr=%TIME:~0,2%
set /a mn=%TIME:~3,2%+1
:: check minutes; greater than 60? add n/60 to hour, n%60 to mins
if %mn% geq 60 set /a hr=hr+mn/60 && set /a mn=mn%%60
if %hr% geq 24 set /a h=0
:: pad zeros if necessary
if %mn% leq 9 set mn=0%mn%
if %hr% leq 9 set hr=0%hr%

#3


1  

2 PhiLho

thanks

My bath code:

我的浴室代码:

set h=%TIME:~0,2%
set m=%TIME:~3,2%
set s=%TIME:~6,2%
set time=%h%_%m%_%s%
mkdir c:\test\%time%
move c:\test\*.* c:\test\%time%
"C:\Program Files\WinRAR\RAR" a -ep c:\test\%time%\ -agDD-MM-YY_hh-mm-ss c:\test\%time%\*.* -zc:\arhiv\HEADERS.TXT
move c:\test\%time%\*.rar c:\arhiv\
rmdir c:\test\%time% /S /Q

#4


1  

        @echo off
        :: START USAGE  ==================================================================
        ::SET THE NICETIME 
        :: SET NICETIME=BOO
        :: CALL GetNiceTime.cmd 

        :: ECHO NICETIME IS %NICETIME%

        :: echo nice time is %NICETIME%
        :: END USAGE  ==================================================================

        echo set hhmmsss
        :: this is Regional settings dependant so tweak this according your current settings
        for /f "tokens=1-3 delims=:" %%a in ('echo %time%') do set hhmmsss=%%a%%b%%c 
        ::DEBUG ECHO hhmmsss IS %hhmmsss%
        ::DEBUG PAUSE
        echo %yyyymmdd%
            :: this is Regional settings dependant so tweak this according your current settings
        for /f "tokens=1-3 delims=." %%D in ('echo %DATE%') do set  yyyymmdd=%%F%%E%%D
        ::DEBUG ECHO yyyymmdd IS %yyyymmdd%
        ::DEBUG PAUSE


        set NICETIME=%yyyymmdd%_%hhmmsss%
        ::DEBUG echo THE NICETIME IS %NICETIME%

        ::DEBUG PAUSE

#5


0  

copy.cmd:

set hr=%TIME:~0,2%
if %hr% leq 9 set hr=0%hr:~1,1%
set mydate=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%-%hr%%TIME:~3,2%%TIME:~6,2%
copy file.txt file-%mydate%.txt
pause

#6


0  

Extended the "bath code" a little bit:

延长了“浴室代码”一点点:

echo ----- time -----
echo %time%
set _hh=%time:~0,2%
set _mm=%time:~3,2%
set _ss=%time:~6,2%
set __time=%_hh%%_mm%%_ss%
echo _%__time%_
echo ----- time -----
echo:
echo ----- date -----
echo %date%
set _Y=%date:~0,4%
set _M=%date:~5,2%
set _D=%date:~8,2%
set __date=%_Y%%_M%%_D%
echo _%__date%_
echo ----- date -----
pause

Outputs 205146 for %__time%

%__ time%的输出205146

Outputs 20180613 for %__date%

%__ date%的输出20180613

#1


13  

I just typed set/? and discovered cmd is much better than old bat... :-)

我只是键入set /?并发现cmd比老蝙蝠好多了...... :-)

set h=%TIME:~0,2%
set m=%TIME:~3,2%
set/a m2="m+5"
set t2=%h%:%m2%
set t2

Obviously, you can get 62 minutes, etc., I let you do the extra math... :-D

显然,你可以得到62分钟等等,我让你做额外的数学...... :-D

#2


3  

Here is the extra math:

这是额外的数学:

:: Get an offset of 1 minute from the current time, accounting for edge cases
set hr=%TIME:~0,2%
set /a mn=%TIME:~3,2%+1
:: check minutes; greater than 60? add n/60 to hour, n%60 to mins
if %mn% geq 60 set /a hr=hr+mn/60 && set /a mn=mn%%60
if %hr% geq 24 set /a h=0
:: pad zeros if necessary
if %mn% leq 9 set mn=0%mn%
if %hr% leq 9 set hr=0%hr%

#3


1  

2 PhiLho

thanks

My bath code:

我的浴室代码:

set h=%TIME:~0,2%
set m=%TIME:~3,2%
set s=%TIME:~6,2%
set time=%h%_%m%_%s%
mkdir c:\test\%time%
move c:\test\*.* c:\test\%time%
"C:\Program Files\WinRAR\RAR" a -ep c:\test\%time%\ -agDD-MM-YY_hh-mm-ss c:\test\%time%\*.* -zc:\arhiv\HEADERS.TXT
move c:\test\%time%\*.rar c:\arhiv\
rmdir c:\test\%time% /S /Q

#4


1  

        @echo off
        :: START USAGE  ==================================================================
        ::SET THE NICETIME 
        :: SET NICETIME=BOO
        :: CALL GetNiceTime.cmd 

        :: ECHO NICETIME IS %NICETIME%

        :: echo nice time is %NICETIME%
        :: END USAGE  ==================================================================

        echo set hhmmsss
        :: this is Regional settings dependant so tweak this according your current settings
        for /f "tokens=1-3 delims=:" %%a in ('echo %time%') do set hhmmsss=%%a%%b%%c 
        ::DEBUG ECHO hhmmsss IS %hhmmsss%
        ::DEBUG PAUSE
        echo %yyyymmdd%
            :: this is Regional settings dependant so tweak this according your current settings
        for /f "tokens=1-3 delims=." %%D in ('echo %DATE%') do set  yyyymmdd=%%F%%E%%D
        ::DEBUG ECHO yyyymmdd IS %yyyymmdd%
        ::DEBUG PAUSE


        set NICETIME=%yyyymmdd%_%hhmmsss%
        ::DEBUG echo THE NICETIME IS %NICETIME%

        ::DEBUG PAUSE

#5


0  

copy.cmd:

set hr=%TIME:~0,2%
if %hr% leq 9 set hr=0%hr:~1,1%
set mydate=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%-%hr%%TIME:~3,2%%TIME:~6,2%
copy file.txt file-%mydate%.txt
pause

#6


0  

Extended the "bath code" a little bit:

延长了“浴室代码”一点点:

echo ----- time -----
echo %time%
set _hh=%time:~0,2%
set _mm=%time:~3,2%
set _ss=%time:~6,2%
set __time=%_hh%%_mm%%_ss%
echo _%__time%_
echo ----- time -----
echo:
echo ----- date -----
echo %date%
set _Y=%date:~0,4%
set _M=%date:~5,2%
set _D=%date:~8,2%
set __date=%_Y%%_M%%_D%
echo _%__date%_
echo ----- date -----
pause

Outputs 205146 for %__time%

%__ time%的输出205146

Outputs 20180613 for %__date%

%__ date%的输出20180613