├── 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 17 |The Sleep function suspends execution 18 | of the current batch file for a specified interval. 19 |
Parameters 40 |%1 Number of seconds to wait for (by val) 41 |Return Values 42 |See parameters above. 43 |Example 44 |45 |49 | Remarks 50 |This function is based on the idea 51 | by Clay Calvert. 52 |See Also 53 | 54 |55 | |
56 |
GetDG 17 |The GetDG function returns the default 18 | gateway. 19 |
Parameters 44 |%1 var to receive default gateway (by ref) 45 |Return Values 46 |See parameters above. 47 |Example 48 |49 |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 | 60 |61 | |
62 |
GetIP 17 |The GetIP function returns the IP address 18 | of the primary adapter. 19 |
Parameters 44 |%1 var to receive IP Address (by ref) 45 |Return Values 46 |See parameters above. 47 |Example 48 |49 |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 | 59 |60 | |
61 |
MonthName 17 |The MonthName function returns the name 18 | of month from the month number. 19 |
Parameters 45 |%1 month number convert to name of month, 1 or 01 to 12 (by
46 | val) Return Values 49 |See parameters above. 50 |Example 51 |52 |58 | Remarks 59 |The returned month name is the full name of the month with the first 60 | letter capitalised. 61 |See Also 62 | 63 |64 | |
65 |
68 | 69 | 70 | -------------------------------------------------------------------------------- /File and Directory Functions/IsInPath.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
IsInPath 17 |The IsInPath function determines if 18 | the specified files are in the current directory or in a directory listed 19 | in the path statement. 20 |
Parameters 42 |%1 List of files separated by whitespace (by val) 43 |Return Values 44 |See parameters above. 45 |Example 46 |47 |63 | Remarks 64 |A space, a tab and a semi-colon are considered to be whitespace. 65 |See Also 66 |None. 67 |68 | |
69 |
IsDirEmpty 17 |The IsDirEmpty function determines if 18 | the specified directory is empty. 19 |
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 |58 | Remarks 59 |None. 60 |See Also 61 | 62 |63 | |
64 |
DayName 17 |The DayName function returns the name 18 | of the day of week from the day of week number. 19 |
Parameters 43 |%1 day number to convert to name of day of week, 1 to 7 (by
44 | val) 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 |53 | Example 54 |55 |60 | Remarks 61 |Day 1 is Monday, day 7 is Sunday. 62 |See Also 63 |DayNumber, DateToWeek, 64 | WeekToDate 65 |
|
66 |
69 | 70 | 71 | -------------------------------------------------------------------------------- /System Functions/IsRunning.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
IsRunning 17 |The IsRunning function determines if 18 | the specified service is running. 19 |
Parameters 40 |%1 Display name of the service to check 41 | (by val) 42 |Return Values 43 |See parameters above. 44 |Example 45 |46 |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 | |
64 |
MonthNumber 17 |The MonthNumber function returns the 18 | number of a number from a month name. 19 |
Parameters 49 |%1 month name to convert to month number, Jan to Dec (by val) 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 |62 | Remarks 63 |None. 64 |See Also 65 | 66 |67 | |
68 |
71 | 72 | 73 | -------------------------------------------------------------------------------- /01_Introduction/1_Batch Function Overview.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
| 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 |43 |
|
77 |
DayNumber 17 |The DayNumber function returns the number 18 | of the day of week from the day of week name. 19 |
Parameters 45 |%1 Weekday name to convert to day of week number (by val) 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 |59 | Remarks 60 |Monday is day 1 and Sunday day 7. 61 |See Also 62 |DayName, DateToWeek, 63 | WeekToDate 64 |65 | |
66 |
69 | 70 | 71 | -------------------------------------------------------------------------------- /System Functions/GetOS.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
GetOS 17 |The GetOS function returns the operating 18 | system version. 19 |
Parameters 41 |%1 var to receive O/S version (by ref) 42 |Return Values 43 |
Example 66 |67 |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 | |
80 |
DaysToDate 17 |The DaysToDate function converts the 18 | number of days elapsed since 1970-01-01 to a calendar date. 19 |
Parameters 50 |%1 MJD used to create calendar date (by val) Return Values 56 |See parameters above. 57 |Example 58 |59 |64 | Remarks 65 |Use in conjunction with DateToDays to perform 66 | date arithmetic with a resolution of one day. 67 |See Also 68 | 69 | |
70 |
73 | 74 | 75 | -------------------------------------------------------------------------------- /Network Functions/GetSM.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
GetSM 17 |The GetSM function returns the subnet 18 | mask of the primary adapter. 19 |
Parameters 53 |%1 var to receive subnet mask (by ref) 54 |Return Values 55 |See parameters above. 56 |Example 57 |58 |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 | |
73 |
GetNA 17 |The GetNA function returns the network 18 | address of the primary adapter. 19 |
Parameters 53 |%1 var to receive network address (by ref) 54 |Return Values 55 |See parameters above. 56 |Example 57 |58 |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 | |
73 |
DateToDOW 17 |The DateToDOW function returns the day 18 | of week number for a given calendar date.. 19 |
Parameters 50 |%1 year component to be converted, 2 or 4 digits (by val) Return Values 55 |See parameters above. 56 |Example 57 |58 |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 | |
73 |
76 | 77 | 78 | -------------------------------------------------------------------------------- /Network Functions/GetMAC.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
GetMAC 17 |The GetMAC function returns the MAC 18 | address of the primary adapter. 19 |
Parameters 57 |%1 var to receive MAC address (by ref) 58 |Return Values 59 |See parameters above. 60 |Example 61 |62 |67 | Remarks 68 |To remove the dashes from the result use variable substitution. For example:- 69 |70 |75 | See Also 76 |77 |
|
78 |
MJDToDate 17 |The MJDToDate function converts a Modified 18 | Julian Day to a UTC date. 19 |
Parameters 52 | %1 MJD used to create calendar date (by val) Return Values 57 |See parameters above. 58 |Example 59 |60 |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 | 71 | |
72 |
75 | 76 | 77 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToMJD.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
DateToMJD 17 |The DateToMJD function converts a UTC 18 | date to a Modified Julian Day. 19 |
Parameters 51 |%1 year component used to create MJD, 2 or 4 digits (by val) Return Values 56 |See parameters above. 57 |Example 58 |59 |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 | 72 | |
73 |
76 | 77 | 78 | -------------------------------------------------------------------------------- /Date and Time Functions/OrdinalToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
OrdinalToDate 17 |The OrdinalToDate function returns a 18 | calendar date from an ISO 8601 19 | Ordinal date. 20 |
Parameters 53 |%1 year component to be converted, 4 digits (by val) Return Values 59 |See parameters above. 60 |Example 61 |62 |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 | 71 |72 | |
73 |
76 | 77 | 78 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToOrdinal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
DateToOrdinal 22 |The DateToOrdinal function returns an 23 | ordinal date from a calendar date as described by ISO 24 | 8601. 25 |
Parameters 60 |%1 year component to be converted, 2 or 4 digits (by val) Return Values 66 |See parameters above. 67 |Example 68 |69 |75 | Remarks 76 |None 77 |See Also 78 | 79 |80 | |
81 |
84 | 85 | 86 | -------------------------------------------------------------------------------- /01_Introduction/2_Using the Functions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
| 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 |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 |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 |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 |78 | |
79 |
GetDirStats 17 |The GetDirStats function returns the 18 | number of files, subdirectories and total size of a specified directory. 19 |
Parameters 52 |%1 Name of directory, absolute or relative (by val) 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 |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 | 74 |75 | |
76 |
FileSizeComp 17 |The FileSizeComp function compares the 18 | size of a specified file. 19 |
Parameters 55 |%1 File to compare (by val) Return Values 60 |See parameters above. 61 |Example 62 |63 |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 |79 | See Also 80 |81 | |
82 |
DateToDays 17 |The DateToDays function converts a calendar 18 | date to the number of days elapsed since 1970-01-01. 19 |
Parameters 52 |%1 year component used to create days, 2 or 4 digits (by val) Return Values 58 |See parameters above. 59 |Example 60 |61 |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. See Also 78 | 79 | |
80 |
83 | 84 | 85 | -------------------------------------------------------------------------------- /Date and Time Functions/WeekToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
WeekToDate 17 |The WeekToDate function returns a calendar 18 | date from an ISO 8601 Week 19 | date. 20 |
Parameters 62 |%1 year to convert, 2 or 4 digits (by val) Return Values 69 |See parameters above. 70 |Example 71 |72 |77 | Remarks 78 |None. 79 |See Also 80 | 81 |82 | |
83 |
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 17 |The DateToWeek function returns an ISO 18 | 8601 Week date from a calendar date. 19 |
Parameters 64 |%1 year component to be converted, 2 or 4 digits (by val) Return Values 71 |See parameters above. 72 |Example 73 |74 |80 | Remarks 81 |None. 82 |See Also 83 | 84 |85 | |
86 |
89 | 90 | 91 | -------------------------------------------------------------------------------- /Date and Time Functions/MinsToDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
MinsToDate 17 |The MinsToDate function converts the 18 | number of minutes elapsed since 1970-01-01 00:00 to a calendar date and 19 | time. 20 |
Parameters 59 |%1 minutes used to create calendar date and time of day (by
60 | val) Return Values 67 |See parameters above. 68 |Example 69 |70 |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 | 86 | |
87 |
90 | 91 | 92 | -------------------------------------------------------------------------------- /Date and Time Functions/GetTime.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|
40 |
41 | GetTime 42 | 43 | 44 | 45 | 46 |The GetTime function obtains the local 47 | system time. 48 | 49 | 50 | 51 | 52 |
Parameters 141 | 142 | 143 | 144 | 145 |%1 Var to receive hours, 2 digits, 00 to 23 (by ref) Return Values 160 | 161 | 162 | 163 | 164 |See parameters above. 165 | 166 | 167 | 168 | 169 |Example 170 | 171 | 172 | 173 | 174 |175 | 176 | 177 |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 | 213 | 214 | 215 | |
216 |
217 |
218 |
227 | 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /Date and Time Functions/DateToMins.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
DateToMins 16 |The DateToMins function converts a calendar 17 | date to the number of minutes elapsed since 1970-01-01 00:00. 18 |
Parameters 62 |%1 years to convert, 2 or 4 digit (by val) Return Values 70 |See parameters above. 71 |Example 72 |73 |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 | 87 | |
88 |
91 | -------------------------------------------------------------------------------- /Date and Time Functions/0 Date and Time Functions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
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 |
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 |
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 |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 |
References 97 | HEASARC
98 | online date converter |
105 |
108 | 109 | 110 | -------------------------------------------------------------------------------- /System Functions/Uptime.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
Uptime 17 |The Uptime function returns the elapsed 18 | days, hours, minutes and seconds since the system booted. 19 |
Parameters 71 |%1 var to receive number of days of uptime (by ref) Return Values 76 |See parameters above. 77 |Example 78 |79 |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 | |
93 |
|
18 | Timer 19 |The Timer 20 | function returns the number of elapsed seconds since the function was 21 | last called and first called. 22 |
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 |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 | 126 |127 | |
128 |
DateToSecs 17 |The DateToSecs function converts a calendar 18 | date to the number of seconds elapsed since 1970-01-01 00:00:00. 19 |
Parameters 69 |%1 year to convert, 2 or 4 digit (by val) Return Values 78 |See parameters above. 79 |Example 80 |81 |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. 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 | 98 | |
99 |
102 | 103 | 104 | -------------------------------------------------------------------------------- /Date and Time Functions/GetDate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
|
27 | GetDate 28 | 29 | 30 |The GetDate function obtains the local 31 | system date. 32 | 33 | 34 |
Parameters 93 | 94 | 95 |%1 variable to receive year, 4 digits (by ref) Return Values 104 | 105 | 106 |See parameters above. 107 | 108 | 109 |Example 110 | 111 | 112 |113 | 114 |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 |
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 |
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 | 151 | 152 | 153 |154 | 155 | |
156 |
157 |
163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /Batch Howto's/Reading Files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
Reading Files 10 |This page shows how to read specific lines from a 11 | text file. There are many ways to have the for /f command read the input 12 | file, for instance:- 13 |14 |20 | However, only the last method (using the more command) 21 | will give consistent results across Windows NT, 2000, XP and 2003. The 22 | first method does not recognise unicode files. Also, the usebackq switch 23 | must be used if the input filename contains spaces. The second method, 24 | using the type command, also fails to recognise 25 | unicode files on Windows 2000, XP and 2003 if the input file does not 26 | begin with a bit order mark (BOM). 27 |In all the examples, assume the contents of of the 28 | file numbers.txt to 29 | be:- 30 | Displaying the first line 41 |This example prints one. 42 |49 | Displaying the first X lines 50 |This example prints one, two and three. 51 |52 | Displaying the last line 65 |This example prints ten. 66 |67 | Displaying the last X lines 71 |This example prints nine and ten. 72 |73 |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 | Displaying the Nth line plus X number of lines 99 |This example prints five and six. 100 |101 | |
115 |
SecsToDate 17 |The SecsToDate function converts the 18 | number of seconds elapsed since 1970-01-01 00:00:00 to a calendar date 19 | and time. 20 |
Parameters 63 |%1 seconds used to create calendar date and time of day (by
64 | val) 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 |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 | 99 | |
100 |
103 | 104 | 105 | -------------------------------------------------------------------------------- /01_Introduction/3_Batch Functions Explained.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
|
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 |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 |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 |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 |197 |178 |196 | The above example displays:- 198 |199 |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 |
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 |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 |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". |
255 |