3

I’m trying to add some optional paramters to a batch file but am having trouble because the SHIFT command does not affect the all-arguments variable %*.

Does anyone know how I can allow for optional batch-file arguments if I have to use %*?

For example, a simple example that merely prints the arguments to the screen with and without a new-line, using an optional argument to determine which:

@echo off
if (%1)==()   goto usage
if (%1)==(/n) goto noeol
goto eol

:eol
  echo %*
  goto :eof

:noeol
  shift
  call showline %*
  goto :eof

:usage
  echo Prints text to the screen
  echo > %0 [/n] TEXT
  echo /n to NOT print a new-line at the end of the text
  goto :eof
4

2 に答える 2

0

私は James のソリューションが好きです。なぜなら、それはユーザーにとって特別なことを何も必要としないからです (これは常に望ましいことです)。しかし、別の方法を考えただけです。引数を引用符で囲み、実行時に削除します。

Print.bat:

@echo off
if (%1)==()   goto usage
if (%1)==(/n) goto noeol
goto eol

:eol
            :: Use %~1 to 
    echo %~1
    goto :eof

:noeol
    shift
    call showline %~1
    goto :eof

:usage
    echo Prints text to the screen
    echo > %0 [/n] TEXT
    echo /n to NOT print a new-line at the end of the text
    goto :eof



Results:

    C:\>print.bat "foo bar baz"
    foo bar baz

    C:\>print.bat /n "foo bar baz"
    foo bar baz
    C:\>
于 2012-10-21T15:37:20.600 に答える