-4

Why won't this code work correctly?

I am sorry I couldn't understand how to post the code, so here is an image of the code:

![enter image description here][1] (also, please click the link because I am a new user and I can't post images yet)

Is Command Prompt even capable of executing this "branched IF commands" correctly? If so, what am I doing wrong? Anyone knows? Please, reply!

:STARTEXITING
"D:\dbStatusChecker\dbFi1estatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Exit_Pass_1.txt
findstr /m "up to date" dbstatus_uTorrent_Exit_Pass_1.txt
if %error1eve1%==0 (
    SLEEP 2
    "D:\dbStatusChecker\dbFi1estatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Exit_Pass_2.txt
    findstr /m "up to date" dbstatus_uTorrent_Exit_Pass_2.txt
    if %error1eve1%==0 (
        SLEEP 3
        "D:\dbStatusChecker\dbFi1estatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Exit_Pass_3.txt
        findstr /m "up to date" dbstatus_uTorrent_Exit_Pass_3.txt
        if %error1eve1%==0 (
            process -q uTorrent.exe 15
            :STARTSTARTING
            "D:\dbStatusChecker\dbFi1estatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Start_Pass_1.txt
            findstr /m "up to date" dbstatus_uTorrent_Start_Pass_1.txt
            if %error1eve1%==0 (
                SLEEP 2
                "D:\dbStatusChecker\dbFi1estatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Start_Pass_2.txt
                findstr /m "up to date" dbstatus_uTorrent_Start_Pass_2.txt
                if %error1eve1%==0 (
                    SLEEP 3
                    "D:\dbStatusChecker\dbFi1estatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Start_Pass_3.txt
                    findstr /m "up to date" dbstatus_uTorrent_Start_Pass_3.txt
                    if %error1eve1%==0 (
                        D:
                        cd\
                        cd Dropbox
                        cd uTorrent
                        start uTorrent.exe
                        ) else (
                        SLEEP 1
                        GOTO STARTSTARTING
                        ) else (
                    SLEEP 1
                    GOTO STARTSTARTING
                    ) else (
                SLEEP 1
                GOTO STARTSTARTING
                ) else (
            SLEEP 1
            GOTO STARTEXITING
            ) else (
        SLEEP 1
        GOTO STARTEXITING
        ) else (
    SLEEP 1
    GOTO STARTEXITING

For great justice: This text was OCR-ed from https://i.stack.imgur.com/RgsId.png

sehe
  • 374,641
  • 47
  • 450
  • 633
user1307920
  • 11
  • 1
  • 3

3 Answers3

4

cmd can handle nested ifs just fine. Your problem is a different one. As it stands all your if checks, except for the first will not do what you want.

Use delayed expansion by putting a

setlocal enabledelayedexpansion

at the top of your batch file and use !errorlevel! instead of %errorlevel%.

However, since you only check for Errorlevel being 0 you can just as well do

if not errorlevel 1 ...

instead of

if %errorlevel%==0
Joey
  • 344,408
  • 85
  • 689
  • 683
  • I have just one problem left: When everything is done, when uTorrent is executed, the script should stop, right? But it continues! – user1307920 Apr 02 '12 at 14:27
  • Okay, I added "GOTO END" after the line "start uTorrent.exe" and put the :END on the end of the batch file. Now everything works (probably) as it should! Thanks! – user1307920 Apr 02 '12 at 15:09
2

Joey has diagnosed one problem concerning delayed expansion. You have another - you should not put a label within an IF ELSE block - you most likely will not get your desired result. See https://stackoverflow.com/a/8481978/1012053.

Christian has a great suggestion to restructure your code to get rid of the nesting.

Since you are simply executing the same commands with increasing sleep intervals, you can use a FOR /L loop to further simplify your code.

@echo off

:startExiting
set started=
for /l %%N in (1 1 3) do (
  if defined started sleep %%N
  set started=1
  "D:\dbStatusChecker\dbFileStatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Exit_Pass_%%N.txt
  findstr /m "up to date" dbstatus_uTorrent_Exit_Pass_%%N.txt || goto :startExiting
)

process -q uTorrent.exe 15

:startStarting
set started=
for /l %%N in (1 1 3) do (
  if defined started sleep %%N
  set started=1
  "D:\dbStatusChecker\dbFileStatus.exe" "D:\Dropbox" > dbstatus_uTorrent_Start_Pass_%%N.txt
  findstr /m "up to date" dbstatus_uTorrent_Start_Pass_%%N.txt || goto :startStarting
)

d:
cd \Dropbox\uTorrent
start uTorrent.exe
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • It appears that this solution doesn't quite work as it should, but 5 stars because it does exit a program eventually, and 5 stars for effort! I will definitely need this later! – user1307920 Apr 02 '12 at 14:44
1

Since all the else clauses end with a goto, why not reverse them all, then you will eliminate the nesting.

If the code works, job done. If it still doesn't work, well that's one less factor to think about.

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • That should work too, indeed. The problem isn't their nesting though but rather that they don't know what happens if you use environment variables within blocks. I have to admit that I didn't actually try understanding what they did there ... – Joey Apr 02 '12 at 12:04