|René Nyffenegger's collection of things on the web|
René Nyffenegger on Oracle - Most wanted - Feedback
Unknown dos box features
There are a few dos box features that I was not aware of until recently. Here are some of them.
I thought SET can only be used to set variables. But it is also possible to make some mathematical calculations with it as long as the operands are integers. SET must be invoked with the /A flag to compute an expression:
C:\>set /a 25+17 42 C:\>set /a 10+5*5 35 C:\>set /a (10+5)*5 75 C:\>set /a (995+5)/(7+3) 100
If the /P flag is specified, an environment variable is set to what is entered. In the following example, I am prompted to Enter your name to which I answer with Rene. Rene is then assigned to the variable username.
C:\>set /P username="Enter your name " Enter your name Rene C:\>echo %username% Rene
Environment variable substitution
Replacing strings with others: Here I replace (every occurence) of scream with want:
C:\>set Foo=I scream, you scream Ice cream C:\>echo %Foo:scream=want% I want, you want Ice cream
Substrings. The following example retrieves the substring of length 3 that starts at position 6:
C:\>set Bar=01234567890 C:\>echo %Bar:~5,3% 567
If /F is given to FOR, it is possible to read the output of another program
The following example prints only directories. The tokens=3,4 specifies that I am interested in the third and fourth (by whitespace delimted) tokens. This implicitely also sets the variable %b. If the third token (%a) is equal to <DIR> then it must be a directory and the fourth token (%b) is its name.
for /f "tokens=3,4" %a in ('dir') do @if "%a" == "<DIR>" @echo %b
With findstr one can (even recursively /s) find patterns in files. The following example finds all html files that contain the word boombleba in the current directory and all its subdirectories. The /i flag specifies that boombleba should be searched in a case insensitive manner.
C:> findstr /s /i boombleba *.html
I found the following example a bit disturbing.
C:> findstr /s /i "foo bar" *.baz
I expected it (because I failed to read the documentation) to return all *.baz files that contained the word foo followed by a space followed by a bar. However, it returned all *.baz files that contained either foo or bar. The /c: flag helps:
C:> findstr /s /i /c:"foo bar" *.baz