2011-11-02 20 views
4

剝離文字反正有從set /p=Command?批量從一組/ P

比如我做了一批遊戲,命令被保存的一個設置字符串中刪除「文本」。

有沒有辦法刪除的字保存,如果有什麼遺留下來的,然後將其設置爲TEST,然後將文件命名爲%TEST%

回答

6

如果我理解正確的話,你想要的變量%TEXT%這包含Save filename將被轉換爲只有filename。使用:

Set TEXT=%TEXT:Save =% 

冒號後面的部分是搜索和替換表達式。 =左側文字的第一個位置被右側的文字替換。在這種情況下,「保存」(尾隨空格)被替換爲無。這是不區分大小寫的,所以大小寫無關緊要。

編輯:應對下面的評論:

要檢索的第一個空格前的一切(前導空格被忽略),你可以寫一個函數:

:FirstTerm 
Set TEXT=%1 
Exit /B 

,並從同一調用它批處理文件:

Call :FirstTerm %TEXT% 
+2

這個方法對任何字符串的作用都不重要,如果它被'set/P'讀取或不是。 – Aacini

+0

有沒有辦法「扭轉」這一點,所以剝奪了一切,但命令。一個例子是「使用某些東西」被剝離使用「保存某些東西」去掉保存 –

0

簡單的答案是使用上面提到的字母,但其餘的是elem如果你知道在你的查詢前刪除多少個字符,那麼它就是entary。以下是使用變量操作文本的不同方式的示例。簡短的回答是如下

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

該腳本將設置變量「短語」到「其他一些短語」 您可以通過訪問該詞組的正文的其餘部分:〜跳到第十一個字符的短語並顯示接下來的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