├── lib.css ├── System Functions ├── Sleep.html ├── IsRunning.html ├── GetOS.html ├── Uptime.html └── Timer.html ├── Network Functions ├── GetDG.html ├── GetIP.html ├── GetSM.html ├── GetNA.html └── GetMAC.html ├── Date and Time Functions ├── MonthName.html ├── DayName.html ├── MonthNumber.html ├── DayNumber.html ├── DaysToDate.html ├── DateToDOW.html ├── MJDToDate.html ├── DateToMJD.html ├── OrdinalToDate.html ├── DateToOrdinal.html ├── DateToDays.html ├── WeekToDate.html ├── DateToWeek.html ├── MinsToDate.html ├── GetTime.html ├── DateToMins.html ├── 0 Date and Time Functions.html ├── DateToSecs.html ├── GetDate.html └── SecsToDate.html ├── File and Directory Functions ├── IsInPath.html ├── IsDirEmpty.html ├── GetDirStats.html └── FileSizeComp.html ├── 01_Introduction ├── 1_Batch Function Overview.html ├── 2_Using the Functions.html └── 3_Batch Functions Explained.html ├── README.md └── Batch Howto's └── Reading Files.html /lib.css: -------------------------------------------------------------------------------- 1 | body, p, td, div { 2 | color: black; 3 | font: normal 11px Verdana, Helvetica, sans-serif, Arial; 4 | } 5 | 6 | p { 7 | color: #000000; 8 | margin: 5px; 9 | 10 | 11 | 12 | } 13 | 14 | a { 15 | text-decoration: none; 16 | color: #000000; 17 | 18 | } 19 | 20 | a:link { 21 | text-decoration: none; 22 | color: #000099; 23 | } 24 | 25 | a:visited { 26 | text-decoration: none; 27 | color: #000099; 28 | 29 | } 30 | 31 | a:active { 32 | text-decoration: none; 33 | color: #000099; 34 | 35 | } 36 | 37 | a:hover { 38 | text-decoration: underline; 39 | color: #000099; 40 | 41 | } 42 | 43 | .title { 44 | font-weight: bold; 45 | font-size: 14px; 46 | 47 | 48 | } 49 | .heading { 50 | font-size: 11px; 51 | font-weight: bold; 52 | margin-top: 10px; 53 | 54 | 55 | 56 | 57 | 58 | } 59 | .function { 60 | font-family: "Courier New", Courier, mono; 61 | font-size: 12px; 62 | background-color: EAF0FF; 63 | 64 | 65 | } 66 | 67 | .footer { 68 | font-size: 9px; 69 | color: #339933; 70 | } 71 | 72 | .footer a:link { 73 | color: #339933; 74 | text-decoration: none; 75 | 76 | 77 | } 78 | 79 | .footer a:visited { 80 | color: #339933; 81 | text-decoration: none; 82 | 83 | 84 | } 85 | 86 | .footer a:hover { 87 | color: #339933; 88 | text-decoration: none; 89 | 90 | 91 | } 92 | 93 | .footer a:active { 94 | color: #339933; 95 | text-decoration: none; 96 | 97 | 98 | } 99 | -------------------------------------------------------------------------------- /System Functions/Sleep.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sleep 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 56 | 57 |

Sleep

17 | 19 | 20 | 21 | 22 | 36 | 37 | 38 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :Sleep %secs%
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-16. Version 1.0
26 | ::
27 | :: Func: Suspends batch execution for a number of seconds.
28 | ::       For NT4/2000/XP/2003.
29 | ::
30 | :: Args: %1 Number of seconds to wait for (by val)
31 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
32 | setlocal ENABLEEXTENSIONS & set /a n=%1+1
33 | ping -n %n% 127.0.0.1 >nul
34 | endlocal & goto :EOF
35 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39 |

Parameters

40 |

%1 Number of seconds to wait for (by val)

41 |

Return Values

42 |

See parameters above.

43 |

Example

44 |
45 |

@echo off
46 | call :Sleep 5
47 | goto :EOF

48 |
49 |

Remarks

50 |

This function is based on the idea 51 | by Clay Calvert.

52 |

See Also

53 |

Timer

54 |

 

55 |
58 | 59 | 60 | -------------------------------------------------------------------------------- /Network Functions/GetDG.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | GetDG 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 62 | 63 |

GetDG

17 | 19 | 20 | 21 | 22 | 41 | 42 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetDG dg
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-22. Version 1.0
26 | ::
27 | :: Func: Obtains the default gateway. For NT4/2000/XP/2003.
28 | ::       If functions fails, 0.0.0.0 29 | is returned.
30 | ::
31 | :: Args: %1 var to receive default gateway (by ref)
32 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
33 | setlocal ENABLEEXTENSIONS & set "g=0.0.0.0" & set 34 | "j="
35 | for /f "tokens=3" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') 36 | do (
37 |   if not defined j for %%b in (%%a) do set "g=%%b" 38 | & set "j=1")
39 | endlocal & set "%1=%g%" & goto :EOF
40 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43 |

Parameters

44 |

%1 var to receive default gateway (by ref)

45 |

Return Values

46 |

See parameters above.

47 |

Example

48 |
49 |

@echo off & setlocal ENABLEEXTENSIONS
50 | call :GetDG dg
51 | echo/Default Gateway is: %dg%
52 | goto :EOF

53 |
54 |

Remarks

55 |

If the machine has more than one interface, the default gateway returned 56 | will be that of the interface whose IP address is attached to the default 57 | route.

58 |

See Also

59 |

GetIP

60 |

 

61 |
64 | 65 | 66 | -------------------------------------------------------------------------------- /Network Functions/GetIP.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | GetIP 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 61 | 62 |

GetIP

17 | 19 | 20 | 21 | 22 | 41 | 42 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetIP ip
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-22. Version 1.0
26 | ::
27 | :: Func: Obtains the IP address of primary adapter. For NT4/2000/XP/2003.
28 | ::       If functions fails, 0.0.0.0 29 | is returned.
30 | ::
31 | :: Args: %1 var to receive IP address (by ref)
32 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
33 | setlocal ENABLEEXTENSIONS & set "i=0.0.0.0" & set 34 | "j="
35 | for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') 36 | do (
37 |   if not defined j for %%b in (%%a) do set "i=%%b" 38 | & set "j=1")
39 | endlocal & set "%1=%i%" & goto :EOF
40 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43 |

Parameters

44 |

%1 var to receive IP Address (by ref)

45 |

Return Values

46 |

See parameters above.

47 |

Example

48 |
49 |

@echo off & setlocal ENABLEEXTENSIONS
50 | call :GetIP ip
51 | echo/IP Address is: %ip%
52 | goto :EOF

53 |
54 |

Remarks

55 |

If the machine has more than one interface, the IP address returned will 56 | be that of the interface whose IP address is attached to the default route.

57 |

See Also

58 |

GetMAC

59 |

 

60 |
63 | 64 | 65 | -------------------------------------------------------------------------------- /Date and Time Functions/MonthName.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | MonthName 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 65 | 66 |

MonthName

17 | 19 | 20 | 21 | 22 | 41 | 42 | 43 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :MonthName %mm% month
24 | ::
25 | :: By:   Ritchie Lawrence, 2002-10-04. Version 1.0
26 | ::
27 | :: Func: Returns the name of month from the number of the month.
28 | ::       For NT4/2000/XP/2003.
29 | ::
30 | :: Args: %1 month number convert to name of month, 1 or 01 to 12 (by 31 | val)
32 | ::       %2 var to receive name of month 33 | (by ref)
34 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35 | setlocal ENABLEEXTENSIONS&set /a m=100%1%%100
36 | for /f "tokens=%m%" %%a in ('echo/January February March 37 | April May June^
38 |   July August September October November December'
39 | ) do endlocal&set %2=%%a&goto :EOF
40 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
44 |

Parameters

45 |

%1 month number convert to name of month, 1 or 01 to 12 (by 46 | val)
47 | %2 var to receive name of month (by ref)

48 |

Return Values

49 |

See parameters above.

50 |

Example

51 |
52 |

@echo off & setlocal ENABLEEXTENSIONS
53 | call :GetDate yy mm dd
54 | call :MonthName %mm% month
55 | echo/The current month is: %month%
56 | goto :EOF

57 |
58 |

Remarks

59 |

The returned month name is the full name of the month with the first 60 | letter capitalised.

61 |

See Also

62 |

MonthNumber

63 |

 

64 |
67 |

 

68 | 69 | 70 | -------------------------------------------------------------------------------- /File and Directory Functions/IsInPath.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | IsInPath 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 69 | 70 |

IsInPath

17 | 20 | 21 | 22 | 23 | 39 | 40 |
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
24 | :IsInPath %filelist%
25 | ::
26 | :: By:   Ritchie Lawrence, 2003-11-29. Version 1.0
27 | ::
28 | :: Func: Checks if the file(s) in the specified list exist within the
29 | ::       current directory or path. If all files 30 | found, errorlevel set to
31 | ::       zero otherwise non-zero. NT4/2000/XP/2003.
32 | ::
33 | :: Args: %1 List of files separated by whitespace (by val)
34 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35 | setlocal ENABLEEXTENSIONS & set "path=.;%path%" & set "i="
36 | for %%a in (%*) do for %%b in (%%a) do if not exist "%%~$PATH:b" set "i=1"
37 | ver>nul & if defined i md;2>nul & endlocal & goto :EOF
38 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
41 |

Parameters

42 |

%1 List of files separated by whitespace (by val)

43 |

Return Values

44 |

See parameters above.

45 |

Example

46 |
47 |

@echo off & setlocal ENABLEEXTENSIONS
48 | set "tools="
49 | set "tools=%tools% ping.exe"
50 | set "tools=%tools% find.exe"
51 | set "tools=%tools% "exe with space in name.exe""
52 | set "tools=%tools% drivers\etc\hosts"
53 |
54 | call :IsInPath %tools% || (
55 |   >&2echo/Error: Unable to locate the utilities required by 56 | this script.
57 |   md;2>nul & goto :EOF
58 | )
59 |
60 | rem Rest of code here...
61 | goto :EOF

62 |
63 |

Remarks

64 |

A space, a tab and a semi-colon are considered to be whitespace.

65 |

See Also

66 |

None.

67 |

 

68 |
71 | 72 | 73 | -------------------------------------------------------------------------------- /File and Directory Functions/IsDirEmpty.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | IsDirEmpty 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 64 | 65 |

IsDirEmpty

17 | 19 | 20 | 21 | 22 | 42 | 43 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :IsDirEmpty %dir%
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-22. Version 1.0
26 | ::
27 | :: Func: If specified directory is empty of files and directories, 28 | then
29 | ::       errorlevel is set to zero. 30 | If directory is not empty or does not
31 | ::       exist, errorlevel is non-zero 32 | one. For NT4/2000/XP/2003.
33 | ::
34 | :: Args: %1 Name of directory to be tested (by val)
35 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
36 | setlocal ENABLEEXTENSIONS & dir/ad %1 >nul 2>nul || (endlocal 37 | & goto :EOF)
38 | set i=0 & for /f %%a in ('dir %1/a/b 2^>nul') do set/a "i+=1"
39 | md;2>nul & (if %i%==0 ver>nul) & endlocal & goto 40 | :EOF
41 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
44 |

Parameters

45 |

%1 Name of directory to be tested (by val)

46 |

Return Values

47 |

If specified directory does not contain any files (including SYSTEM and/or 48 | HIDDEN files) and the specified directory does not contain any subdirectories, 49 | the errorlevel is set to zero. If the specified directory in not empty 50 | or does not exist, the errorlevel is set to non-zero

51 |

Example

52 |
53 |

@echo off & setlocal ENABLEEXTENSIONS
54 | set var="c:\test dir"
55 | call :IsDirEmpty %var% && echo/%var% is empty
56 | goto :EOF

57 |
58 |

Remarks

59 |

None.

60 |

See Also

61 |

GetDirStats

62 |

 

63 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /Date and Time Functions/DayName.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DayName 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 66 | 67 |

DayName

