Jefferson Bruchado
Jefferson Bruchado

Reputation: 918

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!

Upvotes: 1

Views: 2535

Answers (2)

Aniket Navlur
Aniket Navlur

Reputation: 1012

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

Upvotes: 2

Dominique
Dominique

Reputation: 17575

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.

Upvotes: 1

Related Questions