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
>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' ?
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/
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%
>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
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
>@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.
>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.
>Also, if it's untested code, then say so.
Nope it's not tested, I based it on my Freeware.bat which DOES work.
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!
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)
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
>foxi, in case you were not aware, teranews does not honor cancel msgs.
Thanks Todd, and for your comments.
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!
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 -
> 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!