如何在调用批处理文件时检查参数是否定义?

时间:2022-04-11 07:08:54

I'm trying to use the following validation logic in a batch file but the "usage" block never executes even when no parameter is supplied to the batch file.

我正在尝试在批处理文件中使用以下验证逻辑,但是即使没有向批处理文件提供参数,“usage”块也不会执行。

if ("%1"=="") goto usage

@echo This should not execute

@echo Done.
goto :eof

:usage
@echo Usage: %0 <EnvironmentName>
exit 1

What am I doing wrong?

我做错了什么?

6 个解决方案

#1


116  

The check for whether a commandline argument has been set can be [%1]==[], but, as Dave Costa points out, "%1"=="" will also work.

检查命令行参数是否已经设置,可以是[%1]=[],但是,正如Dave Costa指出的,“%1”=“”也可以。

I also fixed a syntax error in the usage echo to escape the greater-than and less-than signs. In addition, the exit needs a /B argument otherwise CMD.exe will quit.

我还修复了用法echo中的语法错误,以避免大于号和小于号。此外,退出需要a /B参数,否则CMD。exe将辞职。

@echo off

if [%1]==[] goto usage
@echo This should not execute
@echo Done.
goto :eof
:usage
@echo Usage: %0 ^<EnvironmentName^>
exit /B 1

#2


13  

A more-advanced example:

⍟ unlimited arguments.

⍟无限参数。

⍟ exist on file system (either file or directory?) or a generic string.

⍟存在于文件系统(文件或目录?)或一个通用的字符串。

⍟ specify if is a file

如果是一个文件⍟指定

⍟ specify is a directory

⍟指定一个目录

no extensions, would work in legacy scripts!

⍟没有扩展,会遗留工作脚本!

minimal code ☺

☺⍟最少的代码

@echo off

:loop
      ::-------------------------- has argument ?
      if ["%~1"]==[""] (
        echo done.
        goto end
      )
      ::-------------------------- argument exist ?
      if not exist %~s1 (
        echo not exist
      ) else (
        echo exist
        if exist %~s1\NUL (
          echo is a directory
        ) else (
          echo is a file
        )
      )
      ::--------------------------
      shift
      goto loop


:end

pause

✨ other stuff..✨

. .✨✨其他东西

■ in %~1 - the ~ removes any wrapping " or '.

■在% ~ 1 - ~删除任何包装”或“。

