Nitin Singh
Nitin Singh

Reputation: 102

Running python scripts in a for loop inside a windows batch file with some arguments passed to them

I'm trying to run the following windows batch file:

@echo off

CALL workon my_env
CD C:\scripts

SET DataPath = C:\user\data
SET SavePath = C:\results

FOR %DataQuantity IN ( 100 75 60 50 40 33 25 10 ) DO (
    FOR %Subject IN ( 1 2 4 5 6 7 8 9 ) DO (
        python classifier.py DataQuantity Subject DataPath SavePath 
        python detector.py DataQuantity Subject DataPath SavePath
        python compiler.py DataQuantity Subject DataPath SavePath
    )
)

I want to three python scripts with different arguments using nested for loops. There are four arguments passed to each of the python scripts, out of which two are set beforehand, and two come form the for loops.

But I'm getting the following error:

"DataQuantity was unexpected at this time"

Upvotes: 1

Views: 1673

Answers (2)

André Ginklings
André Ginklings

Reputation: 381

You are missing percent char:

FOR %%B IN ( 100 75 60 50 40 33 25 10 ) DO (
    FOR %%S IN ( 1 2 4 5 6 7 8 9 ) DO (
        python classifier.py %%B %%S %DataPath% %SavePath%
        python detector.py %%B %%S %DataPath% %SavePath%
        python compiler.py %%B %%S %DataPath% %SavePath%
    )
)

In for use %% and to use setted variable use %VAR%.

Upvotes: 0

Nitin Singh
Nitin Singh

Reputation: 102

@echo off

CALL workon my_env
CD C:\scripts

SET DataPath=C:\user\data
SET SavePath=C:\results

FOR %%q IN ( 100 75 60 50 40 33 25 10 ) DO (
    FOR %%s IN ( 1 2 4 5 6 7 8 9 ) DO (
        python classifier.py %%q %%s %DataPath% %SavePath% 
        python detector.py %%q %%s %DataPath% %SavePath% 
        python compiler.py %%q %%s %DataPath% %SavePath% 
    )
)

Following were fixed:

  1. There should not be any space adjacent to '='
SET DataPath=C:\user\data
  1. The loop variable has to have '%%' as prefix when used in a batch file, and must be one letter only.
%%q
  1. Variables must be encapsulated inside '%'.
%DataPath%

Upvotes: 3

Related Questions