环境:win2000server的dos
计划任务:定一个计划任务,每天10点运行a.bat
a.bat的功能:判断日期(1,6,11,16,21,26,月未日)相等时生成另b.bat文件内容如下并自动执行b.bat:
======================================
atx.exe 200501to200502
atx.exe 200412to200501
atx.exe 200411to200412
ren atx1.txt 0501to0502atx.txt
ren atx2.txt 0412to0501atx.txt
ren atx3.txt 0411to0412atx.txt
======================================
b.bat中的日期内容:为自动判断当前日期生成的,上面的规则是当前日期为2005年3月份,如当前是4月份即如下类推:
======================================
atx.exe 200502to200503
atx.exe 200501to200502
atx.exe 200412to200501
ren atx1.txt 0502to0503atx.txt
ren atx2.txt 0501to0502atx.txt
ren atx3.txt 0412to0501atx.txt
======================================
求教:如何编写a.bat程序(不用第三方软件),谢谢指点!
[em06][em06][em06]作者: chenhui530 时间: 2005-3-6 00:00 意思还是不太看得明白作者: 影视狂奔 时间: 2005-3-6 00:00 简单点:win2000server的dos编写一个a.bat批处理实现功能如下:
判断当前日期为(1,6,11,16,21,26,月未日)相等时生成另b.txt文件,
例:当前为3月1日时生成内容如下
atx.exe 200501to200502
ren atx1.txt 0501to0502atx.txt
说明:自动判断当前日期生成的,生成上上月"200501",生成上月"200502"的文本嵌在该txt文件中。[em01][em01][em01]作者: chenhui530 时间: 2005-3-6 00:00 要是是纯粹只用批处理的话不用第三放工具很难,我目前还没想到方法.但是使用VBS就很容易完成作者: chenhui530 时间: 2005-3-6 00:00 搞定:你把条件讲得详细点我把整个批处理全部给你for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set a=%%c
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set b=%%b
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set c=%%a
for %%a in (01 02 03 04 05 06 07 08 09 10 11 12) do if %b%==%%a goto %%a:01
set /a d=%b%+11
set /a e=%b%+10
set /a n=%c%-1
set name=%n%%d%to%n%%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:02
set /a d=%b%+10
set /a e=%b%+9
set /a n=%c%-1
set name=%n%%d%to%n%%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:03
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:04
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:05
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:06
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:07
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:08
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:09
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:10
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:11
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:12
set /a d=%b%-2
set /a e=%b%-1
set name=%c%%d%to%c%%e%
echo atx.exe %name%>b.bat
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:end作者: 影视狂奔 时间: 2005-3-6 00:00 不概高手,条件如下,我睇程序有好多共同点,能否简化?
条件:是遇日期为1,6,11,16,21,26,月未日 的时候才生成文件,其它日无须生成。另:echo ren atx1.txt %name%atx.txt>>b.bat 这句的生成有异。。。
正确的应是:ren atx.txt 0501to0502atx.txt (是没有年份的20的)[em07][em07]作者: chenhui530 时间: 2005-3-6 00:00 不好意思楼主一下忘记判断时间了现在修正代码如下for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next1
goto end:next1作者: chenhui530 时间: 2005-3-6 00:00 怎么搞得又出问题了怎么只出现那么点代码for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set a=%%c
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set b=%%b
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set c=%%a
for %%a in (01 02 03 04 05 06 07 08 09 10 11 12) do if %b%==%%a goto %%a:01
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next1
goto end:next1
set /a d=%b%+11
set /a e=%b%+10
set /a n=%c%-1
set name=%n%%d%to%n%%e%
if %c%==2005 set c=05
set /a n=%c%-1
echo atx.exe %name%>b.bat
echo ren atx1.txt 0%n%%d%to0%n%%e%atx.txt>>b.bat
goto end:02
for %%a in (01 06 11 16 21 26 28) do if %a%==%%a goto next2
goto end:next2
set /a d=%b%+10
set /a e=%b%+9
set /a n=%c%-1
set name=%n%%d%to%n%%e%
if %c%==2005 set c=05
set /a n=%c%-1
echo atx.exe %name%>b.bat
echo ren atx1.txt 0%n%%d%to0%n%%e%atx.txt>>b.bat>>b.bat
goto end:03
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next3
goto end:next3
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:04
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next4
goto end:next4
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:05
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next5
goto end:next5
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:06
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next6
goto end:next6
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:07
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next7
goto end:next7
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:08
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next8
goto end:next8
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:09
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next9
goto end:next9
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:10
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next10
goto end:next10
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:11
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next11
goto end:next11
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:12
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next12
goto end:next12
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%%d%to%c%%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:end作者: 影视狂奔 时间: 2005-3-6 00:00 你好牛啊,真佩服。。
我有个想法,能否将“月未日”的算法改为:以当月的1日减1得到上月的月未日期呢?
如:当前为2005-03-06,取2005-03-01减1得出2005-02-28,这样获到28的日期。另:if %c%==2005 set c=05 这句仅适用于2005年,对于2006年就无效了。[em07][em07]作者: chenhui530 时间: 2005-3-7 00:00 你可以自己添加吗if %c%==2006 set c=06不就行了
楼主你最好把你的详细想法写上我好写出代码来不然等我写出代码来你又改变想法作者: 影视狂奔 时间: 2005-3-7 00:00 你的程序已非常好了,
作者: chenhui530 时间: 2005-3-7 00:00 不存在的我都测试过了批处理结束就会释放内存的作者: smileseeker 时间: 2005-3-9 00:00 楼上的兄弟确实有的:d:>set /a d=08-1
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或八进制(021)。set /a d=09-1也是作者: smileseeker 时间: 2005-3-9 00:00 我用的系统是win2000作者: chenhui530 时间: 2005-3-9 00:00 那也很容易解决只需要在每个月份的标号如:01下接一个命令set %b%=1就行了修改如下:for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set a=%%c
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set b=%%b
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do set c=%%a
for %%a in (01 02 03 04 05 06 07 08 09 10 11 12) do if %b%==%%a goto %%a:01
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next1
goto end:next1
set /a d=%b%+11
set /a e=%b%+10
set /a n=%c%-1
set name=%n%%d%to%n%%e%
if %c%==2005 set c=05
set /a n=%c%-1
echo atx.exe %name%>b.bat
echo ren atx1.txt 0%n%%d%to0%n%%e%atx.txt>>b.bat
goto end:02
for %%a in (01 06 11 16 21 26 28) do if %a%==%%a goto next2
goto end:next2
set /a d=%b%+10
set /a e=%b%+9
set /a n=%c%-1
set name=%n%%d%to%n%%e%
if %c%==2005 set c=05
set /a n=%c%-1
echo atx.exe %name%>b.bat
echo ren atx1.txt 0%n%%d%to0%n%%e%atx.txt>>b.bat>>b.bat
goto end:03
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next3
goto end:next3
set /a d=%b%-2
set /a e=%b%-1
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:04
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next4
goto end:next4
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:05
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next5
goto end:next5
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:06
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next6
goto end:next6
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:07
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next7
goto end:next7
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:08
set %b%=8
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next8
goto end:next8
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:09
set %b%=9
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next9
goto end:next9
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:10
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next10
goto end:next10
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%0%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:11
for %%a in (01 06 11 16 21 26 30) do if %a%==%%a goto next11
goto end:next11
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%0%d%to%c%%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:12
for %%a in (01 06 11 16 21 26 31) do if %a%==%%a goto next12
goto end:next12
set /a d=%b%-2
set /a e=%b%-1
if %c%==2005 set c=05
set name=%c%%d%to%c%%e%
echo atx.exe %name%>b.bat
set name=%c%0%d%to%c%0%e%
echo ren atx1.txt %name%atx.txt>>b.bat
goto end:end作者: 影视狂奔 时间: 2005-3-9 00:00 哈哈,我在你回复前,已修改了,谢谢chenhui530 ,非常感激。。我已将程序发扬光大了。。[em01][em01]作者: chenhui530 时间: 2005-3-10 00:00 呵呵不知道楼主用这个批处理作什么用啊?作者: smileseeker 时间: 2005-3-11 00:00 chenhui530大侠 是不是只有08-1 09-1 不能通过其他的好像都不会提示出错作者: chenhui530 时间: 2005-3-11 00:00