Batch deactivate text from / p

In any case, remove the "text" from the line set with set /p=Command?

For example, I am making a batch game, and one of the teams is saving.

Is there a way to delete the word "Save", and if something remains, set it as TEST, and then name the file% TEST%

+4
source share
2 answers

If I understand correctly, you want the %TEXT% variable containing Save filename to be converted only to filename . Using:

 Set TEXT=%TEXT:Save =% 

The section after the colon is an expression of search and replace. The first occlusion of the text to the left of = is replaced by the text to the right. In this case, "Save" (with a finite space) is replaced with nothing. This is case insensitive, so capitalization does not matter.

Edit:. Reply to comment below:

To get 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:

 Call :FirstTerm %TEXT% 
+6
source

The simple answer is to use the letters as described above, however the rest is elementary if you know how many characters you need to delete before your request. The following is an example with various 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 using: ~ move to the eleventh character of the phrase and display the next 6 characters following it. Remember that spaces are characters, and the word phrase is 6 characters long.

The longer version used by my example to use 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 /pa=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=ABCDEFGHIJKLMNOPQRSTU VWXYZ SET _LCase=abcdefghijklmnopqrstu vwxyz 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 
0
source

Source: https://habr.com/ru/post/1379392/


All Articles