Kezdőlap > General > Older date via batch-file

Older date via batch-file


Egy adott problema kapcsan (hamarosan megirom…Wink) felmerult, hogy parancs-sori eszkozokkel szuksegem lenne a mai napnal regebbi datumokra. Gyakorlatilag pl. a Date()-x nap erdekelt, ahol x valtoztathato, s "datum" formatumban ertelmezheto. A "datum" azert van idezojelek kozott, hiszen a kulonbozo formatumok miatt nem egyertelmu a kinezet (xxxx-xx-xx, xx/xx/xx, stb.) Viszont sikerult talalni egy olyan mintat, amit kicsit (vagy kevesbe kicsit) atgyurtam, igy megszuletett a vegso formatum.
 
Meg mielott bemasolnam a script-et, nehany informacio. Eredetileg angol nyelvu operacios rendszerre keszult, ezert a magyaron nem volt teljesen mukodokepes. Mindez amiatt, mert megprobalja lekerdezni a valtozok neveit is (ev, ho, nap), magyar Windows eseten viszont az ev neve ekezetes, igy a script mar nem tudja tovabb kezelni, nem lehet valtozokent hasznalni. Ezert a magyar resznel explicit adtam meg a valtozo-neveket, ertelemszeruen.
 
Az eredeti mintaban nem volt semmi komment, igy az olvashatosag, erthetoseg kedveert probaltam minel tobb helyen hozzaszolasokat fuzni (termeszetesen angolul), igy kezdoknek is alkalmas lehet megerteni…
 
Mint mar emlitettem, a vegso format barmikor ossze lehet allitani barmilyen formatumban, hiszen "darabokbol" all ossze a datum – de ez az utolso sorban latszik is…
 
@echo off
 
Set English=0
:: English
if exist "%AllUsersProfile%Desktop" Set English=1
 
:: The parameter will be the countdown, if empty, set to 1
set Before=%1
if "%Before%"=="" set Before=1
 
set $tok=1-3
 
:: Search for delimiter in %DATE%, puts the first number in $d1
for /f "tokens=1 delims=.:/-, " %%u in (‘date /t’) do set $d1=%%u
 
:: If the character of $d1 from the 0. position, 1 long is Greater then "9" set token to 2-4, so we should have the date
if "%$d1:~0,1%" GTR "9" set $tok=2-4
 
for /f "tokens=%$tok% delims=.:/-, " %%u in (‘date /t’) do (
 
if %English%==1 (
:: Skips the first row, goes to the second row
   for /f "skip=1 tokens=2-4 delims=/-,()." %%x in (‘echo.^|date’) do (
     set %%x=%%u
     set %%y=%%v
     set %%z=%%w
 )
) ELSE (
 
:: If not English, you must know the correct order of data display (here is YYYY.MM.DD -> u.v.w),
    Set yyyy=%%u
    Set mm=%%v
    Set dd=%%w
)
)
 
:: If English, then we have only short year date
if "%yyyy%"=="" Set yyyy=%yy%
 
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% – 100
 
:: On English set the current date
set CurDate=%mm%/%dd%/%yyyy%
 
::On Hungarian set the current date
set CurDate=%yyyy%.%hh%.%nn%
 
:: Here we are decreasing the days
set /A dd=1%dd% – 100 – %Before%
set /A mm=1%mm% – 100
 
:CHKDAY
 
:: If the day is ok, then we are done
if /I %dd% GTR 0 goto DONE
 
:: Else decrease the month with one
set /A mm=%mm% – 1
 
:: If the month is ok, set correct date
if /I %mm% GTR 0 goto ADJUSTDAY
 
:: Else set month to december, decrease year
set /A mm=12
set /A yyyy=%yyyy% – 1
 
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
:: Else, if month is 12 then anyway continues with the next line, Set31
 
:SET31
set /A dd=31 + %dd%
goto CHKDAY
 
:SET30
set /A dd=30 + %dd%
goto CHKDAY
 
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
 
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
 
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
 
:SET28
set /A dd=28 + %dd%
goto CHKDAY
 
:SET29
set /A dd=29 + %dd%
goto CHKDAY
 
:DONE
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
 
:: Clearing the temporary variables
Set $tok=
Set $d1=
Set Before=
Set English=
 
Echo %yyyy%.%mm%.%dd%
Kategóriák:General
  1. Még nincs hozzászólás.
  1. No trackbacks yet.

Hozzászólás