■ in %~s1 - the s makes the path be DOS 8.3 naming, which is a nice trick to avoid spaces in file-name while checking stuff (and this way no need to wrap the resource with more "s.

■% ~ s1 - s使DOS 8.3命名的道路,这是一个很好的技巧,以避免在文件名称空间,而检查的东西(这方法不需要包装资源有更多”年代。

■ the ["%~1"]==[""] "can not be sure" if the argument is a file/directory or just a generic string yet, so instead the expression uses brackets and the original unmodified %1 (just without the " wrapping, if any..)

■(“% ~ 1”)= =["]“不能确定”如果参数是一个文件/目录或只是一个通用的字符串,所以表达式使用括号和原始的未修改的% 1(就没有“包装,如果任何. .)

if there were no arguments of if we've used shift and the arg-list pointer has passed the last one, the expression will be evaluated to [""]==[""].

如果没有参数,如果我们使用了shift,而arg-list指针已经通过了最后一个,那么表达式将被计算为["]==["]。

■ this is as much specific you can be without using more tricks (it would work even in windows-95's batch-scripts...)

■这是具体的你一样可以不使用更多的技巧(它将工作即使在windows - 95的批处理脚本…)

■ execution examples

save it as identifier.cmd

将其保存为identifier.cmd

it can identify an unlimited arguments (normally you are limited to %1-%9), just remember to wrap the arguments with inverted-commas, or use 8.3 naming, or drag&drop them over (it automatically does either of above).

它可以识别一个无限制的参数(通常您被限制为%1-%9),只需要记住用反向逗号包装参数,或者使用8.3命名,或者拖放它们(它会自动执行上面的任何一个)。


this allows you to run the following commands:

这允许您运行以下命令:

identifier.cmd c:\windows and to get

⓵标识符。cmd c: windows和get

exist
is a directory
done

identifier.cmd "c:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE" and to get

⓶标识符。\程序文件(x86)\Microsoft Office\OFFICE11\WINWORD。EXE”和

exist
is a file
done

⓷ and multiple arguments (of course this is the whole-deal..)

⓷和多个参数(当然这是一桩生意. .)

identifier.cmd c:\windows\system32 c:\hiberfil.sys "c:\pagefile.sys" hello-world

标识符。cmd c:\ windows \ system32系统c:\ hiberfil。sys”c:\页面文件。sys”hello world

and to get

并得到

exist
is a directory
exist
is a file
exist
is a file
not exist
done.

naturally it can be a lot more complex, but nice examples should always be simple and minimal. :)

当然,它可能要复杂得多,但是好的示例应该始终是简单和最小的。:)

Hope it helps anyone :)

published here:CMD Ninja - Unlimited Arguments Processing, Identifying If Exist In File-System, Identifying If File Or Directory

发布在这里:CMD Ninja -无限制参数处理,标识文件系统中是否存在,标识文件或目录

and here is a working example that takes any amount of APK files (Android apps) and installs them on your device via debug-console (ADB.exe): Make The Previous Post A Mass APK Installer That Does Not Uses ADB Install-Multi Syntax

这里有一个工作示例,它接受任何数量的APK文件(Android应用程序),并通过调试控制台(ADB.exe)在您的设备上安装它们:使前一篇文章成为不使用ADB安装-多语法的大规模APK安装程序

#3


13  

Get rid of the parentheses.

去掉括号。

Sample batch file:

示例批处理文件:

echo "%1"

if ("%1"=="") echo match1

if "%1"=="" echo match2

Output from running above script:

以上脚本运行输出:

C:\>echo "" 
""

C:\>if ("" == "") echo match1 

C:\>if "" == "" echo match2 
match2

I think it is actually taking the parentheses to be part of the strings and they are being compared.

我认为括号实际上是字符串的一部分它们正在被比较。

#4


5  

IF "%~1"=="" GOTO :Usage

~ will de-quote %1 if %1 itself is quoted.

如果引用%1,则将取消引用%1。

" " will protect from special characters passed. for example calling the script with &ping

"将保护不受特殊字符通过。例如使用&ping调用脚本

#5


2  

IF "%1"=="" GOTO :Continue
.....
.....
:Continue
IF "%1"=="" echo No Parameter given

#6


0  

IF "%1"=="" will fail, all versions of this will fail under certain poison character conditions. Only IF DEFINED or IF NOT DEFINED are safe

如果“%1”=“”将失败,那么在特定的毒药字符条件下,所有版本都将失败。只有定义或未定义时才是安全的

#1


116  

The check for whether a commandline argument has been set can be [%1]==[], but, as Dave Costa points out, "%1"=="" will also work.

检查命令行参数是否已经设置,可以是[%1]=[],但是,正如Dave Costa指出的,“%1”=“”也可以。

I also fixed a syntax error in the usage echo to escape the greater-than and less-than signs. In addition, the exit needs a /B argument otherwise CMD.exe will quit.

我还修复了用法echo中的语法错误,以避免大于号和小于号。此外,退出需要a /B参数,否则CMD。exe将辞职。

@echo off

if [%1]==[] goto usage
@echo This should not execute
@echo Done.
goto :eof
:usage
@echo Usage: %0 ^<EnvironmentName^>
exit /B 1

#2


13  

A more-advanced example:

⍟ unlimited arguments.

⍟无限参数。

⍟ exist on file system (either file or directory?) or a generic string.

⍟存在于文件系统(文件或目录?)或一个通用的字符串。

⍟ specify if is a file

如果是一个文件⍟指定

⍟ specify is a directory

⍟指定一个目录

no extensions, would work in legacy scripts!

⍟没有扩展,会遗留工作脚本!

minimal code ☺

☺⍟最少的代码

@echo off

:loop
      ::-------------------------- has argument ?
      if ["%~1"]==[""] (
        echo done.
        goto end
      )
      ::-------------------------- argument exist ?
      if not exist %~s1 (
        echo not exist
      ) else (
        echo exist
        if exist %~s1\NUL (
          echo is a directory
        ) else (
          echo is a file
        )
      )
      ::--------------------------
      shift
      goto loop


:end

pause

✨ other stuff..✨

. .✨✨其他东西

■ in %~1 - the ~ removes any wrapping " or '.

■在% ~ 1 - ~删除任何包装”或“。

■ in %~s1 - the s makes the path be DOS 8.3 naming, which is a nice trick to avoid spaces in file-name while checking stuff (and this way no need to wrap the resource with more "s.

■% ~ s1 - s使DOS 8.3命名的道路,这是一个很好的技巧,以避免在文件名称空间,而检查的东西(这方法不需要包装资源有更多”年代。

■ the ["%~1"]==[""] "can not be sure" if the argument is a file/directory or just a generic string yet, so instead the expression uses brackets and the original unmodified %1 (just without the " wrapping, if any..)

■(“% ~ 1”)= =["]“不能确定”如果参数是一个文件/目录或只是一个通用的字符串,所以表达式使用括号和原始的未修改的% 1(就没有“包装,如果任何. .)

if there were no arguments of if we've used shift and the arg-list pointer has passed the last one, the expression will be evaluated to [""]==[""].

如果没有参数,如果我们使用了shift,而arg-list指针已经通过了最后一个,那么表达式将被计算为["]==["]。

■ this is as much specific you can be without using more tricks (it would work even in windows-95's batch-scripts...)

■这是具体的你一样可以不使用更多的技巧(它将工作即使在windows - 95的批处理脚本…)

■ execution examples

save it as identifier.cmd

将其保存为identifier.cmd

it can identify an unlimited arguments (normally you are limited to %1-%9), just remember to wrap the arguments with inverted-commas, or use 8.3 naming, or drag&drop them over (it automatically does either of above).

它可以识别一个无限制的参数(通常您被限制为%1-%9),只需要记住用反向逗号包装参数,或者使用8.3命名,或者拖放它们(它会自动执行上面的任何一个)。


this allows you to run the following commands:

这允许您运行以下命令:

identifier.cmd c:\windows and to get

⓵标识符。cmd c: windows和get

exist
is a directory
done

identifier.cmd "c:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE" and to get

⓶标识符。\程序文件(x86)\Microsoft Office\OFFICE11\WINWORD。EXE”和

exist
is a file
done

⓷ and multiple arguments (of course this is the whole-deal..)

⓷和多个参数(当然这是一桩生意. .)

identifier.cmd c:\windows\system32 c:\hiberfil.sys "c:\pagefile.sys" hello-world

标识符。cmd c:\ windows \ system32系统c:\ hiberfil。sys”c:\页面文件。sys”hello world

and to get

并得到

exist
is a directory
exist
is a file
exist
is a file
not exist
done.

naturally it can be a lot more complex, but nice examples should always be simple and minimal. :)

当然,它可能要复杂得多,但是好的示例应该始终是简单和最小的。:)

Hope it helps anyone :)

published here:CMD Ninja - Unlimited Arguments Processing, Identifying If Exist In File-System, Identifying If File Or Directory

发布在这里:CMD Ninja -无限制参数处理,标识文件系统中是否存在,标识文件或目录

and here is a working example that takes any amount of APK files (Android apps) and installs them on your device via debug-console (ADB.exe): Make The Previous Post A Mass APK Installer That Does Not Uses ADB Install-Multi Syntax

这里有一个工作示例,它接受任何数量的APK文件(Android应用程序),并通过调试控制台(ADB.exe)在您的设备上安装它们:使前一篇文章成为不使用ADB安装-多语法的大规模APK安装程序

#3


13  

Get rid of the parentheses.

去掉括号。

Sample batch file:

示例批处理文件:

echo "%1"

if ("%1"=="") echo match1

if "%1"=="" echo match2

Output from running above script:

以上脚本运行输出:

C:\>echo "" 
""

C:\>if ("" == "") echo match1 

C:\>if "" == "" echo match2 
match2

I think it is actually taking the parentheses to be part of the strings and they are being compared.

我认为括号实际上是字符串的一部分它们正在被比较。

#4


5  

IF "%~1"=="" GOTO :Usage

~ will de-quote %1 if %1 itself is quoted.

如果引用%1,则将取消引用%1。

" " will protect from special characters passed. for example calling the script with &ping

"将保护不受特殊字符通过。例如使用&ping调用脚本

#5


2  

IF "%1"=="" GOTO :Continue
.....
.....
:Continue
IF "%1"=="" echo No Parameter given

#6


0  

IF "%1"=="" will fail, all versions of this will fail under certain poison character conditions. Only IF DEFINED or IF NOT DEFINED are safe

如果“%1”=“”将失败,那么在特定的毒药字符条件下,所有版本都将失败。只有定义或未定义时才是安全的