从一组/ p批量剥离文本

时间:2011-11-02 22:52:21

标签: batch-file

无论如何都要从使用set /p=Command?

的字符串中删除“text”

例如,我正在进行批量游戏,其中一个命令是保存。

有没有办法删除单词“保存”,如果还有剩余内容,则将其设置为“测试”,然后将文件命名为“%TEST%”

2 个答案:

答案 0 :(得分:6)

如果我理解正确,您希望将包含%TEXT%的变量Save filename转换为filename。使用:

Set TEXT=%TEXT:Save =%

冒号后面的部分是搜索和替换表达式。 =左侧文本的第一个出现位置被右侧的文本替换。在这种情况下,“保存”(带有尾随空格)将被替换为空。这是不区分大小写的,因此大写并不重要。

修改:回复以下评论:

要在第一个空格(忽略前导空格)之前检索所有内容,您可以编写一个函数:

:FirstTerm
Set TEXT=%1
Exit /B

并使用以下命令从同一个批处理文件中调用它:

Call :FirstTerm %TEXT%

答案 1 :(得分:0)

简单的答案是使用上面提到的字母,但如果你知道要在查询前删除多少个字符,其余的都是基本的。下面是一个使用变量操作文本的不同方法的示例。简短的回答如下

 @echo off
 set phrase=Some other phrase
 echo %phrase:~11,6%
 pause

此脚本将设置变量"短语" to"其他一些短语" 您可以使用以下内容访问短语中的其余文本:〜跳到短语的第11个字符,然后显示后面的6个字符。请记住,空格是字符和单词"短语"是6个字符。

我的示例用来大写单词首字母的较长版本使用它来显示如何操作文本或变量中的单词。

 goto :top
 :end
 echo.
 pause
 goto :EOF

 :top
 echo.
 SETLOCAL ENABLEDELAYEDEXPANSION
 ::Prompt for Name and set to variable "a"
 set /p a=Enter Name :
 echo.
 ::Set "aName" to the result of what was typed at the prompt.
 SET aName=%a%

 ::Set the First letter of  "aName" to a new variable "firstletter" the 0,1  is the first character (0) 1 character long(1)
 set firstletter=%aName:~0,1%

 ::Set the variable "theRest" to the second character of "aName" to the 44th or whatever you choose 
 ::pneumonoultramicroscopicsilicovolcanoconiosis is the longest word in the dictionary 45 characters long
 set theRest=%aName:~1,44%

 :: display the full name as 2 variables the first letter and the rest of the word/name
 echo %firstletter%%theRest% is what you typed the first letter is "%firstletter%"
 echo.
 CALL :UCase firstletter fl
 CALL :LCase therest tr
 echo %fl% being the uppercase first letter you typed
 echo.
 echo %fl%%aName:~1,44% is the uppercase first and the rest of the word as typed.
 echo.
 echo %fl%%tr% is the Uppercase first letter and all lowercase for the rest of the word.
 echo.
 echo %fl%%tr% >>%userprofile%\desktop\uppercaseresults.txt
 echo.
 echo A file named "uppercasereults.txt" was created on the desktop 
 echo.
 echo with the name you typed above capitalized.
 CALL :UCase aName newName
 echo.
 ECHO.%newName% In all UPPERCASE
 echo.
 CALL :LCase aName newName
 ECHO.%newName% in all LOWERCASE

 ENDLOCAL
 goto end


 :LCase
 :UCase
 :: Converts to upper/lower case variable contents
 :: Syntax: CALL :UCase _VAR1 _VAR2
 :: Syntax: CALL :LCase _VAR1 _VAR2
 :: _VAR1 = Variable NAME whose VALUE is to be converted to upper/lower case
 :: _VAR2 = NAME of variable to hold the converted value
 :: Note: Use variable NAMES in the CALL, not values (pass "by reference")

 SET _UCase=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
 SET _LCase=a b c d e f g h i j k l m n o p q r s t u v w x y z
 SET _Lib_UCase_Tmp=!%1!
 IF /I "%0"==":UCase" SET _Abet=%_UCase%
 IF /I "%0"==":LCase" SET _Abet=%_LCase%
 FOR %%Z IN (%_Abet%) DO SET _Lib_UCase_Tmp=!_Lib_UCase_Tmp:%%Z=%%Z!
 SET %2=%_Lib_UCase_Tmp%
 goto :EOF
 :EOF
 goto :end
相关问题