我需要测试是否设置了一个变量。我尝试了几种技术,但每当%1被引号包围时,它们似乎都失败了,例如%1是“c:\一些带空格的路径”。

IF NOT %1 GOTO MyLabel // This is invalid syntax
IF "%1" == "" GOTO MyLabel // Works unless %1 has double quotes which fatally kills bat execution
IF %1 == GOTO MyLabel // Gives an unexpected GOTO error.

根据这个网站,这些是支持的IF语法类型。所以,我不知道该怎么做。

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

更新:在2020-10-25,我更新了接受的答案,从使用括号到使用波浪号。每个人都说波浪更好,因为它更安全。我有点撕裂,因为波浪线看起来更复杂,不太清楚它的目的是什么,但尽管如此,我改变了它。


当前回答

你可以使用:

IF "%~1" == "" GOTO MyLabel

去掉外部的引号。一般来说,这是一种比使用方括号更可靠的方法,因为即使变量中有空格,它也能工作。

其他回答

This way looks correct:

if "%~1" == "" if [%1] == [] echo Argument 1 is really empty.

First filter (if "%~1" == "") из safe way to detect argument is empty or "".
Second filter (if [%1] == []) will skip "" argument.
Remember we have two kind of empty arguments : really empty (nothing) and empty quotes "".
We have to detect both.

Next code takes all arguments "as is" in variable args:

:GetArgs
set args=%1
:ParseArgs
shift /1
if "%~1" == "" if [%1] == [] goto :DoneArgs
set args=%args% %1
goto :ParseArgs
:DoneArgs
if not defined args echo No arguments
if defined args echo Aguments are: %args%

This code correctly process "normal" arguments (simple words, "multiword phrases" or "") with balanced quotes.
Quoted arguments can even contain special chars like "a & b".
But it is still can fail with "abnormal" expressions with unbalanced quotes (do not use them).

我根据这里的答案创建了这个小批处理脚本,因为有很多有效的答案。只要遵循相同的格式,可以随意添加:

REM Parameter-testing

Setlocal EnableDelayedExpansion EnableExtensions

IF NOT "%~1"=="" (echo Percent Tilde 1 failed with quotes) ELSE (echo SUCCESS)
IF NOT [%~1]==[] (echo Percent Tilde 1 failed with brackets) ELSE (echo SUCCESS)
IF NOT  "%1"=="" (echo Quotes one failed) ELSE (echo SUCCESS)
IF NOT [%1]==[] (echo Brackets one failed) ELSE (echo SUCCESS)
IF NOT "%1."=="." (echo Appended dot quotes one failed) ELSE (echo SUCCESS)
IF NOT [%1.]==[.] (echo Appended dot brackets one failed) ELSE (echo SUCCESS)

pause

从IF /?:

如果命令扩展是启用If 变更如下: IF [/I] string1 compare-op string2命令 IF CMDEXTVERSION编号命令 IF定义变量命令 ...... DEFINED条件刚好 像EXISTS一样,除了它需要一个 环境变量名称和返回值 如果环境变量为,则为 定义的。

你可以使用:

IF "%~1" == "" GOTO MyLabel

去掉外部的引号。一般来说,这是一种比使用方括号更可靠的方法,因为即使变量中有空格,它也能工作。

我在不到一个月的时间里就被录取了(尽管这是8年前的要求)……我希望他/她现在已经超越批处理文件了。: -) 我以前经常这么做。但我不确定最终目标是什么。如果他/她像我一样懒,我的go.bat就能解决这些问题。(见下文) 但是,1,如果您直接将输入作为命令使用,则OP中的命令可能无效。也就是说,

"C:/Users/Me"

是无效命令(如果您在不同的驱动器上,则过去是无效命令)。你得把它分成两部分。

C:
cd /Users/Me

第二,'defined'和'undefined'是什么意思?GIGO。我使用默认值来捕捉错误。如果输入没有被捕获,它将被删除以帮助(或默认命令)。所以,没有输入不是错误。您可以尝试cd到输入并捕捉错误(如果有的话)。(好的,使用go "downloads(只有一个括号)被DOS捕获。(严厉的!)

cd "%1"
if %errorlevel% neq 0 goto :error

并且,3,引号只需要围绕路径,而不是命令。也就是说,

"cd C:\Users" 

是不好的(或者在过去习惯了),除非你在不同的驱动器上。

cd "\Users" 

是功能。

cd "\Users\Dr Whos infinite storage space"

如果你的路径上有空格,就有用。

@REM go.bat
@REM The @ sigh prevents echo on the current command
@REM The echo on/off turns on/off the echo command. Turn on for debugging
@REM You can't see this.
@echo off
if "help" == "%1" goto :help

if "c" == "%1" C:
if "c" == "%1" goto :done

if "d" == "%1" D:
if "d" == "%1" goto :done

if "home"=="%1" %homedrive%
if "home"=="%1" cd %homepath%
if "home"=="%1" if %errorlevel% neq 0 goto :error
if "home"=="%1" goto :done

if "docs" == "%1" goto :docs

@REM goto :help
echo Default command
cd %1
if %errorlevel% neq 0 goto :error
goto :done

:help
echo "Type go and a code for a location/directory
echo For example
echo go D
echo will change disks (D:)
echo go home
echo will change directories to the users home directory (%homepath%)
echo go pictures
echo will change directories to %homepath%\pictures
echo Notes
echo @ sigh prevents echo on the current command
echo The echo on/off turns on/off the echo command. Turn on for debugging
echo Paths (only) with folder names with spaces need to be inclosed in         quotes (not the ommand)
goto :done

:docs
echo executing "%homedrive%%homepath%\Documents"
%homedrive%
cd "%homepath%\Documents"\test error\
if %errorlevel% neq 0 goto :error
goto :done

:error
echo Error: Input (%1 %2 %3 %4 %5 %6 %7 %8 %9) or command is invalid
echo go help for help
goto :done

:done