Batch file not returning correct value after using bat to exe

2

I'm having lots of issues with this script. It's supposed to read the a file (that the user specifies) then save it to the storeresult environment variable.

I'm using a BAT To EXE converter that doesn't affect any batch commands, so it's not that. (Just if you're wondering why i'm calling storvar instead of storvar.bat)

The main script (storvar.exe):

set a=%~1
set b=%~2
set c=%~3
for /f "delims=" %%a in (stor\%a%.stor) do set storeresult=%%a
pause

The script i'm using to call the script (No errors with calling):

storevar Hi hi
pause
echo %storeresult%
pause

When I call it, it just returns the result (%storeresult%) as nothing, I cant find the issue!

ZeekPlayz

Posted 2018-06-10T04:30:17.150

Reputation: 159

What is in stor\Hi.stor? Where are you writing this file? Please show all your code if you expect an answer. – DavidPostill – 2018-06-10T05:49:10.123

Did it work before you used the bat to exe converter? – Mokubai – 2018-06-10T05:56:17.440

No, Fleet Commands answer helped me. – ZeekPlayz – 2018-06-10T18:04:20.243

First of all, welcome to Super User! As you get settled into the community, it might be prudent to devote a few minutes towards reading the Help section tutorials. For example, if it is true that Fleet Command's answer helped you, the following is probably applicable: What does it mean when an answer is "accepted"?

– Run5k – 2018-06-16T16:14:29.890

Answers

1

When you try to define and use a variable, you must always be mindful of its scope.

Environment variables that you define using set command are only valid within the scope of the process in which they are defined. It is easy to prove with an experiment:

  1. Open one Command Prompt and try to define set a = b. Use set to verify that it is properly defined.
  2. Now, open a second Command Prompt and invoke set. You see that a is not defined in this scope.

storvar.exe is a separate process. Hence, it has its own scope. Whatever environment variable you define in it stays there and is deleted when it exits.

So, is there an alternative? Well, yes. I myself use PowerShell instead of the deprecated batch files; it can extract stuff from the file and either use them straightaway or emit them to the next script. (Microsoft officially supports compiling PowerShell to EXE, although you can find newbie-oriented ones on TechNet Gallery as well.) AutoIt is another alternative.

user477799

Posted 2018-06-10T04:30:17.150

Reputation: