not exiting on exit selection until second time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

not exiting on exit selection until second time

#1 Post by JDAIII » 16 Dec 2014 18:46

First caveat, I have not made a batch file for about 15 years. Haven't had a use for anything like this.

So I am trying to create a simple batch file for our customers to use for testing their files against certain tools like Ghostscript and imagemagick.

The issue is not with those, the issue is in fact that I select 0 at the menu item and it will goto :Inputdesignation instead of exit /b or eof
the first time that I select 0 but if I input the file name again and press 0 when it returns me to the menu, then it will exit. The first time it gives me an error of 'FINDSTR: Bad command line'. I am calling findstr for returning a value from a file which works when I turn on the echo :baseFolder, but not seeing why I get this error at this point in the batch file. I also don't have much experience with findstr and yes I have googled it.

For reference I have had :END at the end of the file and set it to go there and the same thing happens.

This will be run on win 7, win 8, server 2008, 2008 R2, 2012, and 2012 R2 in case you see any issues with any of that also.

If anyone can give me some input on this, it would be awesome. I have it up at pastebin.


Code: Select all

@ECHO off
 
IF NOT EXIST C:\Test_Ephe GOTO MakeTestDir
:CheckInput
IF NOT EXIST C:\Test_Ephe\Input GOTO MakeInputDir
:CheckOutput
IF NOT EXIST C:\Test_Ephe\Output GOTO MakeOutputDir
 
 
:Variables
setlocal enableextensions enabledelayedexpansion
Set EpheInput=C:\Test_Ephe\Input
Set EpheOutput=C:\Test_Ephe\Output
 
set dcma-batch="%DCMA_HOME%\WEB-INF\classes\META-INF\dcma-batch\dcma-batch.properties"
call:getvalue %dcma-batch% "batch.base_folder" "" baseFolder
::echo %baseFolder%
 
 
:getvalue
FOR /F "eol=; eol=[ tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~4=%%~j
GOTO InputDesignation
 
 
:Move
C:
Cd c:\Test_Ephe
CLS
GOTO Menu
 
 
 
:Menu
CLS
ECHO Welcome to the Ephesoft Command Line testing tool.
ECHO .
ECHO ...............................................
ECHO PRESS the number to select your task, or # to EXIT.
ECHO Your SharedFolders is located at %baseFolder%.
ECHO ...............................................
ECHO .
ECHO 1 - Recostar PDF to Tif conversion
ECHO 2 - Recostar Tiff to pdf conversion
ECHO 3 - Recostar HOCR
ECHO 4 - Ghostscript PDF Optimization command
ECHO 5 - ImageMagick Submenu
ECHO 6 - Tesseract HOCR Command
ECHO 7 - Zxing barcode value (only works on png files)
ECHO 8 - Enter new test file
ECHO 9 -
ECHO 0 - EXIT (closes CMD window)
ECHO .
 
SET /P M=Type 1, 2, 3, 4, 5, 6, 7, 8, 9, or 0 then press ENTER:
 
 
IF %M%==1 GOTO pdf2tif
IF %M%==2 GOTO t2p_menu
IF %M%==3 GOTO recoHOCR
IF %M%==4 GOTO GSOptimize
IF %M%==5 GOTO IMMenu
IF %M%==6 GOTO Tesseract
IF %M%==7 GOTO ZXing
IF %M%==8 GOTO InputDesignation
IF %M%==9 GOTO
IF %M%==0 exit /b
::IF %M%==0 GOTO :eof
 
 
 
:IMMenu
CLS
ECHO Welcome to the Ephesoft Command Line testing tool.
ECHO ImageMagick Submenu
ECHO .
ECHO ...............................................
ECHO PRESS the number to select your task, or # to EXIT.
ECHO ...............................................
ECHO .
ECHO 1 - TIFF to TIFF conversion
ECHO 2 - TIFF to PNG conversion (gray scale)
ECHO 3 - TIFF to PNG conversion (color thumbnail)
ECHO 4 - Colored TIFF to PDF conversion
ECHO 5 - Non-colored TIFF to PDF conversion
ECHO 6 - PDF to TIFF conversion
ECHO 7 - Main Menu
ECHO .
 
SET /P M=Type 1, 2, 3, 4, 5, 6, or 7 then press ENTER:
IF %M%==1 GOTO IMtiff2tiff
IF %M%==2 GOTO IMtiff2pngG
IF %M%==3 GOTO IMtiff2pngT
IF %M%==4 GOTO IMCtiff2pdf
IF %M%==5 GOTO IMNCtiff2pdf
IF %M%==6 GOTO IMpdf2tiff
IF %M%==7 GOTO MENU
cls
 
       
:InputDesignation
::CLS
ECHO Please place your input file into the c:\Test_Ephe\Input directory now.   
ECHO .
ECHO .
ECHO Only after the file is placed in the input directory should you continue.
ECHO .
ECHO .
ECHO Please designate your input filename
    set /p inputName=Input Filename:
    if "!inputName!"=="" (
        GOTO no_filename
    ) else (
        GOTO Check_Exists
    )
 
