2009-06-01 8 views
8

ciao ho ottenuto un file batch, qualcosa di simile:fanno solo se il file batch di giorno ...

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

ora so la prima linea non funzionerà.

Quello che realmente vuoi che accada:

controlla automatticly che giorno è. Se è dal lunedì al venerdì, deve andare su "sì", altrimenti (sabato/domenica) su "no".

Come fare questo?

risposta

5

Mi sono imbattuto in questo online. Testato e funziona. Restituisce il giorno come un numero intero, su cui puoi ancora lavorare.

@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

Si noti che questo non funzionerà su locali in cui il separatore della data non è/ – laalto

+2

Questa non sarà una soluzione affidabile, come quando uso 'data/t' qui, tutto quello che ottengo è quello breve (AAAA-MM-GG), quindi nessun nome del giorno. Va con i locali. Se questo è per l'esportazione (chiunque non sia voi), non scommetterei su questo funzionando come previsto. – 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

lo fa da solo o ha l'utente di digitarlo? –

+2

Chi sta impostando% day%? – criddell

+0

Ho pensato che% day% fosse già impostato e che non comprendesse appieno la ramificazione. – Randolpho

0

Non ho il fu batch per rispondere alla domanda, come ha chiesto, ma, supponendo che questo è un file batch di Windows, si consideri l'esecuzione lo script utilizzando l'utilità di pianificazione, che vi permetterà di impostare il tipo di programma stai chiedendo.

+0

ti capisco, ma poi devo anche dare un tempo specifico. il file BATCH verrà eseguito all'avvio. –

+0

È possibile impostare un trigger in Utilità di pianificazione per eseguire "All'avvio" o "All'accesso". Non deve avere un tempo specifico. –

9

Ecco un esempio di file bat che farà questo genere di cose, sono sicuro che puoi pensare ad altri modi per utilizzare questo codice di esempio. Ad esempio ogni volta che hai bisogno di un elenco "in". Il bit difficile è la data%: ~ 0,3% dice che espandere la variabile d'ambiente% date% e partire dalla posizione 0 l'inizio della stringa restituisce i successivi 3 caratteri. Puoi saperne di più su "set /?" comando.

esempio: 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 ... questo non sembra funzionare per me. –

+0

+1 eccellente. Funziona su Window7 per me. @YourComputerHelpZ puoi% date% dalla riga di comando e anche data/t. Puoi dirci cosa ottieni. –

5

Come Jay ha detto, usando date /t funzionerà solo su locali in cui questo comando uscite il giorno della settimana insieme alla data, e non lavorerà sugli altri locales (es. russo). Se non ti dispiace mescolare i tuoi file batch con alcuni VBScript, ecco una soluzione che dovrebbe funzionare su tutte le versioni locali.

Il trucco è questo script VBScript piccola che genera il giorno della settimana come numero (1 = Domenica, 2 = Lunedi, ... 7 = Sabato):

WScript.Echo DatePart("w", Date) 

È possibile eseguire questo script dal file batch, di leggere la sua uscita e applicare la logica:

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

Ecco un file batch che estrae giorno della settimana, giorno, mese e anno in un modo quasi locale-neutrale.

L'unica cosa specifica locale è l'ortografia del giorno della settimana, il resto è neutrale.

Quindi in inglese, restituirà Thu per giovedì, ma in olandese sarà do (per 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 

Buon divertimento!

--jeroen

0

Da this answer, abbiamo

wmic path win32_localtime get dayofweek 

espansione su questo sulla base dei suggerimenti in this thread, siamo in grado di impostare una variabile dayofweek come segue:

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

Si noti che 0 può essere domenica o lunedì a seconda delle impostazioni locali.

Problemi correlati