只做日...批处理文件

时间:2022-06-14 05:38:00

hello i got a batch file, something like this:

你好,我有一个批处理文件,如下所示:

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

Now i know the first line won't work.

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

What i actually want to happen:

我真正想要发生的事情:

It automatticly checks which day it is. If it is Monday to Friday, it has to go to 'yes', otherwise (saturday/sunday) to 'no'.

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

How to do this?

这该怎么做?

7 个解决方案

#1


I ran across this online. Tested, and it works. Returns the day as an integer, which you can still work with.

我在网上跑过这个。测试,它的工作原理。以整数形式返回日期,您仍然可以使用它。

@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%

#2


Here is an example bat file that will do this sort of thing I am sure you can think of other ways to use this sample code. For instance anytime you need an "in" list. The tricky bit is the %date:~0,3% this says expand the %date% environment variable and starting at position 0 the beginning of the string return the next 3 characters. You can learn more about this from the "set /?" command.

这是一个示例bat文件,它将执行此类事情我相信您可以考虑使用此示例代码的其他方法。例如,任何时候你需要一个“in”列表。棘手的一点是%date:~0.3%这表示扩展%date%环境变量并从位置0开始,字符串的开头返回接下来的3个字符。你可以从“set /?”中了解更多相关信息。命令。

example: 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

#3


As Jay has mentioned, using date /t will only work on locales where this command outputs the day of week along with the date, and won't work on other locales (e.g. Russian). If you don't mind mixing your batch files with some VBScript, here's a solution that should work on all locales.

正如Jay所提到的,使用date / t只能在这个命令输出星期几和日期的语言环境中工作,并且不适用于其他语言环境(例如俄语)。如果您不介意将批处理文件与某些VBScript混合,这里的解决方案应适用于所有语言环境。

The trick is this tiny VBScript script that outputs the day of the week as a number (1 = Sunday, 2 = Monday, ... 7 = Saturday):

诀窍是这个微小的VBScript脚本输出星期几作为数字(1 =星期日,2 =星期一,... 7 =星期六):

WScript.Echo DatePart("w", Date)

You can run this script from your batch file, read its output and apply your logic:

您可以从批处理文件运行此脚本,读取其输出并应用您的逻辑:

for /f %%d in ('cscript dayofweek.vbs //nologo') do (
  if %%d==7 goto no   :: Saturday
  if %%d==1 goto no   :: Sunday
)
goto yes

#4


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

#5


I don't have the batch fu to answer the question as asked, but, assuming this is a Windows batch file, consider executing the script using the task scheduler, which will let you set the kind of schedule you're asking for.

我没有批处理fu来回答问题,但是,假设这是一个Windows批处理文件,请考虑使用任务计划程序执行脚本,这将允许您设置您要求的计划类型。

#6


Here is a batch file that extracts day-of-week, day, month and year in an almost locale-neutral way.

这是一个批处理文件,以几乎与语言环境无关的方式提取星期几,日,月和年。

The only locale specific thing is the spelling of the day-of-week, the rest is locale neutral.

唯一的区域设置特定事项是星期几的拼写,其余的是区域中性。

So in English, it will return Thu for Thursday, but in Dutch that will be do (for donderdag).

因此在英语中,它将在星期四返回星期四,但在荷兰语中将会返回(对于donderdag)。

:: http://*.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-usi
:: 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

Have fun with it!

玩得开心!

--jeroen

#7


From this answer, we have

从这个答案,我们有

wmic path win32_localtime get dayofweek

Expanding on this based on suggestions in this thread, we can set a dayofweek variable as follows:

根据此线程中的建议对此进行扩展,我们可以设置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%

Note that 0 can be Sunday or Monday depending your locale.

请注意,0可以是周日或周一,具体取决于您的区域设置。

#1


I ran across this online. Tested, and it works. Returns the day as an integer, which you can still work with.

我在网上跑过这个。测试,它的工作原理。以整数形式返回日期,您仍然可以使用它。

@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%

#2


Here is an example bat file that will do this sort of thing I am sure you can think of other ways to use this sample code. For instance anytime you need an "in" list. The tricky bit is the %date:~0,3% this says expand the %date% environment variable and starting at position 0 the beginning of the string return the next 3 characters. You can learn more about this from the "set /?" command.

这是一个示例bat文件,它将执行此类事情我相信您可以考虑使用此示例代码的其他方法。例如,任何时候你需要一个“in”列表。棘手的一点是%date:~0.3%这表示扩展%date%环境变量并从位置0开始,字符串的开头返回接下来的3个字符。你可以从“set /?”中了解更多相关信息。命令。

example: 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

#3


As Jay has mentioned, using date /t will only work on locales where this command outputs the day of week along with the date, and won't work on other locales (e.g. Russian). If you don't mind mixing your batch files with some VBScript, here's a solution that should work on all locales.

正如Jay所提到的,使用date / t只能在这个命令输出星期几和日期的语言环境中工作,并且不适用于其他语言环境(例如俄语)。如果您不介意将批处理文件与某些VBScript混合,这里的解决方案应适用于所有语言环境。

The trick is this tiny VBScript script that outputs the day of the week as a number (1 = Sunday, 2 = Monday, ... 7 = Saturday):

诀窍是这个微小的VBScript脚本输出星期几作为数字(1 =星期日,2 =星期一,... 7 =星期六):

WScript.Echo DatePart("w", Date)

You can run this script from your batch file, read its output and apply your logic:

您可以从批处理文件运行此脚本,读取其输出并应用您的逻辑:

for /f %%d in ('cscript dayofweek.vbs //nologo') do (
  if %%d==7 goto no   :: Saturday
  if %%d==1 goto no   :: Sunday
)
goto yes

#4


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

#5


I don't have the batch fu to answer the question as asked, but, assuming this is a Windows batch file, consider executing the script using the task scheduler, which will let you set the kind of schedule you're asking for.

我没有批处理fu来回答问题,但是,假设这是一个Windows批处理文件,请考虑使用任务计划程序执行脚本,这将允许您设置您要求的计划类型。

#6


Here is a batch file that extracts day-of-week, day, month and year in an almost locale-neutral way.

这是一个批处理文件,以几乎与语言环境无关的方式提取星期几,日,月和年。

The only locale specific thing is the spelling of the day-of-week, the rest is locale neutral.

唯一的区域设置特定事项是星期几的拼写,其余的是区域中性。

So in English, it will return Thu for Thursday, but in Dutch that will be do (for donderdag).

因此在英语中,它将在星期四返回星期四,但在荷兰语中将会返回(对于donderdag)。

:: http://*.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-usi
:: 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

Have fun with it!

玩得开心!

--jeroen

#7


From this answer, we have

从这个答案,我们有

wmic path win32_localtime get dayofweek

Expanding on this based on suggestions in this thread, we can set a dayofweek variable as follows:

根据此线程中的建议对此进行扩展,我们可以设置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%

Note that 0 can be Sunday or Monday depending your locale.

请注意,0可以是周日或周一,具体取决于您的区域设置。