1

I have a batch script with multiple if conditions.

  • Check if a folder C:\Apps\WorkingFolder\NewAppln exists.
    • If it doesn't exist execute a .jar from a network drive, G:.
    • If it does exist compare a file CheckDate.log on both the local drive and network drive.
      • If logs are the same, execute a .jar from a network drive, G:.
      • If logs are different, rename the existing folder to folder_sysdate in local, and execute a .jar from a network drive, G:.

Here is my code

@ echo on
SETLOCAL EnableDelayedExpansion
IF   EXIST  C:\Apps\Workingfolder\NewAppln\  (goto FOUND) else ( goto NOTFOUND)
:EOF

: FOUND
FC C:\Apps\Workingfolder\NewAppln\CheckDate.log   G:\Workingfolder\NewAppln\CheckDate.log | find "***">NUL
    (IF ERRORLEVEL 1 (GOTO SAME) ELSE (GOTO DIFFERENT)
    GOTO :EOF

    :SAME
    @"G:\JRE1.509\bin\java" -jar "loadApp.jar"
    START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe
    GOTO :EOF

    :DIFFERENT
    move C:\Apps\Workingfolder\NewAppln C:\Apps\Workingfolder\NewAppln_%time:~0,2%%time:~3,2%-%DATE:/=% 
    @"G:\JRE1.509\bin\java" -jar "loadApp.jar"
    START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe
    )
:EOF

:NOTFOUND
cd G:\Workingfolder\NewAppln_WIN10\
@"G:\JRE1.509\bin\java" -jar "loadApp.jar"
START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe
2
  • There are flaws in your code, syntax IF, No lables inside (code blocks) and several more... Commented Apr 18, 2019 at 10:34
  • Can you please advise on how the correction can be made Commented Apr 18, 2019 at 11:35

2 Answers 2

1
@echo on
SETLOCAL EnableDelayedExpansion
IF EXIST "C:\Apps\Workingfolder\NewAppln\" (goto FOUND) else goto NOTFOUND
GOTO :EOF

: FOUND
FC "C:\Apps\Workingfolder\NewAppln\CheckDate.log" "G:\Workingfolder\NewAppln\CheckDate.log | find "***" >NUL
IF ERRORLEVEL 1 (GOTO SAME) ELSE GOTO DIFFERENT
GOTO :EOF

:SAME
@"G:\JRE1.509\bin\java" -jar "loadApp.jar"
START "" /D "C:\Apps\Workingfolder\NewAppln" MyApp.exe
GOTO :EOF

:DIFFERENT
move "C:\Apps\Workingfolder\NewAppln" "C:\Apps\Workingfolder\NewAppln_%time:~0,2%%time:~3,2%-%DATE:/=%"
@"G:\JRE1.509\bin\java" -jar "loadApp.jar"
START "" /D "C:\Apps\Workingfolder\NewAppln" MyApp.exe
GOTO :EOF

:NOTFOUND
cd "G:\Workingfolder\NewAppln_WIN10\"
@"G:\JRE1.509\bin\java" -jar "loadApp.jar"
START "" /D "C:\Apps\Workingfolder\NewAppln" MyApp.exe

You do not create labels named :EOF as goto treats :EOF as End Of File.

Use of parentheses was unneeded in some areas so removed them.

Done minor cleanup and added some double quotes with some paths.

It is usually a good idea to specify a title for start so it does not cause issues as the first set of double quotes can be taken as the title whether you like it or not.

Sign up to request clarification or add additional context in comments.

2 Comments

Very tangled code. Using of goto should be minimized for the sake of readability and better understanding.
why not simply move the code into the code blocks, instead of the goto statements?
0

Refactored the code a little. Although there's a lot of room for improvement (especially these blocks with Java launching).

@echo on
SETLOCAL EnableDelayedExpansion
IF EXIST C:\Apps\Workingfolder\NewAppln\ (
   FC C:\Apps\Workingfolder\NewAppln\CheckDate.log  G:\Workingfolder\NewAppln\CheckDate.log | find "***">NUL
   IF ERRORLEVEL 1 (
      @"G:\JRE1.509\bin\java" -jar "loadApp.jar"
      START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe
   ) else (
      move C:\Apps\Workingfolder\NewAppln C:\Apps\Workingfolder\NewAppln_%time:~0,2%%time:~3,2%-%DATE:/=% 
      @"G:\JRE1.509\bin\java" -jar "loadApp.jar"
      START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe
   )
) else (
   cd G:\Workingfolder\NewAppln_WIN10\
   @"G:\JRE1.509\bin\java" -jar "loadApp.jar"
   START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe
)

BTW NOTFOUND part will not work as expected since the script will get there when the folder C:\Apps\Workingfolder\NewAppln is not exist but the command line START /D"C:\Apps\Workingfolder\NewAppln" MyApp.exe does explicitly use it.

4 Comments

Bringing the multiple C:\Apps\Workingfolder\NewApplns out as SET "APPPATH=C:\Apps\Workingfolder\NewAppln" might help (especially if it ever changes).
@TripeHound Completely agree. There's a lot that could be improved but the question from OP was about if nesting so I've decided to leave additional refactoring as a 'homework' :).
What changes do you recommend for the jar part ..please advise .I will try to work on that
@zainab.july2019 If you'll look closely then you'll understand that parts with java.exe and start of MyApp.exe are executed in the end of every possible condition tree. That means you could exclude them out of the conditions and put after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.