Mittwoch, 28. November 2007

BATCH_ Create libraries with valuable Batch subroutines

If you write some Batch subroutines that make common stuff like calculating the string length or testing for a number, you don't want to copy&paste these into each script that needs these. Be cool, there is a very easy solution how you can bundle all of your helper subroutines into one (or even more) files. Just copy them into an empty file, and add the following code at the very first line:
SHIFT /1 & GOTO %~1

That jumps to the label you provided as the first parameter after shifting all remaining parameters one down leaving %0 as it is.

Let's say you saved that file as C:\Windows\lib.cmd. Now you can call any subroutine from another script by issuing
CALL %SystemRoot%\lib.cmd :MY_SUBROUTINE param1 param2 ...

Remember to enable extensions and delayed variable expansion in each of your subroutines when needed - you cannot depend on the "frame" script's setting any more!
:MY_SUBROUTINE
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
... routine here ...
ENDLOCAL
GOTO :EOF

BATCH_ Test a String for Number

Testing a String for a number that can only contain the ciphers 0 to 9 and optionally a prefixed - character can be done with the following subroutine.
:: Checks if the first parameter is a number, that is it only contains the
:: characters 0 to 9 and additionally the minus as the first character.
:: PARAM
:: 1 string to test for number
:: RETURN String "true" if string is a valid number, "false" otherwise
:IS_NUMBER
SETLOCAL
FOR /F "tokens=*" %%A IN ('SET /A %~1 2^>^&1') DO (
    IF "%~1" == "%%A" (
       SET RESULT=true
    ) ELSE (
       SET RESULT=false
    )
)
ENDLOCAL & SET RESULT=%RESULT%
GOTO :EOF
This tests the input string using the SET /A arithmetic extension.
However this test two limits I can think of:
  • As SET /A echoes the error Missing operand. on invalid input, a test against that very string will return true.
  • A number with leading zeros like 007 will not be recognized as a number.
If you cannot live with these limitations, you have to replace this relatively fast solution with two loops doing something like this:
FOR EACH CHARACTER (
    FOR %%A IN (0-9) (
        IF (%%A == CHAR) (
            GOTO :END_INNER
        )
    )
    GOTO:EOF & FAIL
    :END_INNER
)

BATCH_ Redirect special characters into file

ECHOing special characters like german umlauts in Batch or creating files with such in their name does not cause any trouble. However, when redirecting into files
DIR /B *.* > list.txt
these files will be created using the same codepage (or encoding) like the command line. However, as that codepage differs in most cases from the encoding that is used to read that file in external programs or editors, special characters are not being displayed correctly.

Assuming that you are using a standard format for your textfiles, you can switch the encoding before redirection.
FOR /F "tokens=1* delims:" %%A IN ('CHCP') DO (
    SET /A CODEPAGE = %%B 2> NUL
)
CHCP 1252 > NUL
DIR /B *.* > list.txt
CHCP %CODEPAGE% > NUL
CHCP gives you the current codepage, however the output is localized (e.g. Active code page: 850 in English or Aktive Codepage: 850. in German). Therefore the tokens are delimited by the colon, which should work for most languages, I guess.
However, as you can see, the German output is ended with a dot, so that one is removed by converting the token to a number using SET /A. As that expression will work but give you a warning message about a missing operator (caused by the dot), redirection of error messages to NUL is added.

The rest ist simple: switching to standard text codepage 1252, redirecting and switching back to the original codepage.

Reflog

Informationstechnische Howtos, Hinweise und Merkwürdiges

Batchlib v1.0 2008-03-29

Aktuelle Beiträge

HOWTO_ O2 DSL Surf &...
Der O2 DSL Surf & Phone-Router ist für die alleinige...
cypressor - 12. Feb, 19:57
Uptweak Windows XP Home...
There are a lot of annoying limitations in Windows...
cypressor - 9. Okt, 19:30
BATCHLIB_ Batchlib package...
Download Batchlib package v1.0 (5 KB zip file) What...
cypressor - 29. Mär, 19:10
BATCHLIB_ Batchlib library...
The batchlib library string.cmd is part of the batchlib...
cypressor - 29. Mär, 18:10

Homepage Ticker

Links

Status

Online seit 6579 Tagen
Zuletzt aktualisiert: 28. Jun, 11:32
RSS XML 1.0 Button-Get-Firefox

batch
batchlib
howto
tech
video
Profil
Abmelden
Weblog abonnieren