0

I'm writing a script where I want to give some help (calling with /?)

Now I tried with

REM This is myscript.bat
FOR %%A IN (%*) DO (
      rem controllo i parametri
      
      rem ...
         
      if /I "%%A" equ "/?" goto :help
      if /I "%%A" equ "--help" goto :help
      if /I "%%A" equ "-h" goto :help
)

echo stuff

goto :out

:help
echo this is your lifesaver
:out

it works fine with myscript.bat --help' and myscript.bat -h (it prints this is your lifesaver).
If I call it like myscript.bat /? it prints stuff then exits instead of printing this is your lifesaver

DDS
  • 2,340
  • 16
  • 34
  • Please read my answer on [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). It explains in full details how a __string__ comparison is done with Windows command processor using the command __IF__. The string comparison operator is `==` and not `EQU`. – Mofi Mar 18 '22 at 17:12
  • But the main problem is that `/?` is interpreted by command __FOR__ as a wildcard pattern to search in root directory of current drive because of `/` being replaced by Windows I/O functions to ``\`` with the wildcard pattern `?` for a file with a single character as name. There is most likely no file in root of current drive with a name like `a`, `b` or `c` and so `/?` results in doing nothing as no file found of which name can be assigned to the loop variable `A`. See also MS docu about [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file). – Mofi Mar 18 '22 at 17:17
  • 1
    In the future I would suggest learning PowerShell, because batch syntax is objectively awful. This is very easy in posh: `if( $var -eq 'some value' } { DoStuff }`. Don't even get me started on [the voodoo around assigning a command's result to a variable in a batch script](https://stackoverflow.com/questions/59432807/is-there-a-way-to-store-and-retrieve-a-password-in-a-file-from-a-batch-script/59433056#59433056). – codewario Mar 18 '22 at 17:21
  • I suggest to read my answer on [How do you iterate through a subset of the parameters passed to a batch file?](https://stackoverflow.com/a/30003260/3074564) There can be used a __GOTO__ loop with command __SHIFT__ to process all arguments passed to a batch file. – Mofi Mar 18 '22 at 17:23

0 Answers0