Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Command substitution on windows cmd.exe

2,751 views
Skip to first unread message

Ingo Nolden

unread,
Feb 23, 2009, 9:34:30 AM2/23/09
to

Hello,

I was pointed here from another group. So please be gentle if my
question is not completely appriopriate.

I woulc like to know wether the windows commandline/batch language
supports command substitution.


I mean I want the command and its arguments to be replaced by the
std-out of that command. What I have seen, this works in a special
scenario in a for loop. So, technically it shouold be possible. But, I
could not make it work in a more general case.

This is what I found:

for /f %t in ('CD') do @set XX=%t ; store output of CD command

This is what I want to do:

MYVAR=$(dir)

%MYVAR% should expand to the directory content.

Of course I don't want a workaround that suggests to write to a file and
call that file as a batch file. I just would like to know wether and
how it is possible with standard commands.

thanks
Ingo

foxidrive

unread,
Feb 23, 2009, 9:43:24 AM2/23/09
to
On Mon, 23 Feb 2009 15:34:30 +0100, Ingo Nolden <nuttyg...@yahoo.com>
wrote:

>This is what I want to do:
>
>MYVAR=$(dir)
>
>%MYVAR% should expand to the directory content.

set myvar="%cd%"

or without the double quotes embedded:

set "myvar=%cd%"


T Lavedas

unread,
Feb 23, 2009, 10:23:54 AM2/23/09
to
On Feb 23, 9:43 am, foxidrive <got...@woohoo.invalid> wrote:
> On Mon, 23 Feb 2009 15:34:30 +0100, Ingo Nolden <nuttygraph...@yahoo.com>

> wrote:
>
> >This is what I want to do:
>
> >MYVAR=$(dir)
>
> >%MYVAR% should expand to the directory content.
>
> set myvar="%cd%"
>
> or without the double quotes embedded:
>
> set "myvar=%cd%"

foxidrive,

I don't read the request the same way as you do. I believe the OP is
asking for a means of storing the entire output of a DIR command in an
environment variable. While this might be possible in some limited
sense, the command line limit (1024 characters, I believe) would seem
to get in the way for larger listings.

I was thinking a DOSKEY macro, might serve, but I couldn't quite bend
my thinking around what he has in mind to do with it. I say that
because a simple DIR by itself (nor parameters, not substitution)
answers the question as you have interpreted it. I think the OP needs
to explain the desired outcome some more before I (anyone?) can give a
truly cogent response.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/

billious

unread,
Feb 23, 2009, 10:52:11 AM2/23/09
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:ied5q4h0ti2c2vga2...@4ax.com...

I'd agree with Tom's analysis.

I believe we have the standard "solve my method" approach rather than "solve
the problem."

What good is an environment variable containing the contents of DIR - to say
nothing of thecomplexity of adding line-separatorsand limited size?

The question seems to be how to make a DIR listing APPEAR to be an
environment variable (for an unstated purpose) - and the "no file
generation" requirement appears to be to avoid the DOS-era approach of
writing a subsidiary batch.

OP needs to state what PROBLEM needs to be solved, not how to engineer data
to fit an approach to an unstated problem.

I'd suggest that the logical solution may well lay with a simple

FOR /F %%i in ( ' dir ... ' ) do

that is, literally process the output of a DIR command - but without more
details of WHAT is to be accomplished, we could spend a week guessing.

If OP really wants the DIR output in the environment, then possibly

FOR /F "tokens=1*delims=][" %%i in ( ' dir ^|find /v /n "" ' ) do set
Y%%i=%%j

may suit, setting environment variables Y1 .. Ynnn with the line-output from
DIR.

(Using Y since no manufacturer-defined environment variables start "Y")

Next question is how to USE these environment-variables, or possibly how to
process the variables established.

More info required!!


Matthias Tacke

unread,
Feb 23, 2009, 11:04:50 AM2/23/09
to
Ingo Nolden wrote:
...

> This is what I want to do:
>
> MYVAR=$(dir)
>
> %MYVAR% should expand to the directory content.
>
> Of course I don't want a workaround that suggests to write to a file and
> call that file as a batch file. I just would like to know wether and
> how it is possible with standard commands.
>

Since DOS/NT variables can normally have only a single line, this is not
possible.

If you want to have a unix/linux like shell try cygwin.

If you have to work on given PCs without third party tools, you'll have to
think different, For /f will be the solution.

