开发者

storing returned values from functions present in exe file using bat script

开发者 https://www.devze.com 2023-04-13 09:29 出处:网络
I am trying to call a function present in saur.exe using bat. It looks something like this: saur.exe readName

I am trying to call a function present in saur.exe using bat. It looks something like this:

saur.exe readName

When i execute it, it returns a string "Saurabh".

Now that I want to store "saurabh" in a variable called name开发者_如何学Python.

So I am doing :

set name = saur.exe readName
echo .%name%

In this case, it doesnot execute. It gives blank in front of echo command.


For some strange reason, doing what you want requires some awkward workarounds. The long way would be to store the output of the command in a file, then read the file into the variable, and finally delete the file. The shorter (and barely readable) way is:

for /f "delims=" %%a in ('saur.exe readName') do set name=%%a
echo %name%

:-(


Some SET command documentation will show that you can only assign strings to environment variables (http://www.computerhope.com/sethlp.htm).

In your example above, you have actually set an environment variable called:

"name "

Yet you are echoing the variable:

"name"

The best solution I can find is to do something similar to

saur.exe readName>tempFile
SET /p variableName=<tempFile
ECHO %variableName%

Hopefully this helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消