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

return values and variables

23 views
Skip to first unread message

graham_...@hotmail.com

unread,
Mar 20, 2009, 6:12:58 AM3/20/09
to
Hi,


I'm wrestling with dos (infrequent user). I need to call a command and
to get the return value into a variable for subsequent use.

for example lets say I want to call the command "dir" and have the
output stored in a variable which I will later use.

(in reality I'm getting the name of my current view and passing the
return value as a parameter to another script).

thanks much for any help, I've spent too long on this and feel kind
stupid! :(

G

foxidrive

unread,
Mar 20, 2009, 7:02:54 AM3/20/09
to
On Fri, 20 Mar 2009 03:12:58 -0700 (PDT), graham_...@hotmail.com wrote:

>I'm wrestling with dos (infrequent user). I need to call a command and
>to get the return value into a variable for subsequent use.

Assuming Windows 2k or Xp etc

file.exe
set var=%errorlevel%
echo errorlevel is %var%

>for example lets say I want to call the command "dir" and have the
>output stored in a variable which I will later use.

A variable doesn't normally contain carriage returns...

>(in reality I'm getting the name of my current view and passing the
>return value as a parameter to another script).

What is a 'current view' ?

T Lavedas

unread,
Mar 20, 2009, 9:12:07 AM3/20/09
to

You're going to have to be more specific, I think. As foxidrive has
already indicated, the 'return value' of a command line statement is
its ERRORLEVEL, which is not really what I think you are after. What
you want is access to the text OUTPUT from the command. Often
(usually), this output contains multiple lines of text, which
foxidrive also said. It's not easily stored in an environment
variable.

Having said all that, let me suggest that you investigate the many
virtues (and foibles) of the FOR statement. Start with the on-line
documentation (FOR/? and/or HH ntcmds.chm::/for.htm). Then look up
Timo Salmi's FAQ at http://www.netikka.net/tsneti/info/tscmd.htm (see
item 86, for example).

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

FileGod

unread,
Mar 22, 2009, 4:48:36 PM3/22/09
to
graham_...@hotmail.com wrote:
>Hi,

I guess I understand your question, maybe this will help, this will read the
first line from text file & get it into a variable:

dir/b >temp.txt
@echo off
set /p FN=<temp.txt
echo %FN%

http://www.filegod.netfirms.com/Bats

Graham Hobbs

unread,
Mar 22, 2009, 11:06:31 PM3/22/09
to
On Fri, 20 Mar 2009 03:12:58 -0700 (PDT), graham_...@hotmail.com
wrote:

>Hi,

Graham W,
Something you might find useful but then maybe I'm not really
answering your question; anyway e.g.

if errorlevel 1 echo Heres a little msg > c:\ermsg1.txt

and the one record in the file ermsg1.txt would be

'Heres a little msg'

More to the point is that the file is accessible ad infinitum in most
environments. Learned this from foxidrive and bilious last year. I use
it - simpler than learning all the little trickies of DOS commands
(well, for me anyway):-).
Graham H

FileGod

unread,
Mar 23, 2009, 12:53:40 AM3/23/09
to
Graham Hobbs <gho...@cdpwise.net> wrote:
>if errorlevel 1 echo Heres a little msg > c:\ermsg1.txt
>and the one record in the file ermsg1.txt would be
>'Heres a little msg'

Is this more like it?

@echo off
FIND /I "Your Text" Some.txt
IF ERRORLEVEL 1 GOTO NOT_FOUND
GOTO NOT_FOUND
:FOUND
echo I Found It> c:\ermsg1.txt
GOTO End
:NOT_FOUND
echo echo I did Not Find It>c:\ermsg1.txt
:End

http://www.filegod.netfirms.com/Bats

foxidrive

unread,
Mar 23, 2009, 1:53:23 AM3/23/09
to
On Mon, 23 Mar 2009 04:53:40 GMT, 0@0.0 (FileGod) wrote:

>@echo off
>FIND /I "Your Text" Some.txt
>IF ERRORLEVEL 1 GOTO NOT_FOUND
>GOTO NOT_FOUND
>:FOUND
>echo I Found It> c:\ermsg1.txt
>GOTO End
>:NOT_FOUND
>echo echo I did Not Find It>c:\ermsg1.txt
>:End


Mate, please pay some attention to the logic in your batch files will ya?
None of us are impervious from logic errors but I'm not sure I've seen any
code from you yet that works without errors.

Also, if it's untested code, then say so.

These lines in your code will never be executed.

foxidrive

unread,
Mar 23, 2009, 2:07:01 AM3/23/09
to
On Mon, 23 Mar 2009 16:53:23 +1100, foxidrive <got...@woohoo.invalid>
wrote:

>On Mon, 23 Mar 2009 04:53:40 GMT, 0@0.0 (FileGod) wrote:
>
>>@echo off
>>FIND /I "Your Text" Some.txt
>>IF ERRORLEVEL 1 GOTO NOT_FOUND
>>GOTO NOT_FOUND
>>:FOUND
>>echo I Found It> c:\ermsg1.txt
>>GOTO End
>>:NOT_FOUND
>>echo echo I did Not Find It>c:\ermsg1.txt
>>:End


