如何删除批处理文件中用户提供的输入的尾随和前导空格?
我知道在预定义变量时如何执行此操作。但是,当要求用户输入某种输入时,如何修剪前导和尾随空格?这是我到目前为止所拥有的:
@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
I know how to do this when the variable is pre-defined. However, when asking for the user to enter in some kind of input, how do I trim leading and trailing whitespace? This is what I have so far:
@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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您还可以通过将带有空格的变量作为参数传递到子函数中来去除空格,然后在子函数中只需使用传入的参数再次设置它们。
You can also strip whitespace by passing in those variables with whitespace as parameters into a sub and then inside your sub simply set them again with the passed in parameters.
下面的
:trimAll
子例程:!
、%
、^
等)阅读注释以获取参考和信息。
HTH 和 HNY! ;)
The
:trimAll
subroutine below:!
,%
,^
, etc.)Read comments for references and info.
HTH and HNY! ;)
刚刚想出这个:
Just came up with this:
更新 3:该实现被更可靠的实现所取代。
我发现这里介绍的所有解决方案对我来说都不够完整,并且在某种情况下不起作用。
警告:
似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了
\x01
或\x04
等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用末尾的链接直接下载脚本。实现:https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/trim_var.bat
trim_var.bat:
trim_var .trim_value_right.bat:
echo_var.bat:
test_trim_var.bat:
输出:
优点:
缺点:
\x04
代码字符用作 EOL 字符。UPDATE 3: The implementation is replaced by more reliable one.
I've found that all solution introduced here are not complete enough for me and does not work in one or another case.
CAUTION:
Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like
\x01
or\x04
) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link at the end to directly download the scripts.Implementation: https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/trim_var.bat
trim_var.bat:
trim_var.trim_value_right.bat:
echo_var.bat:
test_trim_var.bat:
Output:
Pros:
Cons:
\x04
code character uses as EOL character.来自 http://www.netikka.net/tsneti/info/tscmd079.htm
用于删除前导空格。
from http://www.netikka.net/tsneti/info/tscmd079.htm
for removing leading spaces.
我是这样做的(暂时开启延迟扩展):
I did it like this (temporarily turning on delayed expansion):
或者
or
这很简单。不带任何参数的
for
认为空格是分隔符;将“*”设置为tokens
参数会导致程序收集字符串中非空格的所有部分,并将它们放入一个新字符串中,并在其中插入自己的空格。This is very simple.
for
without any parameters considers spaces to be delimiters; setting "*" as thetokens
parameter causes the program to gather up all the parts of the string that are not spaces and place them into a new string into which it inserts gaps of its own.我必须创建一个函数。
使用它:
将其添加到批处理文件的底部:
并记住您必须将“SETLOCAL ENABLEDELAYEDEXPANSION”添加到批处理文件的顶部,否则这些都无法正常工作。
I had to create a Function.
Use it as:
Add this to the bottom of your batch file:
And remember you MUST add "SETLOCAL ENABLEDELAYEDEXPANSION" to the top of your batch file or else none of this will work properly.
设置 newVarNoSpaces=%someVarWithSpaces: =%
set newVarNoSpaces=%someVarWithSpaces: =%
我正在使用“修剪右侧空白”,完全适用于我的“Show-Grp-of-” UID.CMD”。 :)
欢迎其他改进意见..^_^
I'm using the "Trim Right Whitespace" exactly working on my "Show-Grp-of-UID.CMD". :)
Other idea for improvement are welcome.. ^_^
下面的解决方案对我来说非常有效。
仅 4 行,适用于大多数(全部?)字符。
解决方案:
测试:
The solution below works very well for me.
Only 4 lines and works with most (all?) characters.
Solution:
Test:
您需要启用延迟扩展。试试这个:
You need to enable delayed expansion. Try this:
一个非常短的&我正在使用的简单解决方案是:
结果:
A very short & easy solution i'm using is this:
Results in:
我想提出一个紧凑的解决方案,使用对函数和“子函数”的引用调用(是的,“批处理”也有指针!):
I'd like to present a compact solution using a call by reference (yes, "batch" has pointers too!) to a function, and a "subfunction":
为了改进 Forumpie 的答案,技巧是在子中使用
%*
(所有参数):编辑: 添加了 TRIM 子例程参数的回显,以提供更多见解
前导和尾随空格,但保留其间的所有空格。
%* 的详细信息:批量参数
To improve on Forumpie's answer, the trick is using
%*
(all params) in the sub:Edit: Added echo of the TRIM subroutines params, to provide more insight
This strips leading and trailing spaces, but keeps all spaces between.
Details of %*: Batch Parameters
这是我的快速而肮脏的解决方案。请注意,如果输入文本包含文件名中禁止的字符,将会产生意外结果。
编辑
为了提供更强大的文本输入字符集,并仍然删除 for in ('echo...') 方法通常留下的单个尾随空格,这是一个替代方案:
This was my quick-and-dirty solution. Note that it will have unexpected results if the input text has characters that are forbidden in file names.
EDIT
To provide a more robust text input character set and still remove the single trailing space that would normally be left by the for in ('echo...') approach, this is an alternative: