Batch files: Snippets 1: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 14: Line 14:
|}
|}
This is not really batch file specific more a general repository to dump things that I tend to forget. You may find these of use and worth a browse!
This is not really batch file specific more a general repository to dump things that I tend to forget. You may find these of use and worth a browse!
== CALL ==
CALL is used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message.


== General DOS ==
== General DOS ==
Line 65: Line 68:


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
== Current Working Directory ==
== Current Working Directory ==
Uniform Server will run on any drive from any folder, simply double-click on the relevant batch file. If you call these batch files from another drive the chances it will not work. The problem is most likely due to the setting for the current drive. This is easily resolved using a very powerful batch command '''%~''' I use this in conjunction with two other commands, before I expand on this do you really need to use it!
Uniform Server will run on any drive from any folder, simply double-click on the relevant batch file. If you call these batch files from another drive the chances it will not work. The problem is most likely due to the setting for the current drive. This is easily resolved using a very powerful batch command '''%~''' I use this in conjunction with two other commands, before I expand on this do you really need to use it!


=== Environment variable manipulation ===
General format:
%VAR:str1=str2%
Expands VAR ('''any environment variable'''), substituting each occurrence of "str1" with "str2".
%VAR:str1=%
If "str2" is empty all occurrences of "str1" are deleted from the expanded VAR.
'''Substrings'''
%VAR:~position''','''number of characters to extract%
Note: First character position starts at zero
If VAR has a value of abcdefgh then:<br>
'''%VAR:~2,4%''' would produce defg<br>
'''%VAR:~0,1%''' would produce a
Note 1: If the number of characters to extract is not specified it defaults to the remainder after position.
Note 2: If either parameter is negative, position used is the length of the environment variable value added to the number of characters to extract.
'''%VAR:~-5%'''  This would extract the last 5 characters of VAR.
'''%VAR:~0,-2%''' This extract all but the last 2 characters of VAR.
'''''[[#top | Top]]'''''
=== Working directory ===
=== Working directory ===
Suppose you have located (unzipped) Uniform Server on drive "'''D'''" in folder '''progz\uni1'''.
Suppose you have located (unzipped) Uniform Server on drive "'''D'''" in folder '''progz\uni1'''.
Line 129: Line 163:


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
=== Current drive letter 1===
=== Current drive letter 1===
To pick up the current drive letter (where the batch file resides) use this:
To pick up the current drive letter (where the batch file resides) use:
{|
|-
|valign="top"|
{| cellpadding="4" cellspacing="1" style="background:#000000;"
{| cellpadding="4" cellspacing="1" style="background:#000000;"
|- style="background:#e8e8e8;"
|- style="background:#e8e8e8;"
Line 138: Line 176:
<pre style="border:none">
<pre style="border:none">
pushd %~dp0
pushd %~dp0
set current_drive=%~dp0
set current_drive=%~d0
....
....
batch file code
batch file code
Line 145: Line 183:
</pre>  
</pre>  
|}
|}
 
'''''Note'':''' Drive includes colon e.g '''W:'''
|
&nbsp;
|valign="top"|
{| cellpadding="4" cellspacing="1" style="background:#000000;"
|- style="background:#e8e8e8;"
!How to obtain batch file drive letter
|- style="background:#f5f5f5;"
|
<pre style="border:none">
pushd %~dp0
set current_drive=%~d0
set current_drive=%current_drive:~0,1%
....
batch file code
popd
</pre>
|}
'''''Note'':''' Drive letter only e.g '''W'''
|}
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''


Line 297: Line 354:


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
== IF Else ==
'''IF'''
General form: IF '''{Statement} {If statement is TRUE, perform this action}'''<br>
Note: All on a single line
IF [NOT] ERRORLEVEL number command<br>
IF [NOT] string1==string2 command<br>
IF [NOT] EXIST filename command
{|cellpadding="4"  cellspacing="1" style="background:#777777;"
|-style="background:#f5f5f5;"
|NOT||Specifies that Windows 2000 / XP should carry out the command only if the condition is false.
|-style="background:#f5f5f5;"
|ERRORLEVEL&nbsp;number||Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
|-style="background:#f5f5f5;"
|string1==string2||Specifies a true condition if the specified text strings match.
|-style="background:#f5f5f5;"
|EXIST filename||Specifies a true condition if the specified filename exists.
|-style="background:#f5f5f5;"
|command||Specifies the command to carry out if the condition is met. Command can be followed by ELSE command which will execute the command after the ELSE keyword if the specified condition is FALSE
|}
'''Else:'''<br>
IF {Statement} ({If statement is TRUE, perform this action}) ELSE ({If statement is TALSE, perform this action})
'''''Examples'':'''
IF (%1)==() GOTO END
When parameter %1 is empty, the test evaluates to TRUE
if (%first%)==(%second%) goto  :next<br>
When parameter %first% and %second% are equaly jump to next
if errorlevel 3 goto :PAUSE<br>
if errorlevel 2 goto :PAUSE<br>
if not errorlevel 1 goto :STARTED
Note: Error levels are processed in descending order. Error level 1 would capture 2 and 3.
'''''[[#top | Top]]'''''
== SET ==
SET [variable=[string]]
* '''variable''' Specifies the environment-variable name.
* '''string''' Specifies a series of characters to assign to the variable.
SET /A expression<br>
SET /P variable=[promptString]
The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.
'''''Example'':'''
=== Increment a variable ===
set /a cnt=0<br>
set /a cnt+=1
'''''[[#top | Top]]'''''
== SUBST command ==
== SUBST command ==
Uniform Server puts this command to good use, in actual fact the architecture revolves around it. The bottom line it's what makes UniServer portable and quite a nice piece of lateral thinking by the designer.
Uniform Server puts this command to good use, in actual fact the architecture revolves around it. The bottom line it's what makes UniServer portable and quite a nice piece of lateral thinking by the designer.
Line 579: Line 697:


{|  
{|  
| [[Image:uc_small_logo.gif]] || [[User:Ric|Ric]]  
| [[Image:uc_small_logo.gif]] || [[User:Ric|Ric]]
|}
|}


[[Category: UniCenter]]
[[Category: UniCenter]]