2009-06-01 5 views
8

hallo ich habe eine Batch-Datei, so etwas wie diese:nur tun, wenn Tag ... Batch-Datei

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

Jetzt weiß ich die erste Zeile wird nicht funktionieren.

Was ich will, tatsächlich geschehen:

Es automatticly Kontrollen, die es ist. Wenn es Montag bis Freitag ist, muss es auf "Ja" gehen, ansonsten (Samstag/Sonntag) auf "Nein".

Wie geht das?

Antwort

5

Ich lief online über diese. Getestet, und es funktioniert. Gibt den Tag als Integer zurück, mit dem Sie noch arbeiten können.

@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

Beachten Sie, dass dies bei Gebietsschemas nicht funktioniert, bei denen das Datumstrennzeichen nicht ist/ – laalto

+2

Dies ist keine zuverlässige Lösung, da ich hier nur 'date/t' verwende, ist die kurze (JJJJ-MM-DD), also kein Tagesname. Geht mit den Locales. Wenn dies für den Export bestimmt ist (jemand, der nicht Sie ist), würde ich nicht darauf wetten, dass dies wie beabsichtigt funktioniert. – Jay

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

macht es wirklich von selbst, oder muss der Benutzer es eingeben? –

+2

Wer setzt% Tag%? – criddell

+0

Ich nahm an, dass% day% bereits gesetzt war und er Branching nicht ganz verstand. – Randolpho

0

Ich habe nicht die Batch-fu die Frage gefragt, aber, vorausgesetzt, dies ist eine Windows-Batch-Datei, sollten Sie die Ausführung des Skripts mit dem Task-Scheduler zu beantworten, die Ihnen die Art der Zeitplan festgelegt werden lassen du fragst nach.

+0

ich verstehe dich ,, aber dann muss ich auch eine bestimmte Zeit geben. Die BATCH-Datei wird nur beim Start ausgeführt. –

+0

Sie können einen Trigger im Taskplaner so einstellen, dass er "Beim Start" oder "Beim Anmelden" ausführt. Muss keine bestimmte Zeit haben. –

9

Hier ist ein Beispiel bat Datei, die diese Art von Sache tun wird, ich bin sicher, dass Sie an andere Möglichkeiten denken können, diesen Beispielcode zu verwenden. Zum Beispiel, wenn Sie eine "In" -Liste benötigen. Das knifflige Bit ist das% date: ~ 0,3%, das besagt, dass die Umgebungsvariable% date% erweitert wird und beginnend mit Position 0 der Anfang der Zeichenkette die nächsten 3 Zeichen zurückgibt. Mehr dazu erfahren Sie im "set /?" Befehl.

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

hmm ... das scheint bei mir nicht zu funktionieren. –

+0

+1 Ausgezeichnet. Es funktioniert auf Window7 für mich. @IhrComputerHelpZ können Sie% date% von der Kommandozeile und auch date/t. Kannst du uns sagen, was du bekommst? –

5

Wie Jay erwähnt, date /t verwendet, wird nur auf Gegenden arbeiten, wo dieser Befehl gibt den Tag der Woche zusammen mit dem Datum, und nicht auf andere arbeiten Gebietsschemas (zB Russisch). Wenn es Ihnen nichts ausmacht, Ihre Batch-Dateien mit VBScript zu mischen, ist hier eine Lösung, die für alle Gebietsschemas funktionieren sollte.

Der Trick ist, diese kleine VBScript Skript, das den Tag der Woche als Zahl gibt (1 = Sonntag, 2 = Montag, ... 7 = Samstag):

WScript.Echo DatePart("w", Date) 

Sie dieses Skript ausführen kann aus dem Batch-Datei, dessen Ausgang lesen und Ihre Logik anwenden:

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

Hier ist eine Batchdatei, die Tag-of-Woche, Tag, Monat und Jahr in einer fast locale neutral extrahiert.

Die einzige locale spezifische Sache ist die Schreibweise des Wochentags, der Rest ist locale neutral.

Also in Englisch, wird es für Donnerstag zurückgeben, aber in Niederländisch wird das do sein (für donderdag).

:: http://stackoverflow.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 [email protected] for Dutch locale 
:: 20110303 - adapted by [email protected] 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 

Viel Spaß damit!

--jeroen

0

Von this answer, haben wir

wmic path win32_localtime get dayofweek 

auf diese Vorschläge in this thread basierend Erweiterung können wir eine dayofweek Variable wie folgt festgelegt:

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

Beachten Sie, dass 0 kann Sonntag oder Montag sein, abhängig von Ihrem Gebietsschema.

Verwandte Themen