1

I have a folder where I keep my python scripts used for backing up a particular system. Z:\System Administrator\System Backups\My System\Scripts\

To start them all going I would like to use a .bat file kept in the same folder, however I have zero experience with these files. My reading has lead me to this solution :

@echo off
start /B python "File one.py"
start /B python "It's another file.py"   

And if I list every python file in this way then it works as I would want. How can I run all *.py files that appear in this folder regardless of number or filename ?

Thanks in advance

Gerhard
  • 18,114
  • 5
  • 20
  • 38
CatParky
  • 189
  • 4
  • 18
  • 3
    `for %%i in (*.py) do start "" /b python "%%i"` – Gerhard Feb 13 '19 at 13:54
  • Thank you for your quick response. Does this run each in turn or at the same time ? – CatParky Feb 13 '19 at 14:23
  • It will pretty much run at once, one after the other, but not waiting for the first to complete. How do you want it to run? once after the other, while waiting for one to complete or at once? – Gerhard Feb 13 '19 at 14:31
  • One at a time, please. Thank you for your quick response – CatParky Feb 13 '19 at 14:57
  • Then just add /wait. Like `for %%i in (*.py) do start "" /b /wait python "%%i"` – Gerhard Feb 13 '19 at 15:00
  • At first I'd look at this: [How to do something to each file in a directory with a batch script](https://stackoverflow.com/q/180741) – aschipfl Feb 13 '19 at 15:06
  • Note that `start /b` leaves each Python process attached to the console with standard I/O still connected to it. You may want to redirect stdin to `NUL` and stdout/stderr to a file. Also, since Python is still attached to the console, Ctrl+Break and closing the window will abruptly terminate the script. You can use ctypes to install a control handler (`SetConsoleCtrlHandler`) that exits cleanly in this case. That said, as a Python programmer, I'd prefer to write a Python script that uses the subprocess module instead of using a batch script. – Eryk Sun Feb 13 '19 at 20:01
  • Thank you, this works perfectly. – CatParky Feb 14 '19 at 09:05

1 Answers1

2

That's possible with the following trick:

@echo off

for %%A IN (*.py) do start /b /wait "" python "%%~fA"

Note: For older Windows versions (pre Windows 10) the order is specific. , See here.

Gerhard
  • 18,114
  • 5
  • 20
  • 38
double-beep
  • 3,889
  • 12
  • 24
  • 35
  • 3
    Without wishing to sound rude, this is almost certainly a duplicate of thousands of answers on this site. Please do not post answers like this just to build up reputation points, it does not help anyone but yourself, and that's not what we're here for. – Compo Feb 13 '19 at 13:59
  • @CatParky it was directed at the person that answered the question. – Gerhard Feb 13 '19 at 14:30
  • As already mentioned by @eryksun, the order is important and is worth mentioning. Start also commonly expected the first double quoted string to be the title of the window. It would also be nice if you mention if someone else already assisted in a comment to give you the answer provided. Just changing the code in your aswer does not make the answer valid for acception. We are here to help people, not to post what we can as quickly as possible just to gain reputation. – Gerhard Feb 13 '19 at 21:02