0

I have a python script script.py designed to run at the command line. To make it easier to run, I have a windows batch file script.bat which sets up some defaults and other things. They exist in the same directory.

If I run >script at the command prompt, then the python script is run preferentially to the batch file. >script.bat works as expected.

>where script lists the batch file first, so to my understanding, it should be run preferentially to the python script.

Can I ensure that the batch file is run preferentially without renaming or using the file extension?

0

2 Answers 2

3

the order of where is not the order it will execute. Where lists files in the local path first, in alphabetic order, then will it list the names in the environment path. So assume in the working dir you have files:

test
test.bat
test.cmd
test.py

that is how they will be listed, alphabetically.

which executes first is a matter of order from the pathext variable, an example, by running set pathext from cmd:

PATHEXT=.PY;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PYW

In this modified version, I placed .py first, meaning that if only the name is specified it run by the extension in the order of the list. So given your example of only the 2 similar files with extensions .bat and .py here script.py will be launched first.. So if I move it to the end:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW

Now if we run script will it launch the .bat first.

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

3 Comments

I'm pretty sure it is not where but the file system (NTFS) that does the alphabetic sorting. And where also uses the PATHEXT variable…
@aschipfl, correct, I meant rather that where will list alphabetically and has no relation to the executable extension order.
Oh, you're right; even the extensions become sorted alphabetically (by whatever), I wasn't aware of that, I thought where returns them in the order they appear in PATHEXT, but obviously I was wrong…
0

Solved by making my script into a zip file containing a main.py. script.pyz runs after the batch.

Comments

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.