Posts Tagged ‘dos prompt’

I’ve talked a bit previously about alternative windows shells. However, sometimes they are not available and I have no wish to learn powershell so I use the old dos command prompt.

I set up a batch file to configure the path which will be called from Console2. And I use my usual trick of removing unwanted paths and duplicates by calling perl. As dos doesn’t have the nice backtick syntax from the unix shells how do you capture the output from an external command? It turns out you need the dos for command.

c:\home\jared>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]


FOR /F ["options"] %variable IN ('command') DO command [command-parameters]


As the perl command uses quotes itself, you need to pass the usebackq option which allows you to use backticks to quote the command and has the added advantage of looking slightly more like unix shell :)

The way that for works is it splits the output based on a delimiter and you have to specify a variable for each result. The default delimiter is space and as many windows paths contain spaces we would have to provide a large number of variables to retrieve all the results. The alternative is to set delims to be blank which allows us to capture all the results in one variable.

@echo off

set PERL=c:\strawberry\perl\bin\perl

for /f "usebackq delims=" %%x in (`%PERL% -e "print join(q{;}, grep { lc($_) !~ /strawberry|msys|mingw|cygwin/ } split /;/, $ENV{PATH})"`) do set PATH=%%x

set STRAWBERRY=c:\strawberry\perl\bin;c:\strawberry\c\bin
set PATH=%STRAWBERRY%;c:\msys\1.0\bin;%PATH%
cd %HOME%

@echo on

Read Full Post »


Get every new post delivered to your Inbox.