:no_filename
ECHO .
ECHO .
ECHO The filename that you have provided does not exist.
ECHO Please enter a filename that exists inside the c:\Test_Ephe\Input directory
Pause
GOTO InputDesignation
 
 
:Check_Exists
IF EXIST %EpheInput%\%inputName% (
GOTO :MENU
) ELSE (
ECHO .
ECHO ERROR: You must enter a valid filename. Do not include the path.
pause
GOTO InputDesignation
)
 
:pdf2tif
CLS
ECHO Processing %EpheInput%\%inputName%
%RECOSTAR_PATH%\saveimage.exe "%EpheInput%\%inputName%" 70 true
ECHO .
move %EpheInput%\*-00*.tif %Epheoutput%
ECHO .
ECHO Finished with Recostar PDF to tif conversion, press any key to return  to menu
Pause
GOTO MENU
 
 
:tiff2pdf
CLS
%RECOSTAR_PATH%\RecostarPlugin.exe "!!.rsbatch file!!” "!!FPR.rsp!!” pdf 4 "%RECOSTAR_PATH%\RSO2-NET.707″ "%EpheInput%\%inputName%” OFF OFF OFF RECOSTAR_HOCR
ECHO .
ECHO .
ECHO Finished with Recostar tiff to PDF conversion, press any key to return  to menu
Pause
GOTO MENU
 
 
:recoHOCR
CLS
%RECOSTAR_PATH%\RecostarPlugin.exe "!!.rsbatch file!!” "!!FPR.rsp!!” bd 4 "%RECOSTAR_PATH%\RSO2-NET.707″ _HOCR.xml ON OFF OFF RECOSTAR_HOCR
ECHO .
ECHO .
ECHO Finished with Recostar HOCR, press any key to return  to menu
Pause
GOTO MENU
 
 
:GSOptimize
CLS
%GHOSTSCRIPT_HOME%\gswin64c.exe -q -dNODISPLAY -P- -dSAFER -dDELAYSAFER — %GHOSTSCRIPT_HOME%\..\pdfopt.ps "%EpheInput%\%inputName%" "%EpheOutput%\%inputName%"
ECHO .
ECHO .
ECHO Finished with Ghostscript PDF Optimization
ECHO .
Pause
GOTO MENU
 
 
:Tesseract
CLS
%DCMA_HOME%\native\Tesseract-OCR\TesseractConsole.exe "%EpheInput%\%inputName%" "%EpheOutput%\%inputName%" "-l eng" +"%DCMA_HOME%\native\Tesseract-OCR\hocr.txt"
ECHO .
ECHO .
ECHO Finished with Tesseract HOCR extraction, press any key to return  to menu
Pause
GOTO MENU
 
 
:ZXing
CLS
rem must be png file, not tiff
java -cp zxing-1.6.0.jar;.; com.google.zxing.client.j2se.CommandLineRunner "%EpheInput%\%inputName%"
ECHO .
ECHO .
ECHO Finished with Zxing barcode extraction, press any key to return  to menu
Pause
GOTO MENU
 
 
 
 
:IMtiff2tiff
CLS
%IM4JAVA_TOOLPATH%\Convert.exe  -limit area 100mb "%EpheInput%\%inputName%” -compress LZW "%EpheOutput%\%inputName%-%04d.tif”
ECHO ImageMagick has processed this file successfully
Pause
GOTO MENU
 
 
:IMtiff2pngG
CLS
%IM4JAVA_TOOLPATH%\Convert.exe  "%EpheInput%\%inputName%” -colorspace gray -alpha off "%EpheOutput%\%inputName%.png”
ECHO ImageMagick has processed this file successfully
Pause
GOTO MENU
 
 
:IMtiff2pngT
CLS
%IM4JAVA_TOOLPATH%\Convert.exe   "%EpheInput%\%inputName%” -colorspace rgb -thumbnail 200×150 "%EpheOutput%\%inputName%_displayThumb.png”
ECHO ImageMagick has processed this file successfully
Pause
GOTO MENU
 
 
:IMCtiff2pdf
CLS
%IM4JAVA_TOOLPATH%\Convert.exe   "%EpheInput%\%inputName%" -quality 100.0 -compress LZW "%EpheOutput%\%inputName%.pdf"
ECHO ImageMagick has processed this file successfully
Pause
GOTO MENU
 
 
:IMNCtiff2pdf
CLS
%IM4JAVA_TOOLPATH%\Convert.exe   "%EpheInput%\%inputName%" -quality 100.0 -monochrome -compress LZW "%EpheOutput%\%inputName%.pdf"
ECHO ImageMagick has processed this file successfully
Pause
GOTO MENU
 
 
:IMpdf2tiff
CLS
%IM4JAVA_TOOLPATH%\Convert.exe -limit area 100mb "%EpheInput%\%inputName%” -compress LZW "%EpheOutput%\%inputName%-%04d.tif”
ECHO ImageMagick has processed this file successfully
Pause
GOTO MENU
 
 
:MakeTestDir
Mkdir C:\Test_Ephe
GOTO CheckInput
 
