只有在某一天才执行……批处理文件

8

你好,我有一个批处理文件,类似于这样:

if %day%==monday, tuesday, wednesday, thursday, friday (
goto yes
) else (
goto no
)

现在我知道第一行不起作用。

我实际想要发生的事情是:

它会自动检查今天是星期几。如果是周一到周五,它必须跳转到“是”,否则(星期六/星期日)跳转到“否”。

如何做到这一点?

7个回答

10

这里有一个示例的批处理文件,可以完成这种操作。我相信你可以想到其他使用此示例代码的方法。例如,在任何需要“in”列表的情况下,都可以使用它。关键之处在于 “%date:~0,3% ”,它表示展开 %date% 环境变量,并从位置 0 开始返回字符串的下一个 3 个字符。你可以通过 "set /?" 命令了解更多信息。

示例:IsWeekDay.bat

@echo off
setlocal

for %%i in (Mon,Tue,Wed,Thu,Fri) do (
    if "%date:~0,3%"=="%%i" goto YES
)

:NO
echo No
goto EOF

:YES
echo Yes

:EOF
endlocal

+1 很好。它在我的Windows7上运行良好。@YourComputerHelpZ,你能否从命令行中使用%date%和date /t命令,并告诉我们你得到了什么? - Preet Sangha

5
正如Jay所提到的那样,使用date /t仅适用于输出日期和星期几的语言环境,并不适用于其他语言环境(例如俄语)。如果您不介意在批处理文件中混合一些VBScript,请使用下面的解决方案,这将适用于所有语言环境。
关键是这个微小的VBScript脚本,它以数字形式输出星期几(1 =星期日,2 =星期一,... 7 =星期六):
WScript.Echo DatePart("w", Date)

你可以从批处理文件中运行此脚本,读取其输出并应用你的逻辑:
for /f %%d in ('cscript dayofweek.vbs //nologo') do (
  if %%d==7 goto no   :: Saturday
  if %%d==1 goto no   :: Sunday
)
goto yes

5

我在网上找到了这个。经过测试,它可以工作。返回一个整数表示的日期,您仍然可以处理。

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)

@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%

1
请注意,在日期分隔符不是“/”的区域,此代码将无法正常工作。 - laalto
2
这不是一个可靠的解决方案,因为当我在这里使用'date /t'时,我只得到了短格式(YYYY-MM-DD),因此没有星期几。这与区域设置有关。如果这是用于出口(任何人不是您自己),我不会打赌这能按预期工作。 - Jay

0

这个答案,我们得到了:

wmic path win32_localtime get dayofweek

根据这个帖子中的建议,我们可以扩展这个内容并设置一个星期几的变量:

@echo off
REM Unset dayofweek in case set in a previous execution of this batch file
set dayofweek=
REM Get dayofweek into a variable.  Locale-specific:  0 is either Sunday or Monday.
for /F "skip=1 tokens=*" %%a in ('wmic path win32_localtime get dayofweek') do if not defined dayofweek set dayofweek=%%a
echo %dayofweek%

请注意,0 可以是星期天或星期一,具体取决于您的语言环境。

0
IF %day% == monday GOTO YES
IF %day% == tuesday GOTO YES
IF %day% == wednesday GOTO YES
IF %day% == thursday GOTO YES
IF %day% == friday GOTO YES
GOTO NO

1
它真的是自动完成的吗,还是需要用户手动输入? - Deniz Zoeteman
我假设 %day% 已经设置了,他没有完全理解分支。 - Randolpho

0

我没有足够的批处理知识来回答这个问题,但是假设这是一个Windows批处理文件,考虑使用任务计划程序来执行脚本,这将让您设置所需的计划类型。


我理解你的意思,但我也必须给出一个具体的时间。批处理文件将在启动时运行。 - Deniz Zoeteman
您可以在任务计划程序中设置触发器以执行“启动时”或“登录时”的操作。不需要指定具体时间。 - Mark Berry

0
这是一个批处理文件,以几乎与语言环境无关的方式提取星期几、日、月和年。
唯一与语言环境有关的是星期几的拼写,其余都是与语言环境无关的。
因此,在英语中,它将返回星期四的Thu,但在荷兰语中,它将是do(表示星期四)。
:: https://dev59.com/sXVC5IYBdhLWcg3wtzqs
:: Works on any NT/2k machine independent of regional date settings
::
:: 20110103 - adapted by jeroen@pluimers.com for Dutch locale
:: 20110303 - adapted by jeroen@pluimers.com for day-of-week
:: Dutch will get jj as year from echo:^|date, so the '%%c' trick does not work as it will fill 'jj', but we want 'yy'
:: luckily, all countries seem to have year at the end: http://en.wikipedia.org/wiki/Calendar_date
::            set '%%c'=%%k
::            set 'yy'=%%k
::
:: Also, in Dutch, there is a difference between %date% and date/t: the former will not include
:: the day-of-the-week, but the latter will.
:: That means the if "%date%A" LSS "A" trick does not work with %date%, we need a loop
:: to check if the day-of-the-week needs us to take tokens 2-4 in stead of 1-3:
::      if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
::      for /f "tokens=1" %%t in ('date/t') do (...)
::
:: Another difference between Dutch and English is that the Dutch date/t will prepend the day of the week in a lower case 2-letter form.
:: So the LSS "A" trick needs to be replaced with an LSS "a" trick
::      if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
::        if "%%ta" LSS "a" (set toks=1-3) else (set toks=2-4)
::
:: In addition, date will display the current date before the input prompt using dashes
:: in Dutch, but using slashes in English, so there will be two occurances of the outer loop in Dutch
:: and one occurence in English.
:: This skips the first iteration:
::        if "%%a" GEQ "A"
::
:: echo:^|date
:: Huidige datum: ma 03-01-2011
:: Voer de nieuwe datum in: (dd-mm-jj)
:: The current date is: Mon 01/03/2011
:: Enter the new date: (mm-dd-yy)
::
:: date/t
:: ma 03-01-2011
:: Mon 01/03/2011
::
:: The assumption in this batch-file is that echo:^|date will return the date format
:: using either mm and dd or dd and mm in the first two valid tokens on the second line, and the year as the last token.
::
:: The outer loop will get the right tokens, the inner loop assigns the variables depending on the tokens.
:: That will resolve the order of the tokens.
::
@ECHO off
    set v_day_of_week=
    set v_day=
    set v_month=
    set v_year=

    SETLOCAL ENABLEEXTENSIONS
      for /f "tokens=1" %%t in ('date/t') do (
        set v_day_of_week=%%t
        if "%%ta" LSS "a" (set toks=1-3) else (set toks=2-4)
      )
::DEBUG echo toks=%toks%
      for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
::DEBUG echo first token=%%a
        if "%%a" GEQ "A" (
          for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
            set '%%a'=%%i
            set '%%b'=%%j
            set 'yy'=%%k
          )
        )
      )
      if %'yy'% LSS 100 set 'yy'=20%'yy'%
      set Today=%'yy'%-%'mm'%-%'dd'%

    ENDLOCAL & SET day_of_week=%v_day_of_week% & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%

    ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
    set datestring=%V_Year%%V_Month%%V_Day%
    echo %datestring%
    echo day of week=%day_of_week%

    :EOF

玩得开心!

--jeroen


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接