17 | 19 | 20 | 21 | 22 | 40 | 41 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DayName %d% day
24 | ::
25 | :: By:   Ritchie Lawrence, 2002-10-04. Version 1.0
26 | ::
27 | :: Func: Returns the day of week from the day number, 1=Monday, 7=Sunday.
28 | ::       For NT4/2000/XP/2003.
29 | ::
30 | :: Args: %1 day number to convert to name of day of week, 1 to 7 (by 31 | val)
32 | ::       %2 var to receive name of day 33 | of week (by ref)
34 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35 | setlocal ENABLEEXTENSIONS
36 | for /f "tokens=%1" %%a in ('echo/Monday Tuesday Wednesday 37 | Thursday Friday^
  Saturday Sunday') do endlocal&set 38 | %2=%%a&goto :EOF
39 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
42 |

Parameters

43 |

%1 day number to convert to name of day of week, 1 to 7 (by 44 | val)
45 | %2 var to receive name of day of week (by ref)

46 |

Return Values

47 |

The return value is the full name of the day of week, with the first 48 | letter capitalised. It can be shortened to the first three letters using 49 | substring expansion, for example:-

50 |
51 |

set day=%day:~0,3%

52 |
53 |

Example

54 |
55 |

@echo off & setlocal ENABLEEXTENSIONS
56 | call :DayName 3 day
57 | echo/Third day of the week is: %day%
58 | goto :EOF

59 |
60 |

Remarks

61 |

Day 1 is Monday, day 7 is Sunday.

62 |

See Also

63 |

DayNumber, DateToWeek, 64 | WeekToDate

65 |

 

68 |

 

69 | 70 | 71 | -------------------------------------------------------------------------------- /System Functions/IsRunning.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | IsRunning 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 64 | 65 |

IsRunning

17 | 19 | 20 | 21 | 22 | 37 | 38 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :IsRunning %svc%
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-10-08. Version 1.0
26 | ::
27 | :: Func: Sets errorlevel to zero if the specified service is running,
28 | ::       otherwise errorlevel set to one. For NT4/2000/XP/2003.
29 | ::
30 | :: Args: %1 Display name of the service to check (by val)
31 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
32 | setlocal ENABLEEXTENSIONS & set "svc=%1"
33 | set svc=%svc:"=%
34 | net start | findstr/i /b /e /c:"   %svc%" >nul
35 | endlocal & goto :EOF
36 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39 |

Parameters

40 |

%1 Display name of the service to check 41 | (by val)

42 |

Return Values

43 |

See parameters above.

44 |

Example

45 |
46 |

@echo off & setlocal ENABLEEXTENSIONS
47 | call :IsRunning "Task Scheduler" && echo/Yes || echo/No
48 | goto :EOF

49 |
50 |

Remarks

51 |

Note that Findstr is used with the /b and /e switches to avoid false 52 | positives. This could happen when checking if "Server" 53 | service is running and there are other services containing the word "server". 54 | Also note there are three spaces between /c:" and %svc%".

55 |

Bear in mind that a paused service is still regarded as running by the 56 | net command. If it's important to know 57 | whether the service is running and not paused, then a third party utility 58 | will be required for Windows NT4 and 2000. Windows XP and 2003 can use 59 | the built-in SC.exe tool.

60 |

See Also

61 |

None

62 |

 

63 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /Date and Time Functions/MonthNumber.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | MonthNumber 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 68 | 69 |

MonthNumber

17 | 19 | 20 | 21 | 22 | 46 | 47 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :MonthNumber %month% mm
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-05-14. Version 1.0
26 | ::
27 | :: Func: Returns the number of month from name of month. Case insensitive
28 | ::       and only the first three letters 29 | are matched, result is 01 to
30 | ::       12 or NULL if no match. For 31 | NT4/2000/XP/2003.
32 | ::
33 | :: Args: %1 month name to convert to month number, Jan to Dec (by 34 | val)
35 | ::       %2 var to receive number of 36 | month (by ref)
37 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
38 | setlocal ENABLEEXTENSIONS & set i=0
39 | for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
40 |   set /a i+=1 & echo/%1|findstr /i /b "%%a" 41 | >nul && call set mm=%%i%%)
42 | (if 1%mm% LSS 110 set mm=0%mm%) & endlocal&set %2=%mm%&goto 43 | :EOF
44 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 45 |
48 |

Parameters

49 |

%1 month name to convert to month number, Jan to Dec (by val)
50 | %2 var to receive number of month (by ref)

51 |

Return Values

52 |

Only the first three letters of the month are required. For example, 53 | if called with Apr or April, 4 is returned. This function is case insensitive. 54 | If the month name is invalid, NULL is required.

55 |

Example

56 |
57 |

@echo off & setlocal ENABLEEXTENSIONS
58 | call :MonthNumber April mm
59 | echo/April is month: %mm%
60 | goto :EOF

61 |
62 |

Remarks

63 |

None.

64 |

See Also

65 |

MonthName

66 |

 

67 |
70 |

 

71 | 72 | 73 | -------------------------------------------------------------------------------- /01_Introduction/1_Batch Function Overview.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Batch Function Overview 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 77 | 78 |

Batch Function Overview

12 |

What are they?

13 |

A batch function is a block of batch code that can be called by a batch 14 | file, optionally with a number of parameters. The function performs its 15 | task without unintentionally modifying variables outside of its own scope, 16 | and then returns control to the statement following the one that called 17 | the function.

18 |

Why use them?

19 |

The primary benefit of functions is reusable code. Once a function has 20 | been written, it can be used over and over again. This reduces the time 21 | spent developing and debugging scripts.

22 |

Scripts that use functions naturally have a modular structure and use 23 | fewer 'global' variables, therefore they are much easier to understand, 24 | debug and maintain.

25 |

What do they look like?

26 |

All the functions in the library are based on the following 27 | template:-

28 |
29 |

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
30 | :<Function Name> <Parameter list>
31 | ::
32 | :: By:   <Author/date/version information>
33 | ::
34 | :: Func: <Function description>
35 | ::
36 | :: Args: <Argument description>
37 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
38 | setlocal ENABLEEXTENSIONS
39 | <Body of function>
40 | endlocal&<Set return values>&goto :EOF
41 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

42 |
43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 75 | 76 |
<Function name>Name of the function, eg GetDate
<Parameter list>List of arguments. This list only serves 51 | as a reminder of how to call the function (everything after the function 52 | name is ignored by the command interpreter)
<Author/date/version information>Self explanatory
<Function description>Brief explanation of what the function does and which 61 | platforms it has been designed for (NT4, W2K or XP)
<Argument description>Detailed description of the functions arguments
<Body of function>This is where the function performs its main purpose
<Set return values>This is where the function 'returns' any local values 74 | back to the calling routine
79 | 80 | 81 | -------------------------------------------------------------------------------- /Date and Time Functions/DayNumber.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DayNumber 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 66 | 67 |

DayNumber

17 | 19 | 20 | 21 | 22 | 42 | 43 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DayNumber %Weekday% dow
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-05-14. Version 1.0
26 | ::
27 | :: Func: Returns the day number from the name of weekday. Case insensitive
28 | ::       and only the first three letters 29 | are matched, result is 1 to 7 or
30 | ::       NULL if no match. For NT4/2000/XP/2003.
31 | ::
32 | :: Args: %1 Weekday name to convert to day of week number (by val)
33 | ::       %2 var to receive DOW number 34 | (by ref)
35 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
36 | setlocal ENABLEEXTENSIONS & set i=0
37 | for %%a in (Mon Tue Wed Thu Fri Sat Sun) do (
38 |   set /a i+=1 & echo/%1|findstr /i /b "%%a" 39 | >nul && call set dow=%%i%%)
40 | endlocal&set %2=%dow%&goto :EOF
41 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
44 |

Parameters

45 |

%1 Weekday name to convert to day of week number (by val)
46 | %2 var to receive DOW number (by ref)

47 |

Return Values

48 |

Function is case insensitive and only the first three letters of the 49 | day of week name are required. For example, if called with Wed, wed or 50 | Wednesday, the result is 3. If the day of week name is invalid, the return 51 | value is NULL.

52 |

Example

53 |
54 |

@echo off & setlocal ENABLEEXTENSIONS
55 | call :DayNumber Tueday dow
56 | echo/Tuesday is day: %dow%
57 | goto :EOF

58 |
59 |

Remarks

60 |

Monday is day 1 and Sunday day 7.

61 |

See Also

62 |

DayName, DateToWeek, 63 | WeekToDate

64 |

 

65 |
68 |

 

69 | 70 | 71 | -------------------------------------------------------------------------------- /System Functions/GetOS.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | GetOS 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 80 | 81 |

GetOS

17 | 19 | 20 | 21 | 22 | 38 | 39 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetOS os
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-18. Version 1.0
26 | ::
27 | :: Func: Returns the O/S version; NT40, 2000, 2002 or 2003.
28 | ::       For NT4/2000/XP/2003.
29 | ::
30 | :: Args: %1 var to receive O/S version (by ref)
31 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
32 | setlocal ENABLEEXTENSIONS & set "cmd=net config work^|findstr/b 33 | /c:"Soft""
34 | for /f "tokens=1-2 delims=." %%a in ('%cmd%') do (
35 |   for %%z in (%%a%%b) do set o=%%z)
36 | endlocal & set "%1=%o:40=NT40%" & (goto :EOF)
37 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
40 |

Parameters

41 |

%1 var to receive O/S version (by ref)

42 |

Return Values

43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 |
ValueOperating System
NT40Microsoft Windows NT 4.0
2000Microsoft Windows 2000
2002Microsoft Windows XP
2003Microsoft Windows 2003
65 |

Example

66 |
67 |

@echo off & setlocal ENABLEEXTENSIONS
68 | call :GetOS ver
69 | echo/Operating System is: %ver%
70 | goto :EOF

71 |
72 |

Remarks

73 |

This function is independant of the command interpreter version, i.e. 74 | If the script is executed by a Windows 2000 version of cmd.exe on a Windows 75 | NT 4.0 machine, NT40 will be returned.

76 |

See Also

77 |

 

78 |

 

79 |
82 | 83 | 84 | -------------------------------------------------------------------------------- /Date and Time Functions/DaysToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DaysToDate 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 70 | 71 |

DaysToDate

17 | 19 | 20 | 21 | 22 | 46 | 47 | 48 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DaysToDate %days% yy mm dd
24 | ::
25 | :: By:   Ritchie Lawrence, 2002-06-15. Version 1.1
26 | ::
27 | :: Func: Returns a calendar date from the number of elapsed days since
28 | ::       1st January 1970. For NT4/2000/XP/2003.
29 | ::
30 | :: Args: %1 number of days used to create calendar date (by val)
31 | ::       %2 var to receive year component, 32 | 4 digits (by ref)
33 | ::       %3 var to receive month component, 34 | 2 digits, 01-12 (by ref)
35 | ::       %4 var to receive day of month 36 | component, 2 digits, 01-31 (by ref)
37 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
38 | setlocal ENABLEEXTENSIONS
39 | set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
40 | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
41 | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
42 | (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
43 | endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto 44 | :EOF
45 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
49 |

Parameters

50 |

%1 MJD used to create calendar date (by val)
51 | %2 var to receive year component, 4 digits (by ref)
52 | %3 var to receive month component, 2 digits, 01-12 (by ref)
53 | %4 var to receive day of month component, 2 digits, 01-31 (by 54 | ref)

55 |

Return Values

56 |

See parameters above.

57 |

Example

58 |
59 |

@echo off & setlocal ENABLEEXTENSIONS
60 | call :DaysToDate 12345 y m d
61 | echo/Adding 12345 days to 1970-01-01 gives the date %y%-%m%-%d%
62 | goto :EOF

63 |
64 |

Remarks

65 |

Use in conjunction with DateToDays to perform 66 | date arithmetic with a resolution of one day.

67 |

See Also

68 |

DateToDays

69 |
72 |

 

73 | 74 | 75 | -------------------------------------------------------------------------------- /Network Functions/GetSM.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | GetSM 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 73 | 74 |

GetSM

17 | 19 | 20 | 21 | 22 | 50 | 51 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetSM sm
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-22. Version 1.0
26 | ::
27 | :: Func: Obtains the subnet mask of primary adapter. For NT4/2000/XP/2003.
28 | ::       If functions fails, 0.0.0.0 29 | is returned.
30 | ::
31 | :: Args: %1 var to receive subnet mask (by ref)
32 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
33 | setlocal ENABLEEXTENSIONS & set "i=0.0.0.0" & set 34 | "m=0.0.0.0" & set "j="
35 | for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') 36 | do (
37 |   if not defined j (for %%b in (%%a) do set "i=%%b" 38 | & set j=1)) & set "k="
39 | for /f "skip=1 tokens=2-4" %%a in ('route print^|findstr/b 40 | /c:" "') do (
41 |   for %%e in (%%a) do set "x=%%e" & for %%f 42 | in (%%b) do set "y=%%f"
43 |   for %%g in (%%c) do set "z=%%g"
44 |   for /f "tokens=1-3" %%a in ('echo/%%x%% %%y%% 45 | %%z%%') do (
46 |     if not defined k if "%%c"=="%i%" 47 | if "%%b"=="%i%" set k=1 & set m=%%a))
48 | endlocal & set "%1=%m%" & goto :EOF
49 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
52 |

Parameters

53 |

%1 var to receive subnet mask (by ref)

54 |

Return Values

55 |

See parameters above.

56 |

Example

57 |
58 |

@echo off & setlocal ENABLEEXTENSIONS
59 | call :GetSM sm
60 | echo/Subnet Mask is: %sm%
61 | goto :EOF

62 |
63 |

Remarks

64 |

If the machine has more than one interface, the subnet mask returned 65 | will be that of the interface whose IP address is attached to the default 66 | route.

67 |

Note the third, fourth and fifth FOR loops strip whitespace for compatability 68 | with NT4.0 without using a TAB character in the script.

69 |

See Also

70 |

 

71 |

 

72 |
75 | 76 | 77 | -------------------------------------------------------------------------------- /Network Functions/GetNA.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | GetNA 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 73 | 74 |

GetNA