:MakeInputDir
Mkdir c:\Test_Ephe\Input
GOTO CheckOutput
 
:MakeOutputDir
Mkdir c:\Test_Ephe\Output
GOTO Variables

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: not exiting on exit selection until second time

#2 Post by foxidrive » 16 Dec 2014 23:26

I only looked at this part but the quotes protect the input from various typos etc and the final goto will catch every invalid input.


Code: Select all

IF "%M%"=="1" GOTO pdf2tif
IF "%M%"=="2" GOTO t2p_menu
IF "%M%"=="3" GOTO recoHOCR
IF "%M%"=="4" GOTO GSOptimize
IF "%M%"=="5" GOTO IMMenu
IF "%M%"=="6" GOTO Tesseract
IF "%M%"=="7" GOTO ZXing
IF "%M%"=="8" GOTO InputDesignation
IF "%M%"=="9" GOTO
IF "%M%"=="0" exit /b
GOTO :eof


All your OS's have the choice command I think, and that could be easier for your users to drive.

JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Re: not exiting on exit selection until second time

#3 Post by JDAIII » 17 Dec 2014 10:13

Thanks for the tip, tried adding it, didn't help. I tried it with both GOTO :eof and exit /b. I can see that instead of exiting, it looks like it is going to :getvalue which in turn sends it to :InputDesignation. Why would it go to :getvalue when I'm telling it to go to :eof? Starting to miss bash already.

Anyone have any other ideas?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#4 Post by Squashman » 17 Dec 2014 14:34

I am not understanding why you have this CALL command in your code? The call never has any terminating EXIT or GOTO :EOF. But why have a call to that label when it is just the next set of code anyways.

Code: Select all

call:getvalue %dcma-batch% "batch.base_folder" "" baseFolder
::echo %baseFolder%
 
 
:getvalue
FOR /F "eol=; eol=[ tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~4=%%~j
GOTO InputDesignation

JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Re: not exiting on exit selection until second time

#5 Post by JDAIII » 17 Dec 2014 14:41

I am calling it because I wanted to reuse this. This is just the beginning of the tool and I would prefer not to rewrite things and reuse where possible. I guess that I don't have to but it's been a while since I played with batch files so if there is a simpler way, please share.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#6 Post by Squashman » 17 Dec 2014 14:44

I hate to say this but this is just horrible coding.
At the top of your script.

Code: Select all

IF NOT EXIST C:\Test_Ephe GOTO MakeTestDir
:CheckInput
IF NOT EXIST C:\Test_Ephe\Input GOTO MakeInputDir
:CheckOutput
IF NOT EXIST C:\Test_Ephe\Output GOTO MakeOutputDir

At the bottom you have this.

Code: Select all

:MakeTestDir
Mkdir C:\Test_Ephe
GOTO CheckInput
 
:MakeInputDir
Mkdir c:\Test_Ephe\Input
GOTO CheckOutput
 
:MakeOutputDir
Mkdir c:\Test_Ephe\Output
GOTO Variables

There is NO need for the GOTO. Just do the make directory instead of using a GOTO for the make directory.

Code: Select all

IF NOT EXIST C:\Test_Ephe Mkdir C:\Test_Ephe
IF NOT EXIST C:\Test_Ephe\Input Mkdir c:\Test_Ephe\Input
IF NOT EXIST C:\Test_Ephe\Output Mkdir c:\Test_Ephe\Output


Or you could even do this.

Code: Select all

mkdir C:\Test_Ephe Mkdir 2>nul
mkdir C:\Test_Ephe\Input 2>nul
Mkdir c:\Test_Ephe\Output 2>nul

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#7 Post by Squashman » 17 Dec 2014 14:46

JDAIII wrote:I am calling it because I wanted to reuse this. This is just the beginning of the tool and I would prefer not to rewrite things and reuse where possible. I guess that I don't have to but it's been a while since I played with batch files so if there is a simpler way, please share.