If the use of third party tools is possible, you might try Frank P.
Westlake's Conset. <http://www.geocities.com/fp.westlake/nt/>

<Cite>
conset.zip For Windows NT; does everything SET does on Windows 2000,
including prompting for input. Also performs floating point math, sets
system and user variables, sets variables from registry values, sets
variables from a backquoted command string, adjusts console parameters, and
more.
</Cite>

Since intrinsic commands don't expect multi line vars, usage is limited.

--
HTH
Matthias

Message has been deleted

Ted Davis

unread,
Feb 23, 2009, 4:25:40 PM2/23/09
to
On Mon, 23 Feb 2009 07:23:54 -0800, T Lavedas wrote:

> I don't read the request the same way as you do. I believe the OP is
> asking for a means of storing the entire output of a DIR command in an
> environment variable. While this might be possible in some limited sense,
> the command line limit (1024 characters, I believe) would seem to get in
> the way for larger listings.

Not exactly. screen dump:

$ echo $(dir)
netcomics-0.14.1 r0040_03.pdf xgawk-3.1.4-2005-04-09.tar.gz netcomics-0.14.1.zip
r0040_03.png xpn_setup.exe r0040_01.pdf test.txt xpn_setup_noGTK.exe r0040_02.p
df test_os.awk

It would appear that the desired result is a space delimited list of just
the file names in the default directory. This works in *ix because file
names containing spaces are essentially unheard of: only a fool or
masochist would use them.

In other contexts, e.g. cat $(dir), the pseudovariable $(dir) resolves to
the *contents* of every file in the directory, but cat "$(dir)" is the
same as echo $(dir).

The example
MYVAR=$(dir)
is of the former type. However I see little real use for converting the
psuedovariable into a literal variable.

My suggestion to the OP, and others who want bash functionality under
Windows is to install Cygwin and actually use bash rather than trying to
make Windows do something it was intended never to do.

--
T.E.D. (tda...@mst.edu)

frank.w...@yahoo.com

unread,
Feb 25, 2009, 5:23:32 PM2/25/09
to
On Feb 23, 6:34 am, Ingo Nolden <nuttygraph...@yahoo.com> wrote:
> I woulc like to know wether the windows commandline/batch language
> supports command substitution.

Not inherently, but it can be added by other programs.

> for /f %t in ('CD') do @set XX=%t ; store output of CD command
>
> This is what I want to do:
>
> MYVAR=$(dir)

FOR will only output one line at a time. Since you want all the lines
try:

CONSET MYVAR=`dir`

CONSET is located at <http://www.geocities.com/fp.westlake/>.

That will store all lines in the variable, but ECHO will only print
the first line. Use 'SET MYVAR' to print all the lines. SET will print
the variable name as well as the contents; to print just the contents
use 'CONSET /V MYVAR'.

Frank

Ingo Nolden

unread,
Mar 1, 2009, 12:39:25 PM3/1/09
to
Ted Davis schrieb:

> On Mon, 23 Feb 2009 07:23:54 -0800, T Lavedas wrote:
>
>> I don't read the request the same way as you do. I believe the OP is
>> asking for a means of storing the entire output of a DIR command in an
>> environment variable. While this might be possible in some limited sense,
>> the command line limit (1024 characters, I believe) would seem to get in
>> the way for larger listings.
>
> Not exactly. screen dump:
>

> My suggestion to the OP, and others who want bash functionality under


> Windows is to install Cygwin and actually use bash rather than trying to
> make Windows do something it was intended never to do.
>

I am sorry, that I was so unprecise about what I wanted. I will try to
clarify here.

1. It is not important for me to put large amounts of data or multiple
lines into the variable. I all practical cases I remeber where I have
missed this, it was only about a small amount of data.

2. I do not want to pose any preconditions on the target machines. Thats
why cygwin is not the solution. I use cygwin on my machine however for
quite some time.

3. I accept very complex solutions, if nothing else works. And I have
found possible solutions in the answers to my questions. Thank you all
for that.

Actually I was using windows a long time before I first got into contact
with a bash. But very soon I found that the bash is a lot superiour to
the windows commandline. I only occasionally write batch files, and
those tricks mentioned here are far beyond what I would be able to
invent myself.

Anyway, I just wanted to confirm, there is no builtin way to do this.
Thank you for making that clear.
I was thinking that anyway, but got into doubts after seeing that it is
at least possible within a FOR /f loop.

Ingo

0 new messages