0

In batch, I need to read a configuration file written in python that contains multiples variables that I need to set in the batch script !

For example: (variable take are just example, values can be integer, path, ...)

config.py

import time, os
VERSION= "20160801"
PATH = os.getcwd()
...

script.bat

SET VERSION= need to get VERSION variable from config.py
SET PATH =  need to get PATH variable from config.py

I have found this solution :

%PYTHON_PATH% -c "import config;print config.VERSION" > output
set /p CURRENT_VERSION=<output

but it functions just to one variable and i need to set around 30 variables, there exist a method to do this faster without import the config.py each time please ?

Why can i make this :

%PYTHON_PATH% -c "import config;print config.VERSION;print config.PATH" > version,path
set CURRENT_VERSION=version
set PATH=path

Thanks

1 Answer 1

1
for /f "tokens=*" %%a in ('type config.py^|find "="') do set %%a

BUT:
set doesn't ignore spaces, so with your example, it will create a variable named %path % (which isn't that bad, because %path% is an essential system variable. Changing it will lead to unexpected behavour).

You really should tell phyton to write those lines without spaces... (and take care to not use variable names, that are essential to windows)

Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't execute the script python and just return the associate code, so for the variable TODAY it returns " time.strftime("%Y%M%d %H:%m:%S")"
how should it do that with the file content you have shown? If config.py is an executable which outputs what you showed, just delete the type command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.