从 Windows 批处理文件中的字符串中删除第一个和最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12074510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove first and last character from a String in a Windows Batch file
提问by Metalhead89
I have the following string inside my Windows batch file:
我的 Windows 批处理文件中有以下字符串:
"-String"
The string also contains the twoe quotation marks at the beginning and at the end of the string, so as it is written above.
该字符串还在字符串的开头和结尾包含两个引号,就像上面写的那样。
I want to strip the first and last characters so that I get the following string:
我想去掉第一个和最后一个字符,以便得到以下字符串:
-String
I tried this:
我试过这个:
set currentParameter="-String"
echo %currentParameter:~1,-1%
This prints out the string as it should be:
这将打印出应该是的字符串:
-String
But when I try to store the edited string like this, it fails:
但是当我尝试像这样存储编辑过的字符串时,它失败了:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%
Nothing gets printed out. What do I do wrong?
什么都不会打印出来。我做错了什么?
This really is strange. When I remove the characters like this it works:
这真的很奇怪。当我删除这样的字符时,它会起作用:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%
it prints out:
它打印出:
-String
But actually my batch is a bit more complicated and there it does not work. I will show what I programmed:
但实际上我的批次有点复杂,它不起作用。我将展示我编程的内容:
@echo off
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=%currentParameter:~1,-1%
echo String WITH stripping characters: %currentParameter%
echo .
)
exit /b
:end
And the output is:
输出是:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters:
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: ~1,-1
.
But what i want is:
但我想要的是:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters: -String
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: -String2
.
回答by Stach
You are modyfing a variable inside a parenthesized block. Watch out - the new value will not be used within the same block (unless you delimit the variable with ! instead of % - and running in the enabledelayedexpansion mode). Or just extract the couple of lines into another sub-function, using a plain sequence of lines insted of ( )
您正在修改括号内的变量。注意 - 新值不会在同一个块中使用(除非你用 ! 而不是 % 分隔变量 - 并在 enabledelayedexpansion 模式下运行)。或者只是使用 ( ) 插入的简单行序列将这几行提取到另一个子函数中
greets, Stach
问候,斯塔赫
回答by phani
hope will help you. echo String WITHOUT stripping characters: %%I
希望能帮到你。不带剥离字符的回显字符串:%%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter!
echo .
It might work. Try this once.
它可能会起作用。试试这个。
回答by Alex Lindstrom
This script takes advantage of ENABLEDELAYEDEXPANSION. If you don't know, batch scripts execute for and if commands all in one; hence if you do:
此脚本利用了 ENABLEDELAYEDEXPANSION。如果您不知道,批处理脚本将执行 for 和 if 命令全部合二为一;因此,如果你这样做:
if true==true (
@echo off
set testvalue=123
echo %testvalue%
pause >NUL
)
You wont output anything, because when echo %testvalue% is executed, it has not recognized the testvalue has been changed. Using delayedexapnsion allows the script to read that value as it is now, and forget the problem I stated before. You use it just like %testvalue%, but you may do !testvalue! to fix this:
你不会输出任何东西,因为当 echo %testvalue% 被执行时,它没有识别出测试值已经改变。使用 delayexapnsion 允许脚本读取现在的值,并忘记我之前提到的问题。您可以像 %testvalue% 一样使用它,但您可以使用 !testvalue! 解决这个问题:
if true==true (
@echo off
set testvalue=123
echo !testvalue!
pause >NUL
)
- Would echo 123.
- 会回显 123。
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter!
echo .
)
exit /b
:end
~ Alex
~亚历克斯
回答by Ramgopal
I had similar problem but it solved by removing spaces between Ex : set FileName=%Name:~0,11% # Working as No space before and after '=' Ex : set FileName = %Name:~0,11% # Not Working as space before OR after '='
我有类似的问题,但它通过删除 Ex 之间的空格解决了:set FileName=%Name:~0,11% # Working as No space before and after '=' Ex : set FileName = %Name:~0,11% # Not在“=”之前或之后用作空格
So please try by remove spaces, It should work Note: command line should be reopen to get refresh the background values else it shows the same output as it is stored in temp
所以请尝试删除空格,它应该可以工作注意:应该重新打开命令行以刷新背景值,否则它会显示与存储在 temp 中相同的输出