批处理帮助:使用数字输入从变量中删除字符

时间:2013-12-15 00:09:54

标签: variables batch-file character

是否可以使用其他预定义变量输入要从变量的开头和结尾删除的字符数?或者我只是犯了一个代码错误?因为我尝试过这样的事情:

@echo off
CLS
@setlocal enabledelayedexpansion enableextensions
set /p "word=Enter a Word> "
echo.
set /p "numb1=Suffix Amount Removed> "
echo.
set /p "numb2=Prefix Amount Removed> "
echo.
set new=%word:~!numb1!,-!numb2!%
echo.
echo  Before: !word!
echo.
echo  After: !new!
echo.
pause >nul
exit

而不是使用预定义的变量从批处理的开头和结尾删除字符,例如,如果输入3 numb1 numb2 当它回显 !new! 而不是删除它显示的字符时 After: word:~3,-3 请一些帮助,感谢任何帮助过的人。

1 个答案:

答案 0 :(得分:2)

@echo off
CLS
@setlocal enabledelayedexpansion enableextensions
set /p "word=Enter a Word> "
echo.
set /p "numb1=Suffix Amount Removed> "
echo.
set /p "numb2=Prefix Amount Removed> "
echo.
set new=!word:~%numb1%,-%numb2%!
echo.
echo  Before: !word!
echo.
echo  After: !new!
echo.
pause >nul
exit