简体   繁体   中英

Batch File to acess a directory and execute a python script

I'm trying to create a batch file to execute python scripts in different directories, something like that:

C:\Test
   |---Test1\example.py
   |---Test2\example.py
   |---Test3\example.py
|--run.bat

I have multiple folders inside Test, 'Test1', 'Test2' and 'Test3', I need to run them simultaneously with different command prompts, but i'dont know how to do that, I got something like that:

@echo off
set back=%cd%
for /d %%i in (C:\Test\*) do (
start
cd "%%i"
python example.py
pause
cd %back%
)

But it only runs a script and goes back to the home directory, so I noticed, I believe it is running for only the first directory, any suggestions for this problem?

Thanks!

Add the keyword start before the command to execute it in a new prompt.

to open the python interactive shell in a new cmd prompt.

start python

to execute a python file new.py in a new cmd prompt.

start python new.py

EDIT:

here is the complete code to execute the scripts on different cmd prompts

@echo off
echo "this is the main batch script"
for /d %%i in (C:\Test\*) do (
start python "%%i\example.py"
)
pause

When you need to run things simultaneously, you can't do this in a batch file: a batch file is executing commands in sequence, not simultaneously.

I'd propose you to add those things to the task scheduler, and let all things start at the same moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM