Reputation: 1399
Is there anyway to remove "text" from a string set with set /p=Command?
For example I'm making a batch game, and one of the commands is save.
Is there a way to remove the word Save, and if there is anything left over then set it as TEST, and then name the file %TEST%
Upvotes: 4
Views: 334
Reputation: 131
The simple answer is to use letters as mentioned above however the rest is elementary if you know how many characters to remove in front of your query. Below is an example with different ways to manipulate text using variables. The short answer is as follows
@echo off
set phrase=Some other phrase
echo %phrase:~11,6%
pause
This script will set the variable "phrase" to "Some other phrase" You can access the rest of the text in the phrase by using the :~ skipping to the eleventh character of the phrase and displaying the next 6 characters following that. Remember that spaces are characters and the word "phrase" is 6 characters long.
The longer version that my example uses to capitalize the first letter of a word uses this to show how to manipulate words in text or variables.
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
Upvotes: 0
Reputation: 12794
If I understand correctly, you want the variable %TEXT%
which contains Save filename
to be converted to just filename
. Use:
Set TEXT=%TEXT:Save =%
The section after the colon is a search-and-replace expression. The first occcurance of the text on the left of the =
is replaced by the text on the right. In this case "Save " (with a trailing space) is replaced with nothing. This is case insensitive so capitalization doesn't matter.
Edit: Responding to the comment below:
To retrieve everything before the first space (leading spaces are ignored) you can write a function:
:FirstTerm
Set TEXT=%1
Exit /B
and call it from the same batch file with:
Call :FirstTerm %TEXT%
Upvotes: 6