17 | 19 | 20 | 21 | 22 | 50 | 51 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetNA na
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-22. Version 1.0
26 | ::
27 | :: Func: Obtains network address of primary adapter. For NT4/2000/XP/2003.
28 | ::       If functions fails, 0.0.0.0 29 | is returned.
30 | ::
31 | :: Args: %1 var to receive network address (by ref)
32 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
33 | setlocal ENABLEEXTENSIONS & set "i=0.0.0.0" & set 34 | "n=0.0.0.0" & set "j="
35 | for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') 36 | do (
37 | if not defined j (for %%b in (%%a) do set "i=%%b" & 38 | set j=1)) & set "k="
39 | for /f "skip=1 tokens=1,3-4" %%a in ('route print^|findstr/b 40 | /c:" "') do (
41 |   for %%e in (%%a) do set "x=%%e" & for %%f 42 | in (%%b) do set "y=%%f"
43 |   for %%g in (%%c) do set "z=%%g"
44 |   for /f "tokens=1-3" %%a in ('echo/%%x%% %%y%% 45 | %%z%%') do (
46 |     if not defined k if "%%c"=="%i%" 47 | if "%%b"=="%i%" set k=1 & set n=%%a))
48 | endlocal & set "%1=%n%" & goto :EOF
49 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
52 |

Parameters

53 |

%1 var to receive network address (by ref)

54 |

Return Values

55 |

See parameters above.

56 |

Example

57 |
58 |

@echo off & setlocal ENABLEEXTENSIONS
59 | call :GetNA na
60 | echo/Network address: %na%
61 | goto :EOF

62 |
63 |

Remarks

64 |

If the machine has more than one interface, the network address returned 65 | will be that of the interface whose IP address is attached to the default 66 | route.

67 |

Note the third, fourth and fifth FOR loops strip whitespace for compatability 68 | with NT4.0 without using a TAB character in the script.

69 |

See Also

70 |

 

71 |

 

72 |
75 | 76 | 77 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToDOW.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DateToDOW 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 73 | 74 |

DateToDOW

17 | 19 | 20 | 21 | 22 | 47 | 48 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DateToDOW %yy% %mm% %dd% dow
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-04-29. Version 1.1
26 | ::
27 | :: Func: Creates a day of week number from a calendar date, where 28 | 1 = Mon
29 | ::       and 7 = Sun. For NT4/2000/XP/2003.
30 | ::
31 | :: Args: %1 year component to be converted, 2 or 4 digits (by val)
32 | ::       %2 month component to be converted, 33 | leading zero ok (by val)
34 | ::       %3 day of month to be converted, 35 | leading zero ok (by val)
36 | ::       %4 var to receive day of week 37 | number, 1 to 7 (by ref)
38 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39 | setlocal ENABLEEXTENSIONS
40 | set yy=%1&set mm=%2&set dd=%3
41 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
42 | set /a dd=100%dd%%%100,mm=100%mm%%%100
43 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,dow=153*m+2
44 | set /a dow=dow/5+dd+y*365+y/4-y/100+y/400-2472630,dow%%=7,dow+=1
45 | endlocal&set %4=%dow%&goto :EOF
46 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
49 |

Parameters

50 |

%1 year component to be converted, 2 or 4 digits (by val)
51 | %2 month component to be converted, leading zero ok (by val)
52 | %3 day of month to be converted, leading zero ok (by val)
53 | %4 var to receive day of week number, 1 to 7 (by ref)

54 |

Return Values

55 |

See parameters above.

56 |

Example

57 |
58 |

@echo off & setlocal ENABLEEXTENSIONS
59 | call :GetDate y m d
60 | call :DateToDOW %y% %m% %d% dow
61 | call :DayName %dow% day
62 | echo/Today is %day%
63 | goto :EOF

64 |
65 |

Remarks

66 |

The DateToWeek function also returns day 67 | of week (in addition to year and month).

68 |

See Also

69 |

DateToWeek, DayName, 70 | DayNumber

71 |

 

72 |
75 |

 

76 | 77 | 78 | -------------------------------------------------------------------------------- /Network Functions/GetMAC.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | GetMAC 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 78 | 79 |

GetMAC

17 | 19 | 20 | 21 | 22 | 54 | 55 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetMAC mac
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-24, updated 2011-03-09. Version 1.1
26 | ::
27 | :: Func: Obtains the MAC address of the primary adapter in the format 28 | of
29 | ::       XX-XX-XX-XX-XX-XX. If the function 30 | fails 00-00-00-00-00-00 is
31 | ::       returned. For NT4/2000/XP/2003/Win7.
32 | ::
33 | :: Args: %1 var to receive MAC address (by ref)
34 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35 | setlocal ENABLEEXTENSIONS & set "m=00-00-00-00-00-00" 36 | & set "i=" & set "j="
37 | set "n=0" & set "c=ipconfig/all" & set 38 | "f=findstr"
39 | for /f "tokens=4" %%a in ('route print^|findstr 0.0.0.0.*0.0.0.0') 40 | do (
41 |   if not defined j for %%b in (%%a) do set "i=%%b" 42 | & set "j=1")
43 | set "j="
44 | if not defined i endlocal & set "%1=%m%" & goto 45 | :EOF
46 | for /f "delims=:" %%a in ('%c%^|%f%/n IP.*Address.*%i%') 47 | do set /a n=%%a-6
48 | for /f "delims=" %%a in ('%c%^|more/e +%n%^|%f% Physical.Address') 49 | do (
50 |   if not defined j for %%b in (%%a) do set "m=%%b" 51 | & set "j=1")
52 | endlocal & set "%1=%m%" & goto :EOF
53 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
56 |

Parameters

57 |

%1 var to receive MAC address (by ref)

58 |

Return Values

59 |

See parameters above.

60 |

Example

61 |
62 |

@echo off & setlocal ENABLEEXTENSIONS
63 | call :GetMAC mac
64 | echo/MAC address is: %mac%
65 | goto :EOF

66 |
67 |

Remarks

68 |

To remove the dashes from the result use variable substitution. For example:-

69 |
70 |

:: Substitue dashes for spaces
71 | echo/MAC address is: %mac:-= %
72 | :: Permanently remove the dashes
73 | set "mac=%mac:-=%"

74 |
75 |

See Also

76 |

 

77 |

 

80 | 81 | 82 | -------------------------------------------------------------------------------- /Date and Time Functions/MJDToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | MJDToDate 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 72 | 73 |

MJDToDate

