Ten post pokaże po krótce to, że znane wszystkim pliki bat wcale nie muszą być prostymi skryptami uruchamiającymi inne programy. Na poczatek może zaczniemy od jakiegoś przykładu:
@ECHO OFF
%DATE%
PAUSE>NUL
Jest to prosty skrypt wyświetlający aktualną datę. Problem polega na tym, że rodzaj zapisu daty jest zależny od ustawień na danej stacji. Spróbujcie uruchomić to samo na innych komputerach i zobaczcie czy za każdym razem otrzymacie identyczny format daty ;)
To nasuwa nam problem, w przypadku gdy chcemy aby data była zawsze prezentowana w taki sam sposób, niezależnie od ustawień w systemie. Co w przypadku gdy chcemy wypisać na ekran datę w postaci 06.08.2009? Albo 2009/08/06? Niestety nie jesteśmy w stanie zmieniać ustawień każdego komputera na którym miałby być uruchamiany ten - na pozór prosty - skrypt. Z pomocą przychodzi nam polecenie
SET var=%DATE:~a,b%
Zastosowanie powyższego polecenia pozwala nam wyciągać poszczególne substringi, a następnie ustawiać w takiej kolejności w jakiej sobie tylko zażyczymy.
Mały przykład:
@ECHO OFF
ECHO Data na moim komputerze ma postac: %DATE%
IF /i "%DATE:~-3,-2%"=="-" SET CURR=%DATE:~-2%.%DATE:~-5,-3%.%DATE:~-10,-6%
IF /i "%DATE:~-5,-4%"=="-" SET CURR=%DATE:~-10,-8%.%DATE:~-7,-5%.%DATA:~-4%
IF /i "%DATE:~-5,-4%"=="/" SET CURR=%DATE:~-10,-8%.%DATE:~-7,-5%.%DATA:~-4%
ECHO Po przeformatowaniu, data ma postac: %CURR%
PAUSE>NUL
Uruchomcie powyższy skrypt. Prawda, że proste? :) Teraz wytłumaczę co się dzieję w powyższym kodzie.
Wykonanie instrukcji "ECHO Data na moim komputerze ma postac: %DATE%" powoduje wyświetlenie na konsoli bieżącego formatu daty jaki mamy ustawiony w systemie.
W zwiazku z tym, że data w Windowsach może mieć następujący format:
06/08/2009
06-08-2009
2009-08-06
musimy mieć trzy IF'y, które biorą pod uwagę każdą z tych możliwości. Przełącznik /i w funkcji IF służy do porównania dwóch stringów (dzięki niemu możemy korzystać z zapisu string1==string2). Następnie przechodzimy do polecenia o którym wspominałem na początku tego posta. Otóż, jest to o wiele łatwiejsze niż by się mogło wydawać. Zapis
"%DATE:~-3,-2%"=="-"
oznacza nic innego jak "dla wyniku wywołania zmiennej DATE w systemie, zapamiętaj substring od 3 znaku od końca do 2 znaku od końca, a następnie porównaj ze znakiem myślnika "-". Po dwukropku musimy pamiętać o znaku tyldy (~). Pod zmienne a i b podstawiamy numer znaku, na zasadzie 'od a do b'. W naszym przykładzie jest jeszcze znak minusa, oznaczający liczenie znaków od końca stringa. Czyli jak łatwo się zorientować, zapis "%DATE:~-3,-2%"=="-" dotyczy formatowania daty w postaci 2009-08-06 (- jest 3 znakiem od końca). Dalej tworzymy sobie zmienna CURR, w której formatujemy datę dla tego konkretnego przypadku.
SET CURR=%DATE:~-2%.%DATE:~-5,-3%.%DATE:~-10,-6%
Można opisać następująco: ustaw zmienna CURR, która będzie równa: od drugiego znaku od końca do zerowego od końca, z systemowego formatowania daty (czyli 06) -> kropka -> od piątego od końca do trzeciego znaku od końca (czyli 08) -> kropka -> od 10 od końca do 6 od końca (2009). Taki zapis przypisuje zmiennej CURR datę w postaci 06.08.2009.
Dla pozostałych IF'ów sytuacja jest analogiczna, więc nie będę ich już rozpisywał :)
Następnie wyświetlamy zawartość zmiennej CURR, która będzie zawierała datę w postaci DD.MM.RRRR, niezależnie od domyślnego formatowania daty w systemie.
Prawda, że proste? ;)
PS. Powyższy przykład dotyczy formatowania daty, jednak nie oznacza to, że tylko w tym wypadku mamy możliwość formatowania stringa. To samo możemy zrobić na przykład ze zmienna systemową TIME, czy z własnymi zmiennymi, które potrzebujemy odpowiednio przeformatować.
-------------------------------------------------------------------------------------
This post will show that known to all bat files, not at all be easy scripts which starts other applications. Maybe at the beginning we begin with an example:
@ECHO OFF
%DATE%
PAUSE>NUL
This is simple script that displays current date. The problem is that the type of date notation is determined at a given PC settings. Try to run this script on different PCs and look whether date format will be the same each time ;) This raises to mind that in case when we want to show date always in the same way, regardless of system settings. What in case when we want to print date in form 06.08.2009 on screen? Or 2009/08/06? Unfortunately, we are not able to change the setting of each computer where this (seemingly simple) script would be run. With the assistance comes to us following command
SET var=%DATE:~a,b%
Applaying command above, allows us to draw specific substrings and then set in order which we would like. Small example:
@ECHO OFF
ECHO Date on my computer is: %DATE%
IF /i "%DATE:~-3,-2%"=="-" SET CURR=%DATE:~-2%.%DATE:~-5,-3%.%DATE:~-10,-6%
IF /i "%DATE:~-5,-4%"=="-" SET CURR=%DATE:~-10,-8%.%DATE:~-7,-5%.%DATA:~-4%
IF /i "%DATE:~-5,-4%"=="/" SET CURR=%DATE:~-10,-8%.%DATE:~-7,-5%.%DATA:~-4%
ECHO After formatting, date on my computer is: %CURR%
PAUSE>NUL
Please run above script. Simple, true? :) Now I explain what is really going on this code.
Instruction „ECHO Date on my computer is: %DATE%” causes print on console currently date format which we have set in the system. Therefore that data in Windows can have following format:
06/08/2009
06-08-2009
2009-08-06
we must have three IF conditions which take each of these possibilities. Switch /i in function IF occurs to compare two strings (with this tool we can use notation string1==string2). Next we go to command about I mentioned at the beginning of this post. This is more easier than that would seem. Notation
"%DATE:~-3,-2%"=="-"
means nothing else like „for result of call DATE system variable, remember substring from the third sign of the end to the second sign of the end, next compare it with dash sign „-”. After colon we must remember about tilde sign (~). Under variables a and b we put sign number on a rule „from a to b”. In our example exist also a minus sign, which means counting characters from the end of string. That is how easy to find, notice "%DATE:~-3,-2%"=="-" is related with 2009-08-06 date format (minus is the third sign of the end). Further we create variable CURR , where we formatting date for that case.
SET CURR=%DATE:~-2%.%DATE:~-5,-3%.%DATE:~-10,-6%
Can be described as follows: set the variable CURR which will be equal to: from the second sign of the end to the zero sign of the end from system formated date (06) → dot → from the fifth sign of the end to the third sign of the end (08) → dot → from tenth sign of the end to sixth sign of the end (2009). This notation assigned to variable CURR date as 06.08.2009. For rest of IF conditions situation is analogous, so I'll not describe them :)
Next we print content of variable CURR, which will contained date as DD.MM.YYYY, regardless of default date format in system.
This is simple, true? :)
PS. Example above is related to date format, but this doesn't mean that we can use this possibility only in that case. The same we can do for example with system variable TIME, or with own variables which we want to format properly.
Subskrybuj:
Komentarze do posta (Atom)


Brak komentarzy:
Prześlij komentarz