Sorry Filegod, excuse my rant, but if there are two outcomes in your batch
file then test both of them.

FileGod

unread,
Mar 23, 2009, 3:00:22 AM3/23/09
to
foxidrive <got...@woohoo.invalid> wrote:
>Mate, please pay some attention to the logic in your batch files will ya?
>None of us are impervious from logic errors but I'm not sure I've seen any
>code from you yet that works without errors.

>Also, if it's untested code, then say so.

Nope it's not tested, I based it on my Freeware.bat which DOES work.

Esra Sdrawkcab

unread,
Mar 23, 2009, 6:42:56 AM3/23/09
to

You do need to be more careful (and I should know, as I'm often guilty of
not testing before posting)
the error here is quite blatant IMO

<--quote-->


@echo off
FIND /I "Your Text" Some.txt
IF ERRORLEVEL 1 GOTO NOT_FOUND

GOTO NOT_FOUND <-- always jumps to same label as above. Remove this line.


echo I Found It> c:\ermsg1.txt
GOTO End
:NOT_FOUND

echo echo I did Not Find It>c:\ermsg1.txt <-- double echo
:End
<--End quote-->

I would not write to the root of the c drive in general.


--
Reverse! Nuns!

Todd Vargo

unread,
Mar 23, 2009, 5:40:59 PM3/23/09
to
foxidrive wrote:
...
> excuse my rant, but if there are two outcomes...

foxi, in case you were not aware, teranews does not honor cancel msgs.
Although I concur with your sentiment, I have been reading his posts with
tongue in cheek for some time.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

FileGod

unread,
Mar 24, 2009, 1:21:41 AM3/24/09
to
"Esra Sdrawkcab" <ad...@127.0.0.1> wrote:
><--quote-->
>@echo off
>FIND /I "Your Text" Some.txt
>IF ERRORLEVEL 1 GOTO NOT_FOUND
>GOTO NOT_FOUND <-- always jumps to same label as above. Remove this line.
>echo I Found It> c:\ermsg1.txt
>GOTO End
>:NOT_FOUND
>echo echo I did Not Find It>c:\ermsg1.txt <-- double echo
>:End
><--End quote-->

Dang, you are right Esra, I guess I was too tired & should have tested it,
I fixed it...


@echo off
FIND /I "Your Text" Some.txt
IF ERRORLEVEL 1 GOTO NOT_FOUND

GOTO FOUND


:FOUND
echo I Found It

GOTO End
:NOT_FOUND


echo I did Not Find It

:End

foxidrive

unread,
Mar 24, 2009, 4:33:51 AM3/24/09
to
On Mon, 23 Mar 2009 16:40:59 -0500, "Todd Vargo" <tlv...@sbcglobal.netz>
wrote:

>foxi, in case you were not aware, teranews does not honor cancel msgs.

Thanks Todd, and for your comments.

Esra Sdrawkcab

unread,
Mar 24, 2009, 3:14:18 PM3/24/09
to

OK, but it still smacks a bit of "first time coder" - unless you feel the
need to document things, the
...
GOTO FOUND
:FOUND
...
is redundant.

PS For myself, I use all lowercase text for code with the exception of
variables, I find your case-style inconsistant (e.g. echo GOTO).

Sorry if this all seems a bit critical, hope you can find some use for my
comments.

--
Reverse! Nuns!

graham_...@hotmail.com

unread,
Mar 27, 2009, 5:40:52 AM3/27/09
to
Whoaaahhh! Thanks for all the replies guys. I'll take all your
suggestions and try getting my stuff to work (for info the "view" I
mentioned was a clearcase view. So from the command line I'm doing
this;

cleartool pwv # this gets the working view

and I'll pass that return value (after parsing) to another function.

I note that in all the examples mentioned above, the result of the
command I want to call is passed into a temporary file which I must
clean up afterwards. I can live with that but I would have thought dos
would allow me to achieve same without using a file.


thanks for all the replies.

GrahamO
On Mar 24, 8:14 pm, "Esra Sdrawkcab" <ad...@127.0.0.1> wrote:

> Reverse! Nuns!- Hide quoted text -
>
> - Show quoted text -

Esra Sdrawkcab

unread,
Mar 28, 2009, 5:35:22 AM3/28/09
to
On Fri, 27 Mar 2009 09:40:52 -0000, <graham_...@hotmail.com> wrote:

> Whoaaahhh! Thanks for all the replies guys. I'll take all your
> suggestions and try getting my stuff to work (for info the "view" I
> mentioned was a clearcase view. So from the command line I'm doing
> this;
>
> cleartool pwv # this gets the working view
>
> and I'll pass that return value (after parsing) to another function.
>
> I note that in all the examples mentioned above, the result of the
> command I want to call is passed into a temporary file which I must
> clean up afterwards. I can live with that but I would have thought dos
> would allow me to achieve same without using a file.
>
>
> thanks for all the replies.

You might want to look to see if anyone else has used clearcase tools and
errorlevels

http://www.codeguru.com/forum/archive/index.php/t-315793.html

--
Reverse! Nuns!

0 new messages