I guess you don't understand how to use the CALL. If you CALL a LABEL (subroutine) at some point you have to return to the main environment. So you need to use an EXIT /B or GOTO :EOF to get out of that subroutine.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#8 Post by Squashman » 17 Dec 2014 14:59

And because you are not returning from that CALL, the first time it gets to an EXIT /B or GOTO :EOF it returns to the last command that was executed by the CALL and then your code does a GOTO Inputdesignation.

JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Re: not exiting on exit selection until second time

#9 Post by JDAIII » 17 Dec 2014 15:07

Squashman wrote:Or you could even do this.

Code: Select all

mkdir C:\Test_Ephe Mkdir 2>nul
mkdir C:\Test_Ephe\Input 2>nul
Mkdir c:\Test_Ephe\Output 2>nul



Thanks Squashman, I went ahead and went this route. It's been 15 years since I touched a .bat file besides running basic commands.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#10 Post by Squashman » 17 Dec 2014 15:17

Is anyone using this batch file with Windows XP, Windows 2000?
If not then you should consider changing your Menu prompts to use the CHOICE command.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#11 Post by Squashman » 17 Dec 2014 15:18

Have you tested your batch file with files that have spaces in the file names?

JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Re: not exiting on exit selection until second time

#12 Post by JDAIII » 17 Dec 2014 16:44

I have tested it with files with spaces in their filenames. They work.

I think that we are done with this specific issue as it was a matter of closing the call that was giving me the issue. I have and will have other issues, but I'll post anew should I exhaust all possible routes to resolution.

And nobody will be using on anything older than server2008 R2 or win 7. Most of the servers on which this would be used are server OS' and from the last 4 years.

Thanks again.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#13 Post by Squashman » 17 Dec 2014 17:12

Pretty sure your Check_Exists routine will not work with spaces in the file name.

JDAIII
Posts: 9
Joined: 16 Dec 2014 18:28

Re: not exiting on exit selection until second time

#14 Post by JDAIII » 17 Dec 2014 17:18

Seems to have worked. I even get the expected output files from the commands that I run.

c:\Tools>test
========================================
========================================
Please place your input file into the c:\Test_Ephe\Input directory now.
.
.
Only after the file is placed in the input directory should you continue.
.
.
Please designate your input filename
Input Filename: test - Copy.pdf
Welcome to the Ephesoft Command Line testing tool.
.
...............................................
PRESS the number to select your task, or # to EXIT.
Your SharedFolders is located at C:\\Ephesoft\\SharedFolders.
...............................................
.
.1 - Recostar PDF to Tif conversion
.2 - Recostar Tiff to pdf conversion
.3 - Recostar HOCR
.4 - Recostar Extraction
.5 - Ghostscript PDF Optimization command
.6 - GHOSTSCRIPT PDF TO TIFF CONVERSION
.7 - Tesseract HOCR Command
.8 - Zxing barcode value (only works on png files)
.9 - Enter new test file
.0 - ImageMagick Submenu
.x - EXIT (lower case)
.
Type 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, or x then press ENTER:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: not exiting on exit selection until second time

#15 Post by Squashman » 17 Dec 2014 17:45

It works only because you are branching with a GOTO.

But run this code and input a file name with spaces.

Code: Select all

@ECHO off

Set EpheInput=C:\Test_Ephe\Input

ECHO Please place your input file into the c:\Test_Ephe\Input directory now.   
ECHO Only after the file is placed in the input directory should you continue.
ECHO Please designate your input filename
set /p inputName=Input Filename:

::Check_Exists
IF EXIST %EpheInput%\%inputName% (
   echo FILE EXISTS
   pause
) ELSE (
   ECHO .
   ECHO ERROR: You must enter a valid filename. Do not include the path.
   pause
   GOTO :EOF
)


Then run this code and input a file name with spaces.

Code: Select all

@ECHO off

Set EpheInput=C:\Test_Ephe\Input

ECHO Please place your input file into the c:\Test_Ephe\Input directory now.   
ECHO Only after the file is placed in the input directory should you continue.
ECHO Please designate your input filename
set /p inputName=Input Filename:

::Check_Exists
IF NOT EXIST %EpheInput%\%inputName% (
   echo FILE DOES NOT EXIST
   pause
) ELSE (
   ECHO .
   ECHO You entered a valid filename.
   pause
   GOTO :EOF
)


Then go back and edit the code to put DOUBLE QUOTES around the paths that have spaces and then rerun each set of code.

Code: Select all

IF EXIST "%EpheInput%\%inputName%"
IF NOT EXIST "%EpheInput%\%inputName%"

Post Reply