17 | 19 | 20 | 21 | 22 | 48 | 49 | 50 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :MJDToDate %MJD% yy mm dd
24 | ::
25 | :: By:   Ritchie Lawrence, 2002-06-15. Version 1.1
26 | ::
27 | :: Func: Returns a UTC date from a Modified Julian Day (MJD). Reference
28 | ::       date (day 0) is Wednesday 17th 29 | November 1858.
30 | ::       For NT4/2000/XP/2003.
31 | ::
32 | :: Args: %1 MJD used to create calendar date (by val)
33 | ::       %2 var to receive year component, 34 | 4 digits (by ref)
35 | ::       %3 var to receive month component, 36 | 2 digits, 01-12 (by ref)
37 | ::       %4 var to receive day of month, 38 | 2 digits, 01-31 (by ref)
39 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
40 | setlocal ENABLEEXTENSIONS
41 | set /a a=%1+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
42 | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
43 | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
44 | (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
45 | endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto 46 | :EOF
47 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
51 |

Parameters

52 |

%1 MJD used to create calendar date (by val)
53 | %2 var to receive year component, 4 digits (by ref)
54 | %3 var to receive month component, 2 digits, 01-12 (by ref)
55 | %4 var to receive day of month, 2 digits, 01-31 (by ref)

56 |

Return Values

57 |

See parameters above.

58 |

Example

59 |
60 |

@echo off & setlocal ENABLEEXTENSIONS
61 | call :MJDToDate 55555 y m d
62 | echo/MJD 55555 is %y%-%m%-%d% (Christmas day!)

63 |
64 |

Remarks

65 |

Use this function in conjunction with DateToMJD 66 | to perform date arithmetic with a resolution of one day. For example, 67 | use DateToMJD to convert a date to an MJD, 68 | then add/subtract days and finally convert back to a date using MJDToDate.

69 |

See Also

70 |

DateToMJD

71 |
74 |

 

75 | 76 | 77 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToMJD.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DateToMJD 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 73 | 74 |

DateToMJD

17 | 19 | 20 | 21 | 22 | 47 | 48 | 49 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DateToMJD %yy% %mm% %dd% MJD
24 | ::
25 | :: By:   Ritchie Lawrence, 2002-06-15. Version 1.0
26 | ::
27 | :: Func: Returns a Modified Julian Day (MJD) from a UTC date.
28 | ::       Reference date (day 0) is Wednesday 29 | 17th November 1858. For
30 | ::       NT4/2000/XP/2003.
31 | ::
32 | :: Args: %1 year component used to create MJD, 2 or 4 digits (by val)
33 | ::       %2 month component used to 34 | create MJD, leading zero ok (by val)
35 | ::       %3 day of month used to create 36 | MJD, leading zero ok (by val)
37 | ::       %4 var to receive MJD (by ref)
38 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39 | setlocal ENABLEEXTENSIONS
40 | set yy=%1&set mm=%2&set dd=%3
41 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
42 | set /a dd=100%dd%%%100,mm=100%mm%%%100
43 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
44 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2432046
45 | endlocal&set %4=%j%&goto :EOF
46 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
50 |

Parameters

51 |

%1 year component used to create MJD, 2 or 4 digits (by val)
52 | %2 month component used to create MJD, leading zero ok (by val)
53 | %3 day of month used to create MJD, leading zero ok (by val)
54 | %4 var to receive MJD (by ref)

55 |

Return Values

56 |

See parameters above.

57 |

Example

58 |
59 |

@echo off & setlocal ENABLEEXTENSIONS
60 | call :DateToMJD 1999 12 31 x
61 | echo/The MJD for 1999-12-31 is: %x%
62 | goto :EOF

63 |
64 |

Remarks

65 |

Use this function in conjunction with MJDToDate 66 | to perform date arithmetic with a resolution of one day. For example, 67 | use MJDToDate to convert a date to an MJD, 68 | then add/subtract x number of days and finally convert the result 69 | back to a date using DateToMJD.

70 |

See Also

71 |

MJDToDate, GetDate

72 |
75 |

 

76 | 77 | 78 | -------------------------------------------------------------------------------- /Date and Time Functions/OrdinalToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | OrdinalToDate 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 73 | 74 |

OrdinalToDate

17 | 20 | 21 | 22 | 23 | 48 | 50 | 51 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
24 | :OrdinalToDate %year% %doy% yy mm dd
25 | ::
26 | :: By:   Ritchie Lawrence, 2002-09-29. Version 1.0
27 | ::
28 | :: Func: Returns a calendar date from an ISO 8601 Ordinal date.
29 | ::       For NT4/2000/XP/2003.
30 | ::
31 | :: Args: %1 year component to be converted, 4 digits (by val)
32 | ::       %2 day of year component to be converted, 001 to 366 (by val)
33 | ::       %3 var to receive year, 4 digits (by ref)
34 | ::       %4 var to receive month, 2 digits, 01 to 31 (by ref)
35 | ::       %5 var to receive day of month, 01 to 31 (by ref)
36 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
37 | setlocal ENABLEEXTENSIONS
38 | for /f "tokens=1-2" %%a in ('echo/%1 %2') do set /a yy=%%a,o=1%%b%%1000
39 | set /a z=14-1,z/=12,y=yy+4800-z,m=1+12*z-3,j=153*m+2
40 | set /a j=j/5+1+y*365+y/4-y/100+y/400-2432046,j+=o-1
41 | set /a a=j+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
42 | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
43 | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
44 | (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
45 | endlocal&set %3=%yy%&set %4=%mm%&set %5=%dd%&goto 46 | :EOF
47 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 

49 |

52 |

Parameters

53 |

%1 year component to be converted, 4 digits (by val)
54 | %2 day of year component to be converted, 001 to 366 (by val)
55 | %3 var to receive year, 4 digits (by ref)
56 | %4 var to receive month, 2 digits, 01 to 31 (by ref)
57 | %5 var to receive day of month, 01 to 31 (by ref)

58 |

Return Values

59 |

See parameters above.

60 |

Example

61 |
62 |

@echo off & setlocal ENABLEEXTENSIONS
63 | call :OrdinalToDate 2000 090 y m d
64 | echo/The date of the 90th day of the year 2000 is: %y%-%m%-%d%
65 | goto :EOF

66 |
67 |

Remarks

68 |

Note the second parameter (day of year) must be in the range 001 to 366 (not 1 to 366).

69 |

See Also

70 |

DateToOrdinal

71 |

 

72 |
75 |

 

76 | 77 | 78 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToOrdinal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DateToOrdinal 4 | 5 | 6 | 7 | 12 | 17 | 18 | 19 | 20 | 21 | 81 | 82 |

DateToOrdinal

22 | 25 | 26 | 27 | 28 | 56 | 57 | 58 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
29 | :DateToOrdinal %yy% %mm% %dd% year doy
30 | ::
31 | :: By:   Ritchie Lawrence, updated 2002-11-22. Version 1.1
32 | ::
33 | :: Func: Returns an ISO 8601 Ordinal date from a calendar date.
34 | ::       For NT4/2000/XP/2003.
35 | ::
36 | :: Args: %1 year component to be converted, 2 or 4 digits (by val)
37 | ::       %2 month component to be converted, 38 | leading zero ok (by val)
39 | ::       %3 day of month to be converted, 40 | leading zero ok (by val)
41 | ::       %4 var to receive year, 4 digits 42 | (by ref)
43 | ::       %5 var to receive doy of year, 44 | 001 to 366 (by ref)
45 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
46 | setlocal ENABLEEXTENSIONS
47 | set yy=%1&set mm=%2&set dd=%3
48 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
49 | set /a dd=100%dd%%%100,mm=100%mm%%%100
50 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
51 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-32045
52 | set /a y=yy+4799,k=y*365+y/4-y/100+y/400-31738,o=j-k+1
53 | if %o% LSS 100 set o=0%o%&if %o% LSS 10 set o=00%o%
54 | endlocal&set %4=%yy%&set %5=%o%&goto :EOF
55 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
59 |

Parameters

60 |

%1 year component to be converted, 2 or 4 digits (by val)
61 | %2 month component to be converted, leading zero ok (by val)
62 | %3 day of month to be converted, leading zero ok (by val)
63 | %4 var to receive year, 4 digits (by ref)
64 | %5 var to receive day of year, 001 to 366 (by ref)

65 |

Return Values

66 |

See parameters above.

67 |

Example

68 |
69 |

@echo off & setlocal ENABLEEXTENSIONS
70 | call :GetDate y m d
71 | call :DateToOrdinal %y% %m% %d% year doy
72 | echo/Today (as a Ordinal Date) is: %year%-%doy%
73 | goto :EOF

74 |
75 |

Remarks

76 |

None

77 |

See Also

78 |

OrdinalToDate

79 |

 

80 |
83 |

 

84 | 85 | 86 | -------------------------------------------------------------------------------- /01_Introduction/2_Using the Functions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Using the Functions 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 79 | 80 |

Using the Functions

12 |

Copy the Function

13 |

To use any of the functions, just copy and paste them to the end of your 14 | batch file. To prevent the function(s) from being executed after your 15 | main batch file code has run, add goto :EOF 16 | after your main batch file code but before the function(s). For example:-

17 |
18 |

Example 1a

19 |

 <-- Your main batch file code goes here
20 | goto :EOF
21 |  <-- 1st function here
22 |  <-- 2nd function here
23 |  <-- and so on...

24 |
25 |

[The documentation for each function includes an 26 | example that demonstrates how the function should be called. In these 27 | examples the actual function itself has been omitted to avoid unnecessarily 28 | duplicating code. When using any of the examples, remember to append the 29 | function to the end of your batch file.]

30 |

Calling the Function

31 |

The documentation for each function describes the number of required 32 | and optional parameters, the type of data each parameter should contain 33 | and whether the parameter must be passed 'by value' or 'by reference'. 34 | Both terms are used very loosely here and the reason is covered 35 | in great detail in the next section. Suffice to say that to pass a variable 36 | by value, call the function with a constant or expanded variable. And 37 | to pass by reference, call the function with the name of a variable.

38 |

By Value

39 |

To pass a parameter 'by value', either call the function with a constant 40 | or an expanded variable that contains the value. As an example, the Sleep 41 | function expects one parameter (a number of seconds) to be passed by 42 | value. Here are three ways to call the Sleep 43 | function with the value 9:-

44 |
45 |

Example 2a

46 |

call :Sleep 9

47 |

 

48 |

Example 2b

49 |

set AnyVarName=9
50 | call :Sleep %AnyVarName%

51 |

 

52 |

Example 2c

53 |

call :Sleep %1

54 |
55 |

Notes: In example 2b, practically any variable name could have been used. 56 | Example 2c assumes the value of 9 was passed to the batch file as a commandline 57 | parameter.

58 |

By Reference

59 |

To pass a value 'by reference' simply use the name of a variable. As 60 | an example, the GetDate 61 | function expects three parameters, all passed by reference. Here's how 62 | to call the GetDate 63 | function and use the 'returned' values:-

64 |
65 |

Example 3a

66 |

call :GetDate yy mm dd
67 | echo/Todays date is: %yy%-%mm%-%dd%

68 |
69 |

Note that although example 3a used the variable names yy, 70 | mm and dd, 71 | practically any variable names could have been used. For instance, example 72 | 3b is functionally equivalent to example 3a (but not so easy to understand).

73 |
74 |

Example 3b

75 |

call :GetDate x y z
76 | echo/Todays date is: %x%-%y%-%z%

77 |
78 |
 
81 | 82 | 83 | -------------------------------------------------------------------------------- /File and Directory Functions/GetDirStats.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 76 | 77 |

GetDirStats

17 | 19 | 20 | 21 | 22 | 49 | 50 |
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :GetDirStats %dir% files dirs bytes
24 | ::
25 | :: By:   Ritchie Lawrence, updated 2003-09-18. Version 1.1
26 | ::
27 | :: Func: Returns total number of files, directories and bytes (includes
28 | ::       sub-directories and hidden/system 29 | files/dirs). For NT4/2000/XP/2003
30 | ::
31 | :: Args: %1 Name of directory, absolute or relative (by val)
32 | ::       %2 var to receive total number 33 | of files (by ref)
34 | ::       %3 var to receive total number 35 | of directories (by ref)
36 | ::       %4 var to receive total number 37 | of bytes (by ref)
38 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39 | setlocal ENABLEEXTENSIONS & set "var="
40 | pushd %1 2>nul || (md;2>nul & endlocal & goto :EOF)
41 | for /f "tokens=1-5* delims=," %%a in ('compact/s /q^|findstr 42 | ^^^^[0-O]') ^
43 | do (call set "var=%%var%% %%a%%b%%c%%d%%e")
44 | for /f "tokens=2,5,15" %%a in ('echo/%var%') do (popd & 45 | endlocal ^
46 |   & set "%4=%%c" & set/a %3=%%b-1 & 47 | set/a "%2=%%a-%%b+1" & goto :EOF)
48 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
51 |

Parameters

52 |

%1 Name of directory, absolute or relative (by val)
53 | %2 var to receive total number of files (by ref)
54 | %3 var to receive total number of directories (by ref)
55 | %4 var to receive total number of bytes (by ref)

56 |

Return Values

57 |

If the function succeeds, the errorlevel is set to zero. If the function 58 | fails (for example, due to invalid directory or insufficient permissions), 59 | then the errorlevel is set to one.

60 |

Example

61 |
62 |

@echo off & setlocal ENABLEEXTENSIONS
63 | call :GetDirStats %SystemRoot% f d b
64 | echo/%SystemRoot% has %f% files and %d% dirs. Total size is %b% bytes 65 | .
66 | goto :EOF

67 |
68 |

Remarks

69 |

The statistics returned by GetDirStats are identical to those returned 70 | by Windows Explorer. If a difference is observed, this is probably caused 71 | by the TEMP folder being included in the enumeration.

72 |

See Also

73 |

FileSizeComp

74 |

 

75 |
78 | 79 | 80 | -------------------------------------------------------------------------------- /File and Directory Functions/FileSizeComp.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | FileSizeComp 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 82 | 83 |

FileSizeComp

17 | 19 | 20 | 21 | 22 | 52 | 53 |
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :FileSizeComp %file% %operand% %size%
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-08-22. Version 1.0
26 | ::
27 | :: Func: Compares size of specified file against specified size using 28 |
29 | ::       specified operand. Errorlevel 30 | set to zero if result is true, and
31 | ::       set to 1 if false or -1 if 32 | file not found. For NT4/2000/XP/2003.
33 | ::
34 | :: Args: %1 File to compare (by val)
35 | ::       %2 Comparison operator <EQU 36 | | NEQ | LSS | LEQ | GTR | GEQ> (by val)
37 | ::       %3 Size to compare in bytes 38 | (by val)
39 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
40 | setlocal ENABLEEXTENSIONS & set "filter=2^>nul^|findstr/b 41 | /v /c:" ""
42 | for /f "tokens=3" %%a in ('dir/-c %1 %filter%') do set z=%%a 43 | 0000000000000
44 | if not defined z (endlocal & fc;: 2>nul & goto :EOF) else 45 | (set z=%z:~0,13%)
46 | set "c=%3 0000000000000" & call set "c=%%c:~0,13%%"
47 | for /f "tokens=1-4" %%a in ("%z% %c%") do set 48 | "z=%%b%%a" & set "c=%%d%%c"
49 | (md;2>nul & if "%z%" %2 "%c%" ver>nul) 50 | & (endlocal & goto:EOF)
51 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
54 |

Parameters

55 |

%1 File to compare (by val)
56 | %2 Comparison operator <EQU | NEQ | LSS | LEQ | GTR | GEQ> 57 | (by val)
58 | %3 Size to compare in bytes (by val)

59 |

Return Values

60 |

See parameters above.

61 |

Example

62 |
63 |

@echo off & setlocal ENABLEEXTENSIONS
64 | call :FileSizeComp c:\logs\backup.log GTR 1048576 || goto :EOF
65 | echo/c:\logs\backup.log is larger than 1MB
66 | goto :EOF

67 |
68 |

Remarks

69 |

The comparison operator (parameter 2) must be uppercase otherwise the 70 | function will fail under Windows NT 4.0. If a non Windows NT 4.0 solution 71 | is required, it is more efficient to use the enhanced parameter modifiers 72 | present in Windows 2000 and later. For example:-

73 |
74 |

@echo off & setlocal ENABLEEXTENSIONS
75 | for %%a in (c:\logs\backup.log) do if %%~za LEQ 1048576 goto :EOF
76 | echo/c:\logs\backup.log is larger than 1MB
77 | goto :EOF

78 |
79 |

See Also

80 |

 

81 |
84 | 85 | 86 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToDays.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DateToDays 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 80 | 81 |

DateToDays

17 | 19 | 20 | 21 | 22 | 48 | 49 | 50 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DateToDays %yy% %mm% %dd% days
24 | ::
25 | :: By:   Ritchie Lawrence, 2002-09-26. Version 1.0
26 | ::
27 | :: Func: Returns the number of days elapsed since 1st January 1970 28 | for a
29 | ::       given date. For NT4/2000/XP/2003.
30 | ::
31 | :: Args: %1 year component used to create days, 2 or 4 digits (by 32 | val)
33 | ::       %2 month component used to 34 | create days, leading zero ok (by val)
35 | ::       %3 date of month used to create 36 | days, leading zero ok (by val)
37 | ::       %4 var to receive number of 38 | elapsed days (by ref)
39 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
40 | setlocal ENABLEEXTENSIONS
41 | set yy=%1&set mm=%2&set dd=%3
42 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
43 | set /a dd=100%dd%%%100,mm=100%mm%%%100
44 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
45 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
46 | endlocal&set %4=%j%&goto :EOF
47 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
51 |

Parameters

52 |

%1 year component used to create days, 2 or 4 digits (by val)
53 | %2 month component used to create days, leading zero ok (by 54 | val)
55 | %3 date of month used to create days, leading zero ok (by val)
56 | %4 var to receive number of elapsed days (by ref)

57 |

Return Values

58 |

See parameters above.

59 |

Example

60 |
61 |

@echo off & setlocal ENABLEEXTENSIONS
62 | call :DateToDays 2003 12 31 days
63 | echo/Difference between 1970-01-01 and 2003-12-31 is %days% days
64 | goto :EOF

65 |
66 |

Remarks

67 |

Use in conjunction with the DaysToDate 68 | function to perform date arithmetic with a resolution of one day. This 69 | function is virtually identical to the DateToMJD 70 | function except a different reference date (day 0) has been used. The 71 | reference date 1970-01-01, allows for interoperability with MinsToDays 72 | and SecsToDays which both share the same 73 | reference date.

74 |

Also note many Windows registry dates are stored as seconds elapsed since 75 | 1970-01-01 00:00:00 UTC.
76 |

77 |

See Also

78 |

DaysToDate, MJDToDate

79 |
82 |

 

83 | 84 | 85 | -------------------------------------------------------------------------------- /Date and Time Functions/WeekToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | WeekToDate 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 83 | 84 |

WeekToDate

17 | 20 | 21 | 22 | 23 | 58 | 59 | 60 |
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 24 |
25 | :WeekToDate %yn% %cw% %dw% yy mm dd
26 | ::
27 | :: By:   Ritchie Lawrence, 2002-10-04. Version 1.0
28 | ::
29 | :: Func: Returns a calendar date from an ISO 8601 Week date.
30 | ::       For NT4/2000/XP/2003.
31 | ::
32 | :: Args: %1 year to convert, 2 or 4 digits (by val)
33 | ::       %2 calendar week to convert, 34 | 2 digits, 01 to 53 (by val)
35 | ::       %3 day of week to convert, 36 | 1 digit, 1 to 7 (by val)
37 | ::       %4 var to receive year, 4 digits 38 | (by ref)
39 | ::       %5 var to receive month, 2 40 | digits, 01 to 12 (by ref)
41 | ::       %6 var to receive day of month, 42 | 2 digits, 01 to 31 (by ref)
43 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
44 | setlocal ENABLEEXTENSIONS
45 | set yn=%1&set cw=%2&set dw=%3
46 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
47 | set /a cw=100%cw%%%100,d=3210654
48 | set /a z=14-1,z/=12,y=yn+4800-z,m=1+12*z-3,Jt=153*m+2
49 | set /a Jt=Jt/5+1+y*365+y/4-y/100+y/400-32045,wt=Jt%%7
50 | call set at=%%d:~%wt%,1%%&set /a Jt=at+Jt-3
51 | set /a Jt=Jt+7*cw+dw-9,a=Jt+32045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
52 | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
53 | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
54 | (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
55 | endlocal&set %4=%yy%&set %5=%mm%&set %6=%dd%&goto 56 | :EOF
57 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
61 |

Parameters

62 |

%1 year to convert, 2 or 4 digits (by val)
63 | %2 calendar week to convert, 2 digits, 01 to 53 (by val)
64 | %3 day of week to convert, 1 digit, 1 to 7 (by val)
65 | %4 var to receive year, 4 digits (by ref)
66 | %5 var to receive month, 2 digits, 01 to 12 (by ref)
67 | %6 var to receive day of month, 2 digits, 01 to 31 (by ref)

68 |

Return Values

69 |

See parameters above.

70 |

Example

71 |
72 |

@echo off & setlocal ENABLEEXTENSIONS
73 | call :WeekToDate 2000 15 2 yy mm dd
74 | echo/The date on day 2 of week 15 of the year 2000 is: %yy%-%mm%-%dd%
75 | goto :EOF

76 |
77 |

Remarks

78 |

None.

79 |

See Also

80 |

DateToWeek

81 |

 

82 |
85 |

 

86 | 87 | 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Table of Contents 2 | 3 | * [About](#about) 4 | * [Usage](#usage) 5 | * [Library Contents](#library-contents) 6 | - [Tutorials](#tutorials) 7 | - [Date and Time Functions](#date-and-time-functions) 8 | - [File and Directory Functions](#file-and-directory-functions) 9 | - [Network Functions](#network-functions) 10 | - [System Functions](#system-functions) 11 | * [Revisions](#revisions) 12 | * [Copyright and License](#copyright-and-license) 13 | 14 | ## About 15 | 16 | This web based library was created in 2002 and featured a collection of date and time conversion functions. Later additions includes file, network and system functions. The library is no longer developed and is now archived on Github. Note, the webpages were designed for display in a treeview which itself was written in PHP/Javascript. The treeview code has largely been removed. Some of the internal hyperlinks might be broken, but you should have little trouble finding the target page. Happy batching! 17 | 18 | ## Usage 19 | 20 | Download and extract the archive, open the page of interest in a browser. Copy and paste the function to use in a batch file. 21 | 22 | ## Library Contents 23 | 24 | ### Tutorials 25 | 26 | Title | Description 27 | ---|--- 28 | Batch Function Overview | What are they, why use them, and what do they look like? 29 | Using the Functions | How to use the functions, with examples. 30 | Batch Functions Explained | Detailed examination of batch functions. 31 | Reading Files | How to read lines from any position in a file. 32 | 33 | ### Date and Time Functions 34 | 35 | Function | Description 36 | ---|--- 37 | DateToDays | Converts a calendar date to the number of days elapsed since 1970-01-01 38 | DateToDOW | Returns the day of week number for a given calendar date 39 | DateToMins | Converts a calendar date to the number of minutes elapsed since 1970-01-01 00:00 40 | DateToMJD | Converts a UTC date to a Modified Julian Day 41 | DateToOrdinal | Returns an ordinal date from a calendar date as described by ISO 8601 42 | DateToSecs | Converts a calendar date to the number of seconds elapsed since 1970-01-01 00:00:00 43 | DateToWeek | Returns an ISO 8601 Week date from a calendar date 44 | DayName | Returns the name of the day of week from the day of week number 45 | DayNumber | Returns the number of the day of week from the day of week name 46 | DaysToDate | Converts the number of days elapsed since 1970-01-01 to a calendar date 47 | GetDate | Returns the local system date 48 | GetTime | Returns the local system time 49 | MinsToDate | Converts the number of minutes elapsed since 1970-01-01 00:00 to a calendar date and time 50 | MJDToDate | Converts a Modified Julian Day to a UTC date 51 | MonthName | Returns the name of month from the month number 52 | MonthNumber | Returns the number of month from a month name 53 | OrdinalToDate | Returns a calendar date from an ISO 8601 Ordinal date 54 | SecsToDate | Converts the number of seconds elapsed since 1970-01-01 00:00:00 to a calendar date and time 55 | WeekToDate | Returns a calendar date from an ISO 8601 Week date 56 | 57 | ### File and Directory Functions 58 | 59 | Function | Description 60 | ---|--- 61 | FileSizeComp | Compares the size of a specified file 62 | GetDirStats | Returns the number of files, subdirectories and total size of a specified directory 63 | IsDirEmpty | Determines if the specified directory is empty 64 | IsInPath | Determines if the specified files are in the current directory or in a directory listed in the path statement 65 | 66 | ### Network Functions 67 | 68 | Function | Description 69 | ---|--- 70 | GetDG | Returns the default gateway 71 | GetIP | Returns the IP address of the primary adapter 72 | GetMAC | Returns the MAC address of the primary adapter 73 | GetNA | Returns the network address of the primary adapter 74 | GetSM | Returns the subnet mask of the primary adapter 75 | 76 | ### System Functions 77 | 78 | Function | Description 79 | ---|--- 80 | GetOS | Returns the operating system version 81 | IsRunning | Determines if the specified service is running 82 | Sleep | Suspends execution of the current batch file for a specified interval 83 | Timer | Returns the number of elapsed seconds since the function was last called and first called 84 | Uptime | Returns the elapsed days, hours, minutes and seconds since the system booted 85 | 86 | ## Revisions 87 | 88 | Revision | Date | Changes 89 | ---|---|--- 90 | 1.0 | 2016-06-18 | The webpages that make up the library have been removed from [www.commandline.co.uk](http://www.commandline.co.uk/) and archived at Github. 91 | 92 | ## Copyright and License 93 | 94 | Code and documentation copyright 2002-2016 Ritchie Lawrence. Code released under [MIT License](https://github.com/ritchielawrence/mtee/blob/master/LICENSE.txt) 95 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToWeek.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DateToWeek 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 86 | 87 |

DateToWeek

17 | 19 | 20 | 21 | 22 | 59 | 61 | 62 |
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 23 |
24 | :DateToWeek %yy% %mm% %dd% yn cw dw
25 | ::
26 | :: By:   Ritchie Lawrence, Updated 2002-11-20. Version 1.1
27 | ::
28 | :: Func: Returns an ISO 8601 Week date from a calendar date.
29 | ::       For NT4/2000/XP/2003.
30 | ::
31 | :: Args: %1 year component to be converted, 2 or 4 digits (by val)
32 | ::       %2 month component to be converted, 33 | leading zero ok (by val)
34 | ::       %3 day of month to be converted, 35 | leading zero ok (by val)
36 | ::       %4 var to receive year, 4 digits 37 | (by ref)
38 | ::       %5 var to receive calendar 39 | week, 2 digits, 01 to 53 (by ref)
40 | ::       %6 var to receive day of week, 41 | 1 digit, 1 to 7 (by ref)
42 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43 | setlocal ENABLEEXTENSIONS
44 | set yy=%1&set mm=%2&set dd=%3
45 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
46 | set /a dd=100%dd%%%100,mm=100%mm%%%100
47 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,Jd=153*m+2
48 | set /a Jd=Jd/5+dd+y*365+y/4-y/100+y/400-32045
49 | set /a y=yy+4798,Jp=y*365+y/4-y/100+y/400-31738,t=Jp+3,Jp=t-t%%7
50 | set /a y=yy+4799,Jt=y*365+y/4-y/100+y/400-31738,t=Jt+3,Jt=t-t%%7
51 | set /a y=yy+4800,Jn=y*365+y/4-y/100+y/400-31738,t=Jn+3,Jn=t-t%%7
52 | set /a Jr=%Jp%,yn=yy-1,yn+=Jd/Jt,yn+=Jd/Jn
53 | if %Jd% GEQ %Jn% (set /a Jr=%Jn%) else (if %Jd% GEQ %Jt% set /a Jr=%Jt%)
54 | set /a diff=Jd-Jr,cw=diff/7+1,wd=diff%%7,wd+=1
55 | if %cw% LSS 10 set cw=0%cw%
56 | endlocal&set %4=%yn%&set %5=%cw%&set %6=%wd%&goto 57 | :EOF
58 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
60 |
63 |

Parameters

64 |

%1 year component to be converted, 2 or 4 digits (by val)
65 | %2 month component to be converted, leading zero ok (by val)
66 | %3 day of month to be converted, leading zero ok (by val)
67 | %4 var to receive year, 4 digits (by ref)
68 | %5 var to receive calendar week, 2 digits, 01 to 53 (by ref)
69 | %6 var to receive day of week, 1 digit, 1 to 7 (by ref)

70 |

Return Values

71 |

See parameters above.

72 |

Example

73 |
74 |

@echo off & setlocal ENABLEEXTENSIONS
75 | call :GetDate y m d
76 | call :DateToWeek %y% %m% %d% yn cw dw
77 | echo/Today (in ISO 8601 Week Date format) is: %yn%-W%cw%-%dw%
78 | goto :EOF

79 |
80 |

Remarks

81 |

None.

82 |

See Also

83 |

WeekToDate

84 |

 

85 |
88 |

 

89 | 90 | 91 | -------------------------------------------------------------------------------- /Date and Time Functions/MinsToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | MinsToDate 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 87 | 88 |

MinsToDate

17 | 20 | 21 | 22 | 23 | 55 | 56 | 57 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
24 | :MinsToDate %mins% yy mm dd hh nn
25 | ::
26 | :: By:   Ritchie Lawrence, updated 2003-07-13. Version 1.1
27 | ::
28 | :: Func: Returns a calendar date and time of day from the number of
29 | ::       elapsed minutes since 1st January 1970 00:00.
30 | ::       For NT4/2000/XP/2003.
31 | ::
32 | :: Args: %1 minutes used to create calendar date and time of day (by 33 | val)
34 | ::       %2 var to receive year, 4 digits 35 | for all typical dates (by ref)
36 | ::       %3 var to receive month, 2 37 | digits, 01 to 12 (by ref)
38 | ::       %4 var to receive day of month, 39 | 2 digits, 01 to 31 (by ref)
40 | ::       %5 var to receive hours, 2 41 | digits, 00 to 23 (by ref)
42 | ::       %6 var to receive minutes, 43 | 2 digits, 00 to 59 (by ref)
44 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
45 | setlocal ENABLEEXTENSIONS
46 | set /a i=%1,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
47 | set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
48 | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
49 | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
50 | (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
51 | (if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
52 | endlocal&set %6=%nn%&set %5=%hh%&set %4=%dd%&^
53 | set %3=%mm%&set %2=%yy%&goto :EOF
54 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
58 |

Parameters

59 |

%1 minutes used to create calendar date and time of day (by 60 | val)
61 | %2 var to receive year, 4 digits for all typical dates (by ref)
62 | %3 var to receive month, 2 digits, 01 to 12 (by ref)
63 | %4 var to receive day of month, 2 digits, 01 to 31 (by ref)
64 | %5 var to receive hours, 2 digits, 00 to 23 (by ref)
65 | %6 var to receive minutes, 2 digits, 00 to 59 (by ref)

66 |

Return Values

67 |

See parameters above.

68 |

Example

69 |
70 |

@echo off & setlocal ENABLEEXTENSIONS
71 | call :GetDate y m d
72 | call :GetTime h n s t
73 | call :DateToMins %y% %m% %d% %h% %n% mins
74 | set /a mins+=15
75 | call :MinsToDate %mins% y m d h n
76 | echo/Date and time in 15 minutes: %y%-%m%-%d% %h%:%n%
77 | goto :EOF

78 |
79 |

Remarks

80 |

Use in conjunction with DateToMins to perform 81 | date arithmetic with a resolution of one minute.

82 |

Range is from 0 to 2147483647 or (2^31)-1 minutes, which gives a date 83 | range from 1970-01-01 00:00 to 6053-01-23 02:07.

84 |

See Also

85 |

DateToMins

86 |
89 |

 

90 | 91 | 92 | -------------------------------------------------------------------------------- /Date and Time Functions/GetTime.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | GetTime 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 |
40 | 41 |

GetTime

42 | 43 | 44 | 45 | 46 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
64 | 65 | 66 | :GetTime hh nn ss tt
67 | 68 | 69 | ::
70 | 71 | 72 | :: By:   Ritchie Lawrence, updated 2007-05-12. Version 1.3
73 | 74 | 75 | ::
76 | 77 | 78 | :: Func: Loads local system time components into args 1 to 4.
79 | 80 | 81 | ::       For NT4/2000/XP/2003
82 | 83 | 84 | ::
85 | 86 | 87 | :: Args: %1 Var to receive hours, 2 digits, 00 to 23 (by ref)
88 | 89 | 90 | ::       %2 Var to receive minutes, 91 | 2 digits, 00 to 59 (by ref)
92 | 93 | 94 | ::       %3 Var to receive seconds, 95 | 2 digits, 00 to 59 (by ref)
96 | 97 | 98 | ::       %4 Var to receive centiseconds, 99 | 2 digits, 00 to 99 (by ref)
100 | 101 | 102 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
103 | 104 | 105 | setlocal ENABLEEXTENSIONS
106 | 107 | 108 | for /f "tokens=5-8 delims=:,. " %%a in ('echo/^|time') do 109 | (
110 | 111 | 112 |   set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
113 | 114 | 115 | if 1%hh% LSS 20 set hh=0%hh%
116 | 117 | 118 | endlocal&set %1=%hh%&set %2=%nn%&set %3=%ss%&set %4=%cs%&goto 119 | :EOF
120 | 121 | 122 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
136 | 137 | 138 | 139 | 140 |

Parameters

141 | 142 | 143 | 144 | 145 |

%1 Var to receive hours, 2 digits, 00 to 23 (by ref)
146 | 147 | 148 | %2 Var to receive minutes, 2 digits, 00 to 59 (by ref)
149 | 150 | 151 | %3 Var to receive seconds, 2 digits, 00 to 59 (by ref)
152 | 153 | 154 | %4 Var to receive centiseconds, 2 digits, 00 to 99 (by ref)

155 | 156 | 157 | 158 | 159 |

Return Values

160 | 161 | 162 | 163 | 164 |

See parameters above.

165 | 166 | 167 | 168 | 169 |

Example

170 | 171 | 172 | 173 | 174 |
175 | 176 | 177 |

@echo off & setlocal ENABLEEXTENSIONS
178 | 179 | 180 | call :GetTime h n s t
181 | 182 | 183 | echo/Time is: %h%:%n%:%s%
184 | 185 | 186 | goto :EOF

187 | 188 | 189 |
190 | 191 | 192 | 193 | 194 |

Remarks

195 | 196 | 197 | 198 | 199 |

This function is independant of regional settings on English versions of Windows.

200 | 201 | 202 |

Update: Added a comma to the delims statement - thanks to 'Alvydas'.

203 | 204 | 205 | 206 | 207 |

See Also

208 | 209 | 210 | 211 | 212 |

GetDate

213 | 214 | 215 |
224 | 225 | 226 |

 

227 | 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToMins.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DateToMins 5 | 6 | 7 | 12 | 13 | 14 | 15 | 88 | 89 |

DateToMins

16 | 18 | 19 | 20 | 21 | 58 | 59 | 60 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
22 | :DateToMins %yy% %mm% %dd% %hh% %mm% result
23 | ::
24 | :: By:   Ritchie Lawrence, updated 2003-04-03. Version 1.1
25 | ::
26 | :: Func: Returns the number of elapsed minutes since 1970-01-01 00:00
27 | ::       for a given date. For NT4/2K/XP/2003
28 | ::
29 | :: Args: %1 years to convert, 2 or 4 digit (by val)
30 | ::       %2 months to convert, 1/01 31 | to 12, leading zero ok (by val)
32 | ::       %3 days to convert, 1/01 to 33 | 31, leading zero ok (by val)
34 | ::       %4 hours to convert, 1/01 to 35 | 12 for 12hr times (minutes must be
36 | ::          suffixed 37 | by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
38 | ::       %5 mins to convert, 00-59 only, 39 | suffixed by a/p if 12hr (by val)
40 | ::       %6 var to receive number of 41 | elapsed minutes (by ref)
42 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43 | setlocal ENABLEEXTENSIONS
44 | set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
45 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
46 | set /a dd=100%dd%%%100,mm=100%mm%%%100
47 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
48 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
49 | if 1%hh% LSS 20 set hh=0%hh%
50 | if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set 51 | hh=1%hh%&set/a hh-=88
52 | if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set 53 | hh=00
54 | if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
55 | set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
56 | endlocal&set %6=%j%&goto :EOF
57 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
61 |

Parameters

62 |

%1 years to convert, 2 or 4 digit (by val)
63 | %2 months to convert, 1/01 to 12, leading zero ok (by val)
64 | %3 days to convert, 1/01 to 31, leading zero ok (by val)
65 | %4 hours to convert, 1/01 to 12 for 12hr times (minutes must 66 | be suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
67 | %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
68 | %6 var to receive number of elapsed minutes (by ref)

69 |

Return Values

70 |

See parameters above.

71 |

Example

72 |
73 |

@echo off & setlocal ENABLEEXTENSIONS
74 | call :GetDate y m d
75 | call :GetTime h n s t
76 | call :DateToMins %y% %m% %d% %h% %n% mins
77 | echo/Minutes elapsed since 1970-01-01 00:00: %mins%
78 | goto :EOF

79 |
80 |

Remarks

81 |

Use in conjunction with MinsToDate to perform 82 | date arithmetic with a resolution of one minute.

83 |

Date range is from 1970-01-01 00:00 to 6053-01-23 02:07, which gives 84 | a range of 0 to 2147483647 or (2^31)-1 minutes

85 |

See Also

86 |

MinsToDate

87 |
90 |

 

91 | -------------------------------------------------------------------------------- /Date and Time Functions/0 Date and Time Functions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Introduction to Date and Time Functions 4 | 5 | 6 | 7 | 8 | 9 | 10 | 105 | 106 |

Date and Time Functions

11 |

Overview

12 |

Note: Any attempt to optimise the arithmetic used in these functions 13 | will probably cause the functions to fail under NT4 - as contrary to Microsoft's 14 | documentation, parantheses are not supported and arithmetic operator precedence 15 | is incorrect.

16 |

Parameters

17 |

When passing dates and times to the Date and Times Functions, the following 18 | formats must be observed:-

19 |
    20 |
  • Years must be two or four digits. Years in the range 70 to 21 | 99 will be treated as years from the twentieth century, years from 00 22 | to 69 as the twenty-first.
  • 23 |
  • Months must be one or two digits in the range 1 to 12 or 24 | 01 to 12.
  • 25 |
  • Day of month must be one or two digits in the range 1 to 26 | 31 or 01 to 31.
  • 27 |
  • Hours must be in 12 or 24 hour format. For 12 hour format 28 | the hours must be in the range 1 to 12 or 01 to 12. If 12 hour format 29 | is used, then the minutes parameter must be immediately followed by 30 | 'a' or 'p' (eg 00a or 34p). For 24 hour format, the hours must be in 31 | the range 0 to 23 or 00 to 23.
  • 32 |
  • Minutes must be two digits in the range 00 to 59.
  • 33 |
  • Seconds must be two digits in the range 00 to 59.
  • 34 |
35 |

The reason for accepting two digit years and 12 hour times is mainly 36 | for compatibilty with the output of the DIR command, which may use one 37 | or both of these formats.

38 |

Return Values

39 |

The Date and Time Functions that return dates and/or times will use the 40 | following formats:-

41 |
    42 |
  • Years will be four digits.
  • 43 |
  • Months will be two digits in the range 01 to 12.
  • 44 |
  • Day of month will be two digits in the range 01 to 31.
  • 45 |
  • Hours will be 24 hour format in the range 00 to 23.
  • 46 |
  • Minutes will be two digits in the range 00 to 59.
  • 47 |
  • Seconds will be two digits in the range 00 to 59.
  • 48 |
49 |

These formats will be suitable for most applications and make it simple 50 | to use ISO 8601 formatted dates. Any leading zeros can be easily removed 51 | if required by prefixing the value with 100 and then taking modulus 100. 52 | For example:-

53 |
54 |

@echo off & setlocal ENABLEEXTENSIONS
55 | set var=09
56 | set /a var=100%var%%%100
57 | echo/Leading zero has been removed: %var%

58 |
59 |

Suggested Formats

60 |

This is basically a very brief summary of the most commonly used date 61 | and time formats suggested by ISO 8601.

62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 82 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
ComponentBasic FormatExtended Format
Calendar DateYYYYMMDD Eg 19991231YYYY-MM-DD Eg 1999-12-31
Time of dayhhmmss Eg 235909hh:mm:ss Eg 23:59:09
Date and TimeYYYYMMDDThhmmss
81 | Eg 19991231T235909
YYYY-MM-DDThh:mm:ss
83 | Eg 1999-12-31T23:59:09
Ordinal Date YYYYDDD Eg 1999365YYYY-DDD Eg 1999-365
Week DateYYYYWwwD Eg 1999W525YYYY-Www-D Eg 1999-W52-5
96 |

References

97 |

HEASARC 98 | online date converter
99 | Calendar 100 | studies and conversion algorithms
101 | Summary 102 | of ISO 8601 Date and Time Standards
103 | ISO 8601:2004 in PDF
104 |

107 |

 

108 | 109 | 110 | -------------------------------------------------------------------------------- /System Functions/Uptime.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Uptime 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 93 | 94 |

Uptime

17 | 19 | 20 | 21 | 22 | 68 | 69 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :Uptime days hours mins [secs]
24 | ::
25 | :: By:   Ritchie Lawrence, 2003-09-24. Version 1.0
26 | ::
27 | :: Func: Obtains the number of days, hours, minutes and seconds of 28 | uptime.
29 | ::       For NT4/2000/XP/2003.
30 | ::
31 | :: Args: %1 var to receive number of days of uptime (by ref)
32 | ::       %2 var to receive number of hours of uptime (by ref)
33 | ::       %3 var to receive number of minutes of uptime (by ref)
34 | ::       %4 var to receive number of seconds of uptime (optional, by ref)
35 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
36 | setlocal ENABLEEXTENSIONS & set "c=net statistics work"
37 | set t=2&if "%date%z" LSS "A" set t=1
38 | for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') 39 | do (
40 |   for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
41 |     set %%a=%%d&set %%b=%%e&set %%c=%%f))
42 | for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
43 |   set "hh=%%a" & set "nn=%%b" & set "ss=%%c")
44 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
45 | set /a dd=100%dd%%%100,mm=100%mm%%%100
46 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
47 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
48 | if 1%hh% LSS 20 set hh=0%hh%
49 | set /a hh=100%hh%%%100,nn=100%nn%%%100,f=j*1440+hh*60+nn
50 | for /f "tokens=3-8 delims=/:M " %%a in ('%c%^|findstr/b /c:"Stat"') 51 | do (
52 |   set mm=%%a&set dd=%%b&set yy=%%c&set hh=%%d&set nn=%%e%%f)
53 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
54 | set /a dd=100%dd%%%100,mm=100%mm%%%100
55 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
56 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
57 | if 1%hh% LSS 20 set hh=0%hh%
58 | if {%nn:~2,1%} EQU {P} if "%hh%" NEQ "12" set hh=1%hh%&set/a 59 | hh-=88
60 | if {%nn:~2,1%} EQU {A} if "%hh%" EQU "12" set hh=00
61 | if {%nn:~2,1%} GEQ {A} set nn=%nn:~0,2%
62 | set /a hh=100%hh%%%100,nn=100%nn%%%100,s=j*1440+hh*60+nn,n=f-s
63 | set /a d=n/1440,n%%=1440,h=n/60,n%%=60
64 | endlocal & set "%1=%d%" & set "%2=%h%" & set "%3=%n%" ^
65 |   & (if "%4" NEQ "" set "%4=%ss%") & goto 66 | :EOF
67 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
70 |

Parameters

71 |

%1 var to receive number of days of uptime (by ref)
72 | %2 var to receive number of hours of uptime (by ref)
73 | %3 var to receive number of minutes of uptime (by ref)
74 | %4 var to receive number of seconds of uptime (optional, by ref)

75 |

Return Values

76 |

See parameters above.

77 |

Example

78 |
79 |

@echo off & setlocal ENABLEEXTENSIONS
80 | call :Uptime d h n s
81 | echo/Uptime is: %d% days, %h% hours, %n% minutes, %s% seconds.
82 | goto :EOF

83 |
84 |

Remarks

85 |

The uptime reported by this function is typically less than +/- 30 86 | seconds of the uptime reported by third party utilities such as SrvInfo 87 | from the Resource Kit and PsInfo 88 | from sysinternals.

89 |

See Also

90 |

 

91 |

 

92 |
95 | 96 | 97 | -------------------------------------------------------------------------------- /System Functions/Timer.html: -------------------------------------------------------------------------------- 1 | 2 | Timer 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 128 | 129 | 130 |
18 |

Timer

19 | 22 | 23 | 24 | 25 | 26 | 75 | 76 | 77 | 78 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
27 | :Timer ID
28 | ::
29 | :: By:   Ritchie Lawrence, 2002-10-10. Version 1.1
30 | :: Updated 2007-10-31 - thanks to Fabricio and Darren
31 | ::
32 | :: Func: Returns number of seconds elapsed since the function was last
33 | ::       called 34 | and first called. For NT4/2000/XP/2003.
35 | ::
36 | :: Args: %1 (by ref) The first time this function is called, this 37 | variable
38 | ::       is 39 | initialised to '<last> <first> 40 | <init>' where <last> and 41 | <first>
42 | ::       are zero 43 | and <init> is the number of elapsed seconds since
44 | :: 45 |       1970-01-01 46 | 00:00:00. This value is used by subsequent calls to
47 | :: 48 |       determine 49 | the elapsed number of seconds since the last call
50 | :: 51 |       (<last>) 52 | and the first call (<first>).
53 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
54 | setlocal ENABLEEXTENSIONS&call set ID=%%%1%%
55 | set t=2&if "%date%z" LSS "A" set t=1
56 | for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
57 |   for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') 58 | do (
59 |     set %%a=%%d&set 60 | %%b=%%e&set %%c=%%f))
61 | for /f "tokens=5-7 delims=:. " %%a in ('echo/^|time') do (
62 |   set hh=%%a&set nn=%%b&set ss=%%c)
63 | set /a dd=100%dd%%%100,mm=100%mm%%%100
64 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
65 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
66 | set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
67 | set /a j=j*86400+hh*3600+nn*60+ss
68 | for /f "tokens=1-3 delims= " %%a in ('echo/%ID%') do (
69 |   set l=%%a&set f=%%b&set c=%%c)
70 | if {%c%}=={} endlocal&set %1=0 0 %j%&goto :EOF
71 | set /a l=j-c-f,f+=l
72 | endlocal&set %1=%l% %f% %c%&goto :EOF
73 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
74 |
79 |

Parameters

80 |

%1 (by ref) The first time this 81 | function is called, this variable is initialised to 82 | '<last> <first> <init>' where 83 | <last> and <first> are zero and 84 | <init> is the number of elapsed seconds since 1970-01-01 85 | 00:00:00. This value is used by subsequent calls to determine the 86 | elapsed number of seconds since the last call (<last>) 87 | and the first call (<first>).

88 |

Return Values

89 |

See parameters above.

90 |

Example

91 |
92 |

@echo off & setlocal
93 | set var=
94 | call :Timer var
95 | call :show
96 |
97 | ::wait about 3 seconds
98 | ping 127.0.0.1 -n 4 >nul
99 | call :Timer var
100 | call :show
101 |
102 | ::wait about 7 seconds
103 | ping 127.0.0.1 -n 8 >nul
104 | call :Timer var
105 | call :show
106 | goto :EOF
107 |
108 | :show
109 | for /f "tokens=1-2 delims= " %%a in ('echo/%var%') do (
110 |   echo/Seconds since last call: %%a, seconds since 111 | first call: %%b
112 | )
113 | goto :EOF

114 |
115 |

Remarks

116 |

The first time the function is called, it should be 117 | passed an undefined variable.

118 |

Multiple timers can be used in parallel; just use a 119 | different variable name for each instance. See the function's header 120 | for more information.

121 |

Update: Corrected an error in the penultimate statement 122 | (it used to read: set /a l=j-c-l,f+=l) - thank you Fabricio and Darren 123 | for spotting this error.

124 |

See Also

125 |

Sleep

126 |

 

127 |
131 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToSecs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | DateToSecs 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 99 | 100 |

DateToSecs

17 | 19 | 20 | 21 | 22 | 65 | 66 | 67 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
23 | :DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
24 | ::
25 | :: By:   Ritchie Lawrence, updated 2002-08-13. Version 1.1
26 | ::
27 | :: Func: Returns number of seconds elapsed since 1st January 1970 28 | 00:00:00
29 | ::       for a given calendar date 30 | and time of day. For NT4/2000/XP/2003.
31 | ::
32 | :: Args: %1 year to convert, 2 or 4 digit (by val)
33 | ::       %2 month to convert, 1/01 to 34 | 12, leading zero ok (by val)
35 | ::       %3 day of month to convert, 36 | 1/01 to 31, leading zero ok (by val)
37 | ::       %4 hours to convert, 1/01 to 38 | 12 for 12hr times (minutes must be
39 | ::          suffixed 40 | by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
41 | ::       %5 mins to convert, 00-59 only, 42 | suffixed by a/p if 12hr (by val)
43 | ::       %6 secs to convert, 0-59 or 44 | 00-59 (by val)
45 | ::       %7 var to receive number of 46 | elapsed seconds (by ref)
47 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
48 | setlocal ENABLEEXTENSIONS
49 | set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set 50 | ss=%6
51 | if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
52 | set /a dd=100%dd%%%100,mm=100%mm%%%100
53 | set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
54 | set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
55 | if 1%hh% LSS 20 set hh=0%hh%
56 | if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set 57 | hh=1%hh%&set/a hh-=88
58 | if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set 59 | hh=00
60 | if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
61 | set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
62 | set /a j=j*86400+hh*3600+nn*60+ss
63 | endlocal&set %7=%j%&goto :EOF
64 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
68 |

Parameters

69 |

%1 year to convert, 2 or 4 digit (by val)
70 | %2 month to convert, 1/01 to 12, leading zero ok (by val)
71 | %3 day of month to convert, 1/01 to 31, leading zero ok (by val)
72 | %4 hours to convert, 1/01 to 12 for 12hr times (minutes must 73 | be suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
74 | %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
75 | %6 secs to convert, 0-59 or 00-59 (by val)
76 | %7
var to receive number of elapsed seconds (by ref)

77 |

Return Values

78 |

See parameters above.

79 |

Example

80 |
81 |

@echo off & setlocal ENABLEEXTENSIONS
82 | call :GetDate y m d
83 | call :GetTime h n s t
84 | call :DateToSecs %y% %m% %d% %h% %n% %s% secs
85 | echo/%secs% seconds have elapsed since 1970-01-01 00:00:00
86 | goto :EOF

87 |
88 |

Remarks

89 |

Use in conjunction with the SecsToDate 90 | function to perform date arithmetic with a resolution of one second. Note 91 | many Windows registry dates are stored as seconds elapsed since 1970-01-01 92 | 00:00:00 UTC.
93 |

94 |

Date range is from 1970-01-01 00:00:00 to 2038-01-19 03:14:07 which gives 95 | a range of 0 to 2147483647 or (2^31)-1 seconds.

96 |

See Also

97 |

SecsToDate

98 |
101 |

 

102 | 103 | 104 | -------------------------------------------------------------------------------- /Date and Time Functions/GetDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | GetDate 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 156 | 157 | 158 | 159 | 160 |
27 |

GetDate

28 | 29 | 30 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
42 | 43 | :GetDate yy mm dd
44 | 45 | ::
46 | 47 | :: By:   Ritchie Lawrence, 2002-06-15. Version 1.0
48 | 49 | ::
50 | 51 | :: Func: Loads local system date components into args 1 to 3.
52 | 53 | ::       For NT4/2000/XP/2003.
54 | 55 | ::
56 | 57 | :: Args: %1 var to receive year, 4 digits (by ref)
58 | 59 | ::       %2 var to receive month, 2 60 | digits, 01 to 12 (by ref)
61 | 62 | ::       %3 Var to receive day of month, 63 | 2 digits, 01 to 31 (by ref)
64 | 65 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
66 | 67 | setlocal ENABLEEXTENSIONS
68 | 69 | set t=2&if "%date%z" LSS "A" set t=1
70 | 71 | for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') 72 | do (
73 |   for /f "tokens=%t%-4 delims=.-/ " %%d 74 | in ('date/t') do (
75 |     set %%a=%%d&set 76 | %%b=%%e&set %%c=%%f))
77 | 78 | endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto 79 | :EOF
80 | 81 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
90 | 91 | 92 |

Parameters

93 | 94 | 95 |

%1 variable to receive year, 4 digits (by ref)
96 | 97 | %2 variable to receive month, 2 digits, 01 to 12 (by ref)
98 | 99 | %3 variable to receive day of month, 2 digits, 01 to 31 (by 100 | ref)

101 | 102 | 103 |

Return Values

104 | 105 | 106 |

See parameters above.

107 | 108 | 109 |

Example

110 | 111 | 112 |
113 | 114 |

@echo off & setlocal ENABLEEXTENSIONS
115 | 116 | call :GetDate y m d
117 | 118 | echo/Today is: %y%-%m%-%d%
119 | 120 | goto :EOF

121 | 122 |
123 | 124 | 125 |

Remarks

126 | 127 | 128 |

This function is independant of regional settings on English 129 | versions of Windows. For non-English versions of Windows, you will most 130 | likely have to modify the last line of the function to reflect the 131 | output from the DATE command. For example, in a command prompt type 132 | DATE and press enter twice. On a typical French version of Windows the 133 | output is:-

134 |

La date du jour est : 31/01/2007
135 | 136 | Entrez la nouvelle date : (jj-mm-aa)

137 |

On the second line, the "aa" represents the year and "jj" the day of the month, therefore you need to edit the last line of the function so that "yy" becomes "aa" and "dd" becomes "jj", like this:-

138 |

139 | endlocal&set %1=%aa%&set %2=%mm%&set %3=%jj%&goto 140 | :EOF

141 |

To learn more about this 142 | function, see the thread from the alt.msdos.batch.nt newsgroup titled 143 | "Formatting 144 | a date".

145 | 146 | 147 |

See Also

148 | 149 | 150 |

GetTime

151 | 152 | 153 |

 

154 | 155 |
161 | 162 |

 

163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /Batch Howto's/Reading Files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Reading Files 4 | 5 | 6 | 7 | 8 | 9 | 115 | 116 |

Reading Files

10 | 13 |
14 |

for /f "delims=" %%a in (input.txt) do ...

15 |

for /f "delims=" %%a in ('type input.txt') 16 | do ...

17 |

for /f "delims=" %%a in ('more ^< input.txt') 18 | do ...

19 |
20 | 27 |
30 |

one
31 | two
32 | three
33 | four
34 | five
35 | six
36 | seven
37 | eight
38 | nine
39 | ten

40 |

Displaying the first line

41 |

This example prints one.

42 |

@echo off & setlocal ENABLEEXTENSIONS
43 | set "first="
44 | for /f "delims=" %%a in ('more ^< numbers.txt') do (
45 |   if not defined first set first=%%a
46 | )
47 | echo/%first%

48 |
49 |

Displaying the first X lines

50 |

This example prints one, two and three.

51 |
52 |

@echo off & setlocal ENABLEEXTENSIONS
53 | set "lines=3"
54 | set i=-1
55 | set "ok=" 56 |
57 | for /f "delims=" %%a in ('more ^< numbers.txt') 58 | do (
59 |   set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
60 |     if "%%z"=="%lines%" set ok=1
61 |   )
62 |   if not defined ok echo/%%a
63 | )

64 |

Displaying the last line

65 | 66 |
67 |

@echo off & setlocal ENABLEEXTENSIONS
68 | for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
69 | echo/%last%

70 |

Displaying the last X lines

71 | 72 |
73 |

@echo off & setlocal ENABLEEXTENSIONS
74 | set "lines=2"
75 | for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a 76 | skip=%%a-lines
77 | for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') 78 | do (
79 |   echo/%%a
80 | )

81 |
82 |

Displaying the Nth line

83 |

This example prints three. 84 | Note that instead of using the more command's /e switch, 85 | the skip option could have been used with 86 | the for 87 | /f command, however, this fails is it is set to any number less 88 | than one.

89 |

@echo off & setlocal ENABLEEXTENSIONS
90 | set LineNo=3
91 | set "line="
92 | set/a LineNo-=1
93 | for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') 94 | do (
95 |   if not defined line set "line=%%a"
96 | )
97 | echo/%line%

98 |

Displaying the Nth line plus X number of lines

99 |

This example prints five and six.

100 |
101 |

@echo off & setlocal ENABLEEXTENSIONS
102 | set start=5
103 | set "lines=2"
104 | set/a i=-1,start-=1
105 | set "ok="
106 | for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') 107 | do (
108 |   set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
109 |     if "%%z"=="%lines%" set ok=1
110 |   )
111 |   if not defined ok echo/%%a
112 | )
113 |

