Batch file running Python script - batch-file

What I would like to do is create a batch file that will temporarily add a environment variable to the Python executable. From there call the executable and open another script from the batch file. I'm very new to batch and have been researching this, so far I've found that to add a environment variable I would need something like:
set env="path/to/exe"
And to call a script I would need to use:
call "path/to/script"
My question being, if I where to combine the two two and then call the script, would I be able to do something like this:
set env="python.exe"
call "python script.py"
Will this work like I would expect it to?

Well the CALL is used for calling (opening) batch files. If you wanted to open a python script through batch (and set the environment) you would have use the STARTcommand and code it like this:
set env=python.exe
start python script.py
This should work (combined) if you have the Python environment.

Related

How to start Python virtual environment in .bat batch file?

I need to set up a long list of temporary environment variables every time when I start a Python Flask app on Windows 10. Now I would like to create a batch file to run with all settings in one double click. The following lines run well if I copy them and paste to cmd prompt, but I couldn't run them in the batch file.
The execution of batch file always gets tripped and exited at the 2nd line venv\scripts\activate in the batch file, which has no issue at all if I copy and paste line by line at cmd.
cd C:\py\project
venv\scripts\activate
set env1=val1
set env2=val2
set FLASK_APP=some.py
flask run
One of the many (far too many) quirks of .bat files is that if you launch another .bat file, it doesn't know where to return to.
You need to explicitly call it:
call venv\scripts\activate
you can just use
start venv\Scripts\activate
This will open up a new terminal just like that... you can pass other commands using the && or & sign.

Windows BAT file SET command works in weird way - Python virtual environment activate script

I'm trying to add some variables into Python virtual environment activate.bat.
I supposed it's a regular BAT file with pretty expected behaviour. But what I see is weird.
set env=abc
set db_url=dfg
If I do echo %env% after in the command prompt I see:
abcset db_url=dfg
Who can explain why is that and how to fix it?
Also, if I try clear it up like
set env=
it just reads all the strings after.
UPD: Okay, I don't know what I've done but what worked:
I recreated the virtual environment
I opened all files in VS Code (not in Notepad++ as before)
I put all the variables sets into double quotes like set "env=dev"

Is it possible to embed an executable inside a batch file?

There are many programs which help me write more efficient code (NirCmd etc.), but they can't run properly if they aren't installed in the computer. So is there a way to, for example, in the temp folder, extract the program from a batch program and use it.
I tried reading the executables with a hex editor, putting the hex code into another file and saving it as an executable. But this failed. So, is there any way to efficiently store an executable inside a batch file, create it and then run it?
Yes, there is one that I know of and works perfectly.
It is a program called bhx.exe (link to its site here).
It can also embed other file types.
The usage is quite simple:
(optional) Create a cabinet (.cab) file from the original .exe using this command: makecab yourexe.exe yourexe.cab. For better compression you can use the /D switch in this way: makecab /D CompressionType=LZX yourexe.exe yourexe.cab
CD to the directory bhx is in and do this: bhx yourexe.cab. Other switches are described in the website.
There you go, the mybin.cmd file is generated.

How can my batch file find its own location and call a matlab script inside that folder?

I have an internal software that generates folders with batch files. The batch file is supposed to run a matlab file in the same folder, but in fact it just runs Matlab and the previous Matlab script (not the one in its folder).
I need a command in my batch file to recognize its own location(folder) and run the matlab file from the same folder.
Thank you in advance
use the %0 parameter. This on is an implicit parameter that you do not pass to the scrip
try this and see if it helps you get going:
#echo %~dp0
the ~dp sequence strips the name and extension from the full path to the script.
note that this works only from within a script, not from the command prompt
References: for-command

Change an environment variable in between Jenkins build steps

I have a Jenkins job with a string parameter called MyPath, a groovy script build step and then a batch command build step.
If I kicked off a job so that the value of MyPath is "C:\Temp\", I want the groovy script to change the value of MyPath to be "C:\Temp\3.4\setup.exe" and then I want to use the batch file to execute MyPath.
I know I can launch a process from groovy and that I can perform my groovy logic in batch, it would just be nice if I could pass variables between build steps.
To use a parameter in groovy you have to use the library to resolve it (below). Is there a method that can set it?
def path = build.buildVariableResolver.resolve("MyPath");
Something like this would be perfect if it existed:
buildVariableChanger.change("MyPath") = "C:\3.4\setup.exe"
I have also looked into the EnvInject plugin but from what I understand it does not support what I want to do.
Thanks.
I don't think it is possible since field value for StringParameterValue.class is final: http://javadoc.jenkins-ci.org/hudson/model/StringParameterValue.html
The solution may be creating other parameter in groovy script
import hudson.model.StringParameterValue
import hudson.model.ParametersAction
def newPath = build.buildVariableResolver.resolve("MyPath") + "3.4\\setup.exe"
build.addAction(new ParametersAction (new StringParameterValue ('path2', newPath)))
and then use parameter path2 in batch command
Why don't you simply pass a variable to your batch? You could start the batch from your first script instead of starting it with an own build step. Just call it with yourscript.bat C:\3.4\setup.exe. You can access the argument with %1. If you insert the line %1 into your bat file it will execute C:\3.4\setup.exe.
One option should be to use the SETX Windows command to set the value of user (or system) environment variable.
If you set this variable in your Groovy step, then the batch step should be able to use it.

Resources