dos 定时执行作业 dos timer

本文介绍了一种使用Bat脚本实现的简单定时任务解决方案。通过dos_timer_cn.bat和dos_timer_en.bat两个示例脚本,展示了如何设置特定时间执行Bat文件或调用Windows控制台应用程序。此外,还提供了调用PowerShell脚本的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在定时作业的工具语言很多,最方便的个人认为wondows人肯定数bat莫属,还有一点复古feel,

dos_time_cn.bat 可以定时call 一个bat文件,或者是windows console app 程序

不同区域下的终端时间显示不同,所以准备了两个文件方便日后使用

  1. dos_timer_cn.bat for 中文环境
  2. dos_timer_en.bat for 英文环境

实例1

a) 测试bat文件 test.bat

echo this is a test!

b) dos_timer_cn.bat
mode   con:lines=6
mode   con:cols=67
@echo off & setLocal EnableDelayedExpansion
:start
cls
if "%1"=="" (@set dd=%date:~0,4%%date:~5,2%%date:~8,2%) else @set dd=%1
if "%2"=="" (@set tt=160902) else @set tt=%2
@echo Job %3 will run at %dd:~0,4%-%dd:~4,2%-%dd:~6,2% on %tt:~0,2%:%tt:~2,2%:%tt:~4,2%
@echo Times now: %date:~0,4%%date:~5,2%%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%
@set d= %date:~0,4%%date:~5,2%%date:~8,2%
@set t=%time:~0,2%%time:~3,2%%time:~6,2%
if %d% geq %dd% goto s1
goto exit
:s1
if %t% geq %tt% goto s2
goto exit
:s2
ECHO START TO RUN %3 ...
TIMEOUT 10
if "%3"=="" ( @echo Hello world!) else @call %3
goto out
:exit
timeout 3 /NOBREAK
goto start
:out
if %errorlevel%==0 (echo %3 finished at %date% %time%) else echo running %3 failed at %date% %time%
pause
exit

c) 开始-》运行-》cmd回车

d) dos_timer_cn.bat 20160927 123000 test.bat

e) 上图正在等待,伺机执行指定bat .test.bat


f) 12.半执行了test.bat echo this is a test

实例二,call powershell,dos 调用powershell文件

a) 目录结构


b)

test_powershell.bat

powershell -File %cd%\show_hostmsg.ps1
pause

show_hostmsg.ps1

$host

c)命令:dos_timer_cn.bat 20160929 124700 test_powershell.bat


d)结果1


e)结果2

附件dos_timer_en.bat

mode   con:lines=6
mode   con:cols=67
@echo off & setLocal EnableDelayedExpansion
:start
cls
if "%1"=="" (@set dd=%date:~6,4%%date:~0,2%%date:~3,2%) else @set dd=%1
if "%2"=="" (@set tt=160902) else @set tt=%2
@echo Job %3 will run at %dd:~0,4%-%dd:~4,2%-%dd:~6,2% on %tt:~0,2%:%tt:~2,2%:%tt:~4,2%
@echo Times now: %date:~6,4%-%date:~0,2%-%date:~3,2% %time:~0,2%:%time:~3,2%:%time:~6,2%
@set d= %date:~6,4%%date:~0,2%%date:~3,2%
@set t=%time:~0,2%%time:~3,2%%time:~6,2%
if %d% geq %dd% goto s1
goto exit
:s1
if %t% geq %tt% goto s2
goto exit
:s2
ECHO START TO RUN %3 ...
TIMEOUT 10
if "%3"=="" ( @echo Hello world!) else @call %3
goto out
:exit
timeout 3 /NOBREAK
goto start
:out
if %errorlevel%==0 (echo %3 finished at %date% %time%) else echo running %3 failed at %date% %time%
pause
exit

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值