我的设置如下:
我正在读取一个.ini
文件,里面的变量看起来像var=value
for /f "tokens=1* delims==" %%i in (file.ini) do (
set %%i=%%j
)
我有一个循环检查变量是否具有值(已设置)的标准
for %%p in (var1 var2 var3..) do (
if not defined %%p (
call :error "%%p has not been set"
goto exit
) else (
echo %%p=!%%p!
)
)
我可以在其中添加什么来检查尾随空格,就像用户在设置一个值后点击空格一样,由于变量表示路径而什么都不行,这会弄乱事物的流程。
答案 0 :(得分:1)
我有一个函数/子例程:Trim
,我呼吁此类问题。
%*
将保留所有传递的参数,但会修饰前导/后缀空格。 %Trim%
引用的var。:: Q:\Test\2018\07\22\SO_51466512.cmd
::
@Echo off&SetLocal EnableDelayedExpansion
:: generating a file.ini with leading/trailing spaces in the vars
( echo var1= test blah
echo var2= .
) >file.ini
for /f "tokens=1* delims==" %%i in (file.ini) do (
set %%i=%%j
)
for %%p in (var1 var2 var3) do (
if not defined %%p (
call :error "%%p has not been set"
exit /B 1
) else (
Set "Trim=%%p"
Call :Trim !%%p!
echo [%%p=!%%p!]
)
)
Goto :Eof
:Trim text to be trimmed text .
:: Func: If the variable Trim is defined, the content is interpreted
:: as the variable Name to receive the trimmed text,
:: If not the trimmed text is stored in the var Trim.
:: Args: all are treated as text to be trimmed
::
If defined Trim (Set "%Trim%=%*") Else (set "Trim=%*")
goto :eof
:error
Echo [%date% %time%] Error: %*
示例输出:
> Q:\Test\2018\07\22\SO_51466512.cmd
[var1=test blah]
[var2=.]
[2018-07-22 17:59:13,17] Error: "var3 has not been set"
免责声明:如果要修剪的变量包含 有毒 个字符<|>&