如何删除batch file中用户提供的input的尾随和前导空格?
我知道如何做到这一点,当variables是预先定义的。 但是,当要求用户input某种input时,如何修剪首尾空白? 这是我迄今为止:
@echo off set /p input=: echo. The input is %input% before ::trim left whitespace for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a ::trim right whitespace (up to 100 spaces at the end) for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1! echo. The input is %input% after pause
您需要启用延迟扩展。 尝试这个:
@echo off setlocal enabledelayedexpansion :blah set /p input=: echo."%input%" for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1! echo."%input%" pause goto blah
下面的解决scheme对我来说工作得非常好。
只有4行,并与大多数(所有?)字符 。
解:
:Trim SetLocal EnableDelayedExpansion set Params=%* for /f "tokens=1*" %%a in ("!Params!") do EndLocal & set %1=%%b exit /b
testing:
@echo off call :Test1 & call :Test2 & call :Test3 & exit /b :Trim SetLocal EnableDelayedExpansion set Params=%* for /f "tokens=1*" %%a in ("!Params!") do EndLocal & set %1=%%b exit /b :Test1 set Value= abc set Expected=abc call :Trim Actual %Value% if "%Expected%" == "%Actual%" (echo Test1 passed) else (echo Test1 failed) exit /b :Test2 SetLocal EnableDelayedExpansion set Value= a \ / : * ? " ' < > | ` ~ @ # $ [ ] & ( ) + - _ = z set Expected=a \ / : * ? " ' < > | ` ~ @ # $ [ ] & ( ) + - _ = z call :Trim Actual !Value! if !Expected! == !Actual! (echo Test2 passed) else (echo Test2 failed) exit /b :Test3 set /p Value="Enter string to trim: " %=% echo Before: [%Value%] call :Trim Value %Value% echo After : [%Value%]
我使用的一个非常简短的解决scheme是这样的:
@ECHO OFF SET /p NAME=- NAME ? ECHO "%NAME%" CALL :TRIM %NAME% NAME ECHO "%NAME%" PAUSE :TRIM SET %2=%1 GOTO :EOF
结果是:
- NAME ? my_name " my_name " "my_name"
我想提出一个紧凑的解决scheme,使用一个引用调用(是的,“批量”也有指针!)到一个函数和一个“子函数”:
@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET /p NAME=- NAME ? ECHO "%NAME%" CALL :TRIM NAME ECHO "%NAME%" GOTO :EOF :TRIM SetLocal EnableDelayedExpansion Call :TRIMSUB %%%1%% EndLocal & set %1=%tempvar% GOTO :EOF :TRIMSUB set tempvar=%* GOTO :EOF
为了改进Forumpie的答案,诀窍是在子文件中使用%*
(所有参数):
编辑:增加TRIM子程序params的回声,以提供更多的见解
@ECHO OFF SET /p NAME=- NAME ? ECHO "%NAME%" CALL :TRIM %NAME% SET NAME=%TRIMRESULT% ECHO "%NAME%" GOTO :EOF :TRIM echo "%1" echo "%2" echo "%3" echo "%4" SET TRIMRESULT=%* GOTO :EOF
这剥去了前导空间和尾随空间,但保留了所有空间。
" 1 2 3 4 " "1 2 3 4"
%*的详细信息: 批处理参数
@echo off & setlocal enableextensions rem enabledelayedexpansion set S= This is a test echo %S%. for /f "tokens=* delims= " %%a in ('echo %S%') do set S=%%a echo %S%. endlocal & goto :EOF
从http://www.netikka.net/tsneti/info/tscmd079.htm
去除领先空间。
我是这样做的(暂时开启延期扩张):
... sqlcmd -b -S %COMPUTERNAME% -E -d %DBNAME% -Q "SELECT label from document WHERE label = '%DOCID%';" -h-1 -o Result.txt if errorlevel 1 goto INVALID :: Read SQL result and trim trailing whitespace SET /P ITEM=<Result.txt @echo ITEM is %ITEM%. setlocal enabledelayedexpansion for /l %%a in (1,1,100) do if "!ITEM:~-1!"==" " set ITEM=!ITEM:~0,-1! setlocal disabledelayedexpansion @echo Var ITEM=%ITEM% now has trailing spaces trimmed. ....
@echo off setlocal EnableDelayedExpansion set S= This is a test echo %S%. for /f "tokens=* delims= " %%a in ('echo %S%') do (set b=%%a & set b=!b: =! & echo !b!) endlocal & goto :EOF
要么
@echo off setlocal EnableDelayedExpansion set S= This is a test echo %S%. for /f "tokens=* delims= " %%a in ('echo %S%') do (set b=%%a & set b=!b: =_! & echo !b!) endlocal & goto :EOF
你也可以通过将这些带有空格的variables作为参数传入一个子文件然后在你的子文件夹中简单地设置它们再用传入的参数来去除空格。
@echo off set "a= apple " set "b= banana " echo [%a%] echo [%b%] call :Strip %a% %b% pause goto :EOF :Strip set a=%1 set b=%2 echo [%a%] echo [%b%] ---------------- Results.... [ apple ] [ banana ] [apple] [banana] Press any key to continue . . .
我正在使用“修剪右空白”正好在我的“Show-Grp-of-UID.CMD”上工作。 :)其他改进的想法,欢迎.. ^ _ ^
设置newVarNoSpaces =%someVarWithSpaces:=%