1

I'm trying to make a batch file that will open python files, from competition, in notepadd++. Different stages have dieffrent number of tasks. It allows you to choose year and stage. Also it allows you to open them only to view. Last think it does it asks you if you would like to create missing files. I'm using exist to check if the file exist, notepad++ will automatically ask you if you want to create missing files, and I would like to avoid that.

@echo off
echo What stage?
SET /p etap=
echo What year?
SET /p rok=
echo Edit? [y/n] ::Open as read only or not. 
SET /p mode=

if %mode%==y (
    echo Create missing files? [y/n]
    SET /p missing=
    if %missing%==y (
        if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
        if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
        if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
        if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z4.py
        if %etap%==2 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
        if %etap%==2 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
        if %etap%==2 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
        if %etap%==1 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
        if %etap%==1 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
        if %etap%==1 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
    ) else ( 
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z4.py if %etap%==3 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z4.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py if %etap%==2 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py if %etap%==2 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py if %etap%==2 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py if %etap%==1 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py if %etap%==1 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
        if exist E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py if %etap%==1 start notepad++ E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py ) 
) else (
    if %etap%==3 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
    if %etap%==3 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
    if %etap%==3 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
    if %etap%==3 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z4.py
    if %etap%==2 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
    if %etap%==2 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
    if %etap%==2 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py
    if %etap%==1 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z1.py
    if %etap%==1 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z2.py
    if %etap%==1 start notepad++ -ro -nosession E:\Programs\Python\Programs\Logia\%rok%_e%etap%_z3.py )
start chrome http://logia.oeiizk.waw.pl/nowa/page.php?sr=logia%rok%/%etap%etap
pause

When I run the file it asks first 3 questions and closes. I think it is because of the brackets.

2
  • I'm actually fairly certain that it's that inline comment you have there. Batch doesn't have inline comments; :: is technically a line label, and when it's not the first thing on a line, there can be side effects. Try moving the comment to its own line. Commented Feb 26, 2015 at 13:25
  • Comments are only at the stackoverflow, so i can show what i meant. Commented Feb 26, 2015 at 13:26

1 Answer 1

2

It fails as percent expansion is done when a (parnethesis) block is parsed, before the code will be executed.

So %missing% expands to the value it has before the line if %mode%==y ( is executed.

To avoid this you can use delayed expansdion.

setlocal EnableDelayedExpansion

...
if !mode!==y (
    echo Create missing files? [y/n]
    SET /p missing=
    if !missing!==y (
...
1
  • Now if I anwser no (n) to the "Edit?" question, it doesn't work. It opens normaly as r/w not r/o Commented Feb 26, 2015 at 13:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.