Board logo

标题: [日期时间] [已解决]批处理如何编写按时间要求执行程序的命令? [打印本页]

作者: wwjpl    时间: 2013-2-28 02:57     标题: [已解决]批处理如何编写按时间要求执行程序的命令?

具体要求:
如果今天是星期四,则执行xcopy xxxx xxxx,接着再按顺序执行后面的命令,如果不是星期四则跳过xcopy xxxx xxxx命令,按顺序执行后面的命令。
为什么运行到空行之后就不能再运行后面的命令了,也就是运行到“start %temp%\aaa.bat”之后,后面的命令就不能被执行了,另外:在运行这个P的时候会有一个黑的P窗口出现,如何让他不出现。求大大们帮忙,谢谢
  1. ::每星期四备份一次收藏夹
  2. echo if  "%%date:~-1%%"  == "四" call:thursday>>%temp%\aaa.bat
  3. echo goto :eof>>%temp%\aaa.bat
  4. echo :thursday>>%temp%\aaa.bat
  5. echo xcopy /e /h /i /k /y D:\Favorites\* E:\Favorites>>%temp%\aaa.bat
  6. echo goto :eof>>%temp%\aaa.bat
  7. start %temp%\aaa.bat
  8. taskkill /f /im IExplore.exe
  9. del /f /s /q "%userprofile%\Local Settings\History\*"
  10. attrib -s -h "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  11. del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  12. rd /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5">nul 2>nul
复制代码
以下是我自己的解决方法(其实大同小异),留给和我一样不懂批处理的人的一个参考,高手莫笑
  1. @echo off
  2. ::每周四备份IE收藏夹到"E:\Favorites"下
  3. Setlocal enabledelayedexpansion
  4. for /f "skip=2 delims=: tokens=1,*" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Favorites') do (
  5.    set str=%%i
  6.    set var=%%j
  7.    set "var=!var:"=!"
  8.    if not "!var:~-1!"=="=" echo xcopy /e /h /i /k /y !str:~-1!:!var!\* e:\Favorites\>%temp%\aaa.bat
  9. )
  10. if  "%date:~-1%"  == "四" call %temp%\aaa.bat
  11. taskkill /f /im IExplore.exe
  12. del /f /s /q "%userprofile%\Local Settings\History\*"
  13. attrib -s -h "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  14. del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  15. rd /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5">nul 2>nul
复制代码

作者: Batcher    时间: 2013-2-28 10:50

这里有个函数可以判断星期几
http://www.bathome.net/thread-3056-1-1.html
作者: wwjpl    时间: 2013-2-28 12:53

本帖最后由 wwjpl 于 2013-3-2 00:38 编辑

回复 2# Batcher


    谢谢你的帮助,我知道用“%date%”这个命令判断今天是星期几,只是后面的连接部分不会写,也就是满足条件进行拷贝命令不会连接。
我不会解决这个问题,我就用输出的笨办法调用批处理,可问题是运行到空行之后就不能再运行后面的命令了,也就是运行到“start %temp%\aaa.bat”之后,后面的命令就不能被执行了,求大大帮忙,谢谢
  1. ::每星期四备份一次收藏夹
  2. echo if  "%%date:~-1%%"  == "四" call:thursday>>%temp%\aaa.bat
  3. echo goto :eof>>%temp%\aaa.bat
  4. echo :thursday>>%temp%\aaa.bat
  5. echo xcopy /e /h /i /k /y D:\Favorites\* E:\Favorites>>%temp%\aaa.bat
  6. echo goto :eof>>%temp%\aaa.bat
  7. start %temp%\aaa.bat
  8. taskkill /f /im IExplore.exe
  9. del /f /s /q "%userprofile%\Local Settings\History\*"
  10. attrib -s -h "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  11. del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  12. rd /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5">nul 2>nul
复制代码

作者: BAT-VBS    时间: 2013-3-1 23:17

回复 3# wwjpl


    你的start命令在哪里?是不是代码贴错了
作者: wwjpl    时间: 2013-3-2 00:42

本帖最后由 wwjpl 于 2013-3-2 00:47 编辑

回复 4# BAT-VBS


    帖子已经更正过了,加入了“start”之后可以运行正常了,可是会出现一个DOS窗口,我不想他出现,
我尝试在开头加入“echo if not "%%1"=="wind" mshta vbscript:createobject("wscript.shell").run("""%%~f0"" wind",vbhide)(window.close)^&^&exit>>%temp%\aaa.bat”,运行后还是会出现DOS窗口,请问如何解决?谢谢帮助。
作者: BAT-VBS    时间: 2013-3-2 09:36

回复 5# wwjpl
  1. ::每星期四备份一次收藏夹
  2. echo if not "%%1"=="wind" mshta vbscript:createobject("wscript.shell").run("""%%~f0"" wind",vbhide)(window.close)^&^&exit>%temp%\aaa.bat
  3. echo if  "%%date:~-1%%"  == "四" call:thursday>>%temp%\aaa.bat
  4. echo goto :eof>>%temp%\aaa.bat
  5. echo :thursday>>%temp%\aaa.bat
  6. echo xcopy /e /h /i /k /y D:\Favorites\* E:\Favorites>>%temp%\aaa.bat
  7. echo goto :eof>>%temp%\aaa.bat
  8. start %temp%\aaa.bat
  9. taskkill /f /im IExplore.exe
  10. del /f /s /q "%userprofile%\Local Settings\History\*"
  11. attrib -s -h "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  12. del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5\*">nul 2>nul
  13. rd /s /q "%userprofile%\Local Settings\Temporary Internet Files\Content.IE5">nul 2>nul
复制代码
你的代码是这样写的吗?
start命令加个/b可以吗?
作者: wwjpl    时间: 2013-3-2 13:37

回复 6# BAT-VBS

首先 谢谢你的帮助
    “/b”这个参数我没试过,不过我把start改成call完美执行了,不过最终我是用“if  "%date:~-1%"  == "六" call %temp%\aaa.bat”这个命令解决问题的,具体见顶楼。

再次谢谢你的帮助。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2