Batch script to execute a Python script multiple times
<p>I need to run a Python script N times, so I created the following script:</p>

<pre><code>@ECHO OFF
cd PATH_TO_SCRIPT
@set port=5682
set /P INPUT=Number of servers: 
FOR /L %%I IN (1, 1, %INPUT%) DO (
    @set /a newport=%port%+1
    start cmd /k &gt; start python server.py -i 192.168.1.2 -p %newport%
)
pause
</code></pre>

<p>If I enter <strong>1</strong> as input value, such that there is only one iteration, the script works, but if I choose 2, the script only runs one instance of the server and tells me: "Unable to access file. The file is used from another process". What's wrong?</p>

<p>As pointed out already, in your FOR loop, %newport% resolves to [empty] upon execution. It's because newport isn't set until AFTER cmd resolves %newport%, since the entire FOR loop is one statement. In other words, %newport% gets resolved before the commands in the loop are executed, which means it's not set yet, and /p gets nothing. So presumably it uses the same port each time.</p>

<p>You don't have to use delayed expansion, though. You could just use your FOR /L iterator (%%I). I think this would be simpler.</p>

<pre><code>@ECHO OFF &amp; SETLOCAL
CD PATH_TO_SCRIPT
SET /P INPUT=Number of servers: 
SET /A END=5682+%INPUT%-1
FOR /L %%I IN (5682, 1, %END%) DO ( 
   START python server.py -i 192.168.1.2 -p %%I
)
PAUSE
</code></pre>

<p>Also, I agree that it is strange that you're redirecting the output to a file named start. I think you must have been trying to do something else.</p>

python windows batch-file