114 |
117 | 118 | 119 | -------------------------------------------------------------------------------- /Date and Time Functions/SecsToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | SecsToDate 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 100 | 101 |

SecsToDate

17 | 20 | 21 | 22 | 23 | 59 | 60 | 61 |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
24 | :SecsToDate %secs% yy mm dd hh nn ss
25 | ::
26 | :: By:   Ritchie Lawrence, updated 2002-07-24. Version 1.1
27 | ::
28 | :: Func: Returns a calendar date and time of day from the number of
29 | ::       elapsed seconds since 1st January 30 | 1970 00:00:00. For
31 | ::       NT4/2000/XP/2003.
32 | ::
33 | :: Args: %1 seconds used to create calendar date and time of day (by 34 | val)
35 | ::       %2 var to receive year, 4 digits 36 | for all typical dates (by ref)
37 | ::       %3 var to receive month, 2 38 | digits, 01 to 12 (by ref)
39 | ::       %4 var to receive day of month, 40 | 2 digits, 01 to 31 (by ref)
41 | ::       %5 var to receive hours, 2 42 | digits, 00 to 23 (by ref)
43 | ::       %6 var to receive minutes, 44 | 2 digits, 00 to 59 (by ref)
45 | ::       %7 var to receive seconds, 46 | 2 digits, 00 to 59 (by ref)
47 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
48 | setlocal ENABLEEXTENSIONS
49 | set /a i=%1,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
50 | set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
51 | set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
52 | set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
53 | (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
54 | (if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
55 | if %ss% LSS 10 set ss=0%ss%
56 | endlocal&set %7=%ss%&set %6=%nn%&set %5=%hh%&^
57 | set %4=%dd%&set %3=%mm%&set %2=%yy%&goto :EOF
58 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
62 |

Parameters

63 |

%1 seconds used to create calendar date and time of day (by 64 | val)
65 | %2 var to receive year, 4 digits for all typical dates (by ref)
66 | %3 var to receive month, 2 digits, 01 to 12 (by ref)
67 | %4 var to receive day of month, 2 digits, 01 to 31 (by ref)
68 | %5 var to receive hours, 2 digits, 00 to 23 (by ref)
69 | %6 var to receive minutes, 2 digits, 00 to 59 (by ref)
70 | %7 var to receive seconds, 2 digits, 00 to 59 (by ref)

71 |

Return Values

72 |

See parameters above.

73 |

Example

74 |

This example uses REG.EXE 75 | v2.0 to extract the InstallDate value (which is stored as seconds elapsed 76 | since 1970-01-01 00:00:00) from the Windows Registry. This is then converted 77 | to a date and time. Note the second FOR loop avoids having to use a TAB 78 | character in the DELIMS statement of the first FOR loop.

79 |
80 |

echo off & setlocal ENABLEEXTENSIONS
81 | :: requires REG.EXE Version 2.0 or later
82 | set key="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
83 | set val="InstallDate"
84 | for /f "delims=" %%a in ('reg query %key% /v %val%^|find %val%') 85 | do (
86 |   for %%b in (%%a) do set secs=%%b
87 | )
88 | call :SecsToDate %secs% yy mm dd hh nn ss
89 | echo/Windows installation date is: %yy%-%mm%-%dd% %hh%:%nn%:%ss%
90 | goto :EOF

91 |
92 |

Remarks

93 |

Use in conjunction with DateToSecs to perform 94 | date arithmetic with a resolution of one second.

95 |

Range is from 0 to 2147483647 or (2^31)-1 seconds which gives a date 96 | range from 1970-01-01 00:00:00 to 2038-01-19 03:14:07.

97 |

See Also

98 |

DateToSecs

99 |
102 |

 

103 | 104 | 105 | -------------------------------------------------------------------------------- /01_Introduction/3_Batch Functions Explained.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Batch Functions Explained 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 255 | 256 |
17 |

Batch Functions Explained

18 |

Passing Arguments

19 |

Most high-level programming languages allow arguments to be passed to 20 | functions 'by value' and 'by reference'. Passing by value means that 21 | the function receives a copy of the arguments value. Any changes made 22 | to this copy by the function do no affect the original variable.

23 |

When a variable is passed by reference, the function 24 | receives a pointer to the original variable, and loosely speaking, changes 25 | made to this pointer by the function do affect the original variable.

26 |

In the batch programming language variables 27 | are passed by reference. Unfortunately the pointers to the original 28 | variables are read only and consequently cannot be assigned values and 29 | therefore are unable to alter the variable which they point to.

30 |

The good news is that with a 31 | little ingenuity, the batch language can be coerced into passing variables 32 | by value and by reference in a similar way to high-level languages 33 | such as C and Visual Basic.

34 |

By Value

35 |

Below is a very simple example showing how to 36 | pass an argument by value and how that value is then read by the function.

37 |
38 |

1. @echo off & setlocal ENABLEEXTENSIONS
39 | 2. set str=Hello
40 | 3. call :demoA %str%
41 | 4. goto :EOF
42 | 5.
43 | 6. :demoA
44 | 7. echo/%1
45 | 8. goto :EOF

46 |
47 |

In line 3, %str% is expanded to Hello, 48 | and then the memory location of where Hello is 49 | actually stored is assigned to %1. Line 50 | 7 then references %1 which is then expanded 51 | to Hello.

52 |

If two or more arguments were being passed, they 53 | could be referenced using %2, %3 and 54 | so on, all the way up to %9. If more 55 | than nine variables were being passed, then to reference the tenth, 56 | the shift command 57 | would have to be used.

58 |

shift effectively 59 | increments all the pointers. Before 60 | shift is used, %0 points 61 | to the name of the function or routine and %1 points 62 | to the first argument. After shift is 63 | used, %0 points to what %1 used 64 | to point to, %1 points 65 | to what %2 used to point to and so on. 66 | The tenth argument can now be referenced using %9.

67 |

By Reference

68 |

Here is a slighty different version of the example above. Instead 69 | of passing the value Hello to the function, 70 | the name of a variable that contains Hello is 71 | passed instead.

72 |
73 |

1. @echo off & setlocal ENABLEEXTENSIONS
74 | 2. set str=Hello
75 | 3. call :demoB str
76 | 4. goto :EOF
77 | 5.
78 | 6. :demoB
79 | 7. call echo/%%%1%%
80 | 8. goto :EOF

81 |
82 |

Inside the function demoB, %1 expands 83 | to str. To read the value contained in str, 84 | it is necessary to enclose 85 | %1 with double percents and instruct the 86 | command interpreter to perform another expansion. This is most easily 87 | accomplished by using the CALL command 88 | as shown in line 7. For a detailed explanation of variable expansion, 89 | see the thread in alt.msdos.batch.nt titled "Variable 90 | expansion (W2KSP2)".

91 |

Why would we want to pass arguments like this? Well now 92 | that the function knows the name of a variable, it can assign 93 | it a value and thereby the function can now return values.

94 |

Returning Values

95 |

So far the contrived examples have only served to illustrate 96 | the points being made. This next example demonstrates how a function 97 | can return a value and is typical of the functions in this library. The 98 | methods used guarantee this function can be inserted, as it stands, into 99 | any batch file without causing any side effects whatsoever.

100 |

The function below is called Area and 101 | it simply calculates the product of width and height and then returns 102 | the result. Three arguments are required, the first two should be passed 103 | by value, the third by reference.

104 |

[It is a convention of this library to 105 | document a function's arguments by including an argument 106 | list immediately after the function name (as shown below on 107 | line 08). This is a convenient location as anything after the function 108 | name is ignored by the command interpreter. This list serves no other 109 | purpose than to act as a reminder of the type and number of arguments 110 | a function expects.]

111 |
112 |

01. @echo off & setlocal ENABLEEXTENSIONS
113 | 02. set x=2
114 | 03. set y=3
115 | 04. call :Area %x% %y% answer
116 | 05. echo/The area is: %answer%
117 | 06. goto :EOF
118 | 07. 
119 | 08. :Area %width% %height% result
120 | 09. setlocal
121 | 10. set /a res=%1*%2
122 | 11. endlocal & set "%3=%res%"
123 | 12. goto :EOF

124 |
125 |

Line 04 calls the Area function passing 126 | three arguments (it could equally have read call :Area 2 3 answer). 127 | The first command of the function (line 09) is setlocal. 128 | This command creates a copy of the current environment and this copy 129 | then becomes the 130 | current environment. Line 10 calculates the product of arguments %1 and %2 and 131 | saves the result in the variable called res.

132 |

The next line consists of two commands seperated by an ampersand (&). 133 | This causes the command interpreter to expand both commands and then 134 | execute them one after the other. So line 11, after it has been expanded 135 | equates to endlocal & set "answer=6". 136 | Now the endlocal command is executed, which 137 | deletes the copy of the environment created by the most recent setlocal command, 138 | and restores the previous environent. Then set "answer=6" is 139 | executed, and finally on line 12 goto :EOF exits 140 | the function and execution continues at line 05. The net result is that 141 | the Area function has returned a value.

142 |

If on line 11, the command 143 | set "%3=%res%", had been 144 | expanded after endlocal had been 145 | executed, then instead of expanding to set "answer=6" it 146 | probably would have been set "answer=" as the 147 | res variable was deleted by the endlocal command.

148 |

By Value or Reference

149 |

The rule of thumb is to pass arguments by 150 | value unless you need to modify the original values, in which case pass 151 | by reference. The Area function above demonstrates 152 | this rule as the first and second arguments (width and height) are passed 153 | by value, but in order to assign the result of the calculation to the 154 | answer variable, it was passed to the function 155 | by reference.

156 |

Sometimes it's necessary to pass arguments by reference even though 157 | read-only access is required. For example, when passing two or more unquoted 158 | arguments where at least one of them contains whitespace, the function 159 | would not be able to determine which argument was which if they had been 160 | passed by value. Calling by reference can also be used to pass arguments 161 | containing so-called 'poison' characters (such as " ! | < > ^ " & %).

Protecting 162 | out of Scope Variables

163 |

In order to create truely reusable functions, you must ensure that 164 | they do not unintentionally modify variables outside of their own scope. 165 | This is accomplished by enclosing your function's main routine with the 166 | setlocal and endlocal statements as shown in the example above.

167 |

Unfortunately things get a little more complicated when two or more 168 | arguments are passed by reference and those arguments need to 169 | be read by your function (as opposed to just being assigned values). 170 | This is best illustrated by the Swap function 171 | shown below.

172 |

Protecting Arguments

173 |

When passing two or more arguments by reference where those arguments 174 | need to be read, care must 175 | be taken not to destroy any of the arguments before they have all been 176 | read. Consider this example below.

177 |
178 |

01. @echo off & setlocal ENABLEEXTENSIONS
179 | 02. set a=one
180 | 03. set b=two
181 | 04. echo/Before call  :swap a b [%a% %b%]
182 | 05. call :Swap a b
183 | 06. echo/After call 1 :swap a b [%a% %b%]
184 | 07. call :Swap b a
185 | 08. echo/After call 2 :swap b a [%a% %b%]
186 | 09.
187 | 10. goto :EOF
188 | 11.
189 | 12. :Swap
190 | 13. setlocal
191 | 14. call set a=%%%1%%
192 | 15. call set b=%%%2%%
193 | 16. endlocal & set "%1=%b%" & set "%2=%a%" & goto 194 | :EOF

195 |
196 |
197 |

The above example displays:-

198 |
199 |

Before call  :swap a b [one two]
200 | After call 1 :swap a b [two one]
201 | After call 2 :swap b a [one one]

202 |
203 |

This function is supposed to swap the contents of two variables. Notice 204 | it succeed the first time, but failed on the second. This was caused 205 | by variable names outside of the function clashing with duplicate names 206 | inside the function. Specifically, this is what went wrong:-

207 |
    208 |
  • Line 07 called Swap, the first argument 209 | is b, the second a.
  • 210 |
  • Line 14 is expanded twice because of the call command, 211 | after the second expansion, the line equates to set a=one, 212 | and once this command is executed, a is 213 | overwritten.
  • 214 |
  • Line 15 is also expanded twice and equates 215 | to set b=one, in other 216 | words both a and b are 217 | now the one
  • 218 |

The usual workaround for this problem is to use variable names that are 219 | unique to your function by preceeding all function variables with the 220 | function name separated by a period. However, if using that approach, 221 | it's actually only necessary to use unique names for vulnerable variables. 222 | Rewriting lines 14-16 of the Area function as shown below would prevent 223 | any arguments being overwritten before they had been saved.

224 |
225 |

14. call set Swap.a=%%%1%%
226 | 15. call set b=%%%2%%
227 | 16. endlocal&set %1=%b%&set %2=%Swap.a%&goto :EOF

228 |
229 |

230 |

This method has several drawbacks. Firstly, you must never use a period 231 | in any variable name outside of your functions. Secondly, if the function 232 | doesn't have a really short name and reads a number of variables by reference, 233 | the number of lines of code in the function can double if you want to 234 | avoid 'long' lines of code.

235 |

An alternative method is to read the variables with the FOR /F command 236 | using a delimiter character not present in any of the argument values. 237 | Using this method the Swap function could be rewritten as follows:-

238 |
239 |

12. :Swap
240 | 13. setlocal ENABLEEXTENSIONS
241 | 14. for /f "tokens=1-2 delims=¬" %%a in ('echo/%%%1%%¬%%%2%%') 242 | do (
243 | 15.   set a=%%a&set b=%%b
244 | 16. )
245 | 17. endlocal&set %1=%b%&set %2=%a%&goto :EOF

246 |
247 |

The delimiter character is a '¬' (ASCII 172, which looks like a back-to-front 248 | 'L' on its side). It was chosen because it's rarely used and it appears 249 | on a UK keyboard (above the TAB key). If you don't have this key, you 250 | can type it by holding down the Alt key and typing 172 on the numeric 251 | keypad. The only drawback of this method being your variables must never 252 | contain the delimiter character otherwise you may obtain incorrect results.

253 |

This issue was covered in alt.msdos.batch.nt in a thread titled "Function 254 | parameters".

257 | 258 | 259 | --------------------------------------------------------------------------------