home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch.nt      Fun with Windows NT batch files      68,980 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 67,349 of 68,980   
   probyn@berkeley.edu to All   
   Re: GOTO and non existant label   
   06 Dec 18 10:04:26   
   
   On Tuesday, December 4, 2018 at 1:15:24 PM UTC-8, JJ wrote:   
   > How to prevent the batch file from abruptly terminated if `goto` is executed   
   > with a non existing label?   
   >    
   > I know that I can simply refactor the code, but it's a last resort.   
   >    
   > e.g.   
   >    
   > set /p "num=input task number: "   
   > goto task%num%   
   > rem this line is never executed if label doesn't exist   
   > echo invalid task number   
   > goto :eof   
   >    
   > :task1   
   > echo task1...   
   > goto next   
   >    
   > :task2   
   > echo task2...   
   > goto next   
   >    
   > :next   
   > echo further tasks...   
      
   (((screen capture best viewed with fixed-width font)))   
   File=testlabel.cmd   
      
   Line   ····.····1····.····2····.····3·   
   Â·Â·.····4····.····5····.   
   00001. @echo off   
   00002.  if "%1"=="" goto :ERROR   
   00003.  findstr /b /c:":%1" %~f0 >nul   
   00004.  if %errorlevel% gtr 0 goto :ERROR   
   00005.   
   00006. :func1   
   00007.  goto :VALID   
   00008.   
   00009. :func2   
   00010.  goto :VALID   
   00011.   
   00012. :exec3   
   00013.  goto :VALID   
   00014.   
   00015. :VALID   
   00016.  echo/%1: Valid label....&pause>nul   
   00017.  goto :EXIT   
   00018.   
   00019. :ERROR   
   00020.  echo/OOPS^^! Label %1 not found.   
   00021.  pause>nul   
   00022.   
   00023. :EXIT   
   00024.  goto :EOF   
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
   â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â   
   €â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€   
   ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€   
      
   C:\Users\JoeUser\temp: testlabel   
   OOPS! Label  not found.   
      
   C:\Users\JoeUser\temp: testlabel xyz   
   OOPS! Label xyz not found.   
      
   C:\Users\JoeUser\temp: testlabel VALID   
   VALID: Valid label....   
      
   C:\Users\JoeUser\temp: testlabel func2   
   func2: Valid label....   
      
   C:\Users\JoeUser\temp:   
   (((end screen capture Win 10)))   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca