edisonx Posted September 15, 2011 Posted September 15, 2011 hello, everyone, I wonder to know , how to using redirection pipe ( | ), so I write three batch files (sum.bat, rstring3.bat main.bat) to test.like those:: filename : sum.bat@echo off:main SET low=%~1% SET up=%~2% SET ret=0 FOR /L %%I IN (%low%,1,%up%) DO ( SET /A ret+=%%I ) echo %low%+...+%up%=%ret%:: filename : rstring3.bat@echo off:main SET String=%1% SET String=%String:~-3% echo %String%:: filename : main.bat rstring3.bat | sum.bat 1 10in command line, I typeC:\> main.batIt wilil display 1+...+10=55but I think it will display 55what's wrong with the code ? how can I do to fix the wrong answer ?thanks for everyone, and sorry for my poor English.
allen2 Posted September 15, 2011 Posted September 15, 2011 (edited) The "|" is used the same way as other OS use it and the command placed at the right of the pipe will process the result of the command placed before.As for your example, i don't understand what you're trying to do so i can really help there but the only way it would make sense would be :main.batsum.bat 1 10| rstring3.bat and modify also rstring.bat@echo off:main SET String=%ret% SET String=%String:~-3% echo %String%More info about pipe there and the full history there.Edit reason: forgot to say that the output of a batch can't be considered as the argument of another and then you have to pass through another way like a variable. Edited September 15, 2011 by allen2
edisonx Posted September 15, 2011 Author Posted September 15, 2011 thanks for your replay, allen2 . I had studying the website that you gave to me.I really misunderstood pipe '|' ,thank you a again. I used to programming in C/C++ language, but learning to use batch command recently by some reasons.I think that there are many instructions in *.h files in C, so what I need , call the instruction just include ???.h file.But in batch, if there is a batch command was used frequently, will it be written down again in different batch files ? just like sum.batI had ever try this code@echo off:mainSET rst=0call :sum 1 10 rstecho %rst%goto :eof :: --------------------------------:: param 0 : sum (function name):: param 1 : low bound:: param 2 : up bound:: param 3 : ans:: --------------------------------:sumSETLOCAL ENABLEDELAYEDEXPANSION &Rem using local variable SET low=%~1% SET up=%~2% SET rst=0 FOR /L %%i IN (%low%,1,%up%) DO ( SET /A rst+=%%i )(ENDLOCAL &Rem return value SET %3=%rst%)goto :eof in this case,if sum function is used frequently, I think that coding it in another .bat is better , right ?I'm confused how to like C language, to make a function-like batch, let other batch files can re-used.Is this the only way to maintain the source ?:: filename : sum.bat:: --------------------------------:: param 0 : sum (function name):: param 1 : low bound:: param 2 : up bound:: param 3 : ans:: --------------------------------:sumSETLOCAL ENABLEDELAYEDEXPANSION &Rem using local variable SET low=%~1% SET up=%~2% SET rst=0 FOR /L %%i IN (%low%,1,%up%) DO ( SET /A rst+=%%i )(ENDLOCAL &Rem return value SET %3=%rst%)goto :eof :: filename : demo_sum.bat@echo off:demo_sum SET ret=0 call sum.bat 1 10 ret echo ret=%ret%your reply helps me a lot!!thank you again *^_^*
jaclaz Posted September 15, 2011 Posted September 15, 2011 Edit reason: forgot to say that the output of a batch can't be considered as the argument of another and then you have to pass through another way like a variable.Not really-really. There are tricks :http://www.robvanderwoude.com/files/readline_2k.txtessentially:::pipetome.cmd::A batch capable of accepting PIPE input@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=*" %%A IN ('MORE') DO SET Got_From_Pipe=%%ASET Got_From_Pipejaclaz
allen2 Posted September 15, 2011 Posted September 15, 2011 It was clear to me that you were more used to c++. You're right about one should create libraries for most used functions but in .bat it could become more complex.You might be interested in mingw as it can compile c, c++ under windows (that what i would use for creating libraries) and create more exe.@JaclazThat was exactly why i said "you have to pass through another way". I knew i could parsed the ouput with a for command the as the variable was already set why not use it ?
edisonx Posted September 15, 2011 Author Posted September 15, 2011 thank you , allen 2.and sorry for I still have another question..a simple code in C/* pow.c , generate pow.exe */#include <stdio.h>#include <stdlib.h>#include <math.h>int main(int argc, char **argv){ if(argv!=3) return 1; else printf("%lf", pow( atof(argv[1]), atof(argv[2])) ); return 0;}:: demo.batpow.exe 1.23 4.56it will show 2.570202 in console window. but I can't get it in batch-variable. I handle it by redirection to a file, then read the file again:: demo.batSET c=0pow.exe 1.23 4.56 > _rst.txtFOR /F %%I IN ( _rst.txt) DO ( SET c=%%I)del _rst.txtI know, the method is slow and stupid. I just want to know , in batch file, is the method good ? or is any method better than this ?------As you said, I want to save the value into a variable,but in C language, it's a big problem, just like this/* filename: pow2.c generate: pow2.exe */#include <stdio.h>#include <stdlib.h>#include <math.h>int main(int argc, char **argv){ if(argv!=4) return 1; else sprintf(argv[3], "%lf", pow( atof(argv[1]), atof(argv[2])) ); return 0;}I can't do that!! the behavior is very dangerous in C. in batch file @echo offSET rstcall pow2.exe 1.23 4.56 rstecho %rst%those codes can't work , because pow2.exe receive argv by argv[0]=pow2.exeargv[1]=1.23argv[2]=4.56argv[3]=rst const string, not variable, I can't change its value!!----maybe, I should ask another question : 「how to pass the envionment variable to batch file in C ? 」----I know it's a big and studip problem, but I just want to know , how can I write some application in C, and batch file can using it. Thanks for your replies.
jaclaz Posted September 15, 2011 Posted September 15, 2011 @JaclazThat was exactly why i said "you have to pass through another way". I knew i could parsed the ouput with a for command the as the variable was already set why not use it ?Yep , but in this case (since the thing producing the piped text is a batch and it's output is already an Environment Variable) only.I thought it would have been useful the posted trick as a solution to the "generic" problem "my batch won't accept piped input" .@edisonxPlease do READ my previous post.You can pipe the output of your small program into a batch (if needed).More generally you use a FOR loop like:FOR /F "tokens=* delims=" %%A in ('pow.exe 1.23 4.56') DO SET myvar=%%Ajaclaz
edisonx Posted September 15, 2011 Author Posted September 15, 2011 Hi, jaclaz thank you for your replay. I need some time to study and understand the code that you wrote .and need some time to try those.thank you very much, help me a lot !!
edisonx Posted September 15, 2011 Author Posted September 15, 2011 Hi, dear jaclaz :I try your code , it wroks fine "now" . First, I use vc 2008 to compile my C code, there is something wrong !! (It generates execute file, but I can't use it normal).But I use gcc to compile my C code, It's fine !!!! (so suprise !!)I reboot my computer, re-compiler the same code by gcc and vc, then , the batch works fine now. next, I wonder to build all C math / string librarys and some floating operator to cmath.exe.Just because batch can't cal floating and can't using array. allen2, jaclaz, thank you a lot.you help me really very much.again and again.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now