3

I am trying to remove the first word from a string in batch.

Example: "this kid loves batch" to "kid loves batch"

I have tried:

@echo off
set /p text=text: 
for /f "tokens=1,*" %%a in ("%text%") do set updated=%%a
echo %updated%
pause

It just outputs the first word, and does not delete the first word.

How can I make it delete the first word, and keep the rest of the string?

4
  • 3
    Use %%b instead of %%a. Commented Feb 24, 2017 at 0:07
  • @SomethingDark It gives the same output when I change %%a to %%b Commented Feb 24, 2017 at 0:09
  • 3
    No. Just change: set updated=%%b Commented Feb 24, 2017 at 0:15
  • 4
    If you happened to read the help file for the FOR command. Specifically the section about the TOKENS option: If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed. The additional variable is the next letter in the alphabet to put it in simple terms. In reality it is the next ascii character but explaining that is for another question. Commented Feb 24, 2017 at 0:53

2 Answers 2

4

You can do it without the for loop too:

@Echo Off
Set/P "text=text: "
Set "updated=%text:* =%"
Echo(%updated%
Timeout -1
Sign up to request clarification or add additional context in comments.

12 Comments

Bizarre! But how do you also extract the first word to save it separately?
@Victoria, this is even more bizarre… Set "updated=%text: ="&:"%". Please however, in future do not ask new, completely different questions, as comments to other questions. If you cannot find an answer throughout our site by using the relatively good search facility at the top of the page, please submit a new question along with your own code attempt(s).
I'll have to dive back into the docs to figure that one out, like it did for the original answer. But I thought my question here was related, because the for loop solution provides both the first word and the remainder, whereas your solution in the answer only provides the remainder. Granted, the original question was only about obtaining the remainder, but the for solution provided both. OK, the expansion for this one depends on some interesting side effects, not at all documented in the variable expansion description shown by "set /?" (I had missed use of * in earlier readings).
@Victoria, try this script for fun… 1. @Set "string=one two three four five six", 2. @SetLocal EnableDelayedExpansion, 3. @For /F "Delims==" %%G In ('"(Set word[) 2>NUL"') Do @Set "%%G=", 4. @Set "i=1", 5. @Set "word[!i!]=%string: =" & Set /A i += 1 & Set "word[!i!]=%", 6. @Set word[, 7. @EndLocal, 8. @Pause. You should see some even more interesting side effects!
Interesting, but the for line doesn't seem to contribute to the outcome, so I'm a bit baffled as to its inclusion. The syntax of the penultimate Set line is rather interesting of course, with nested delimiters.
|
2

When you use "tokens=1,*" with the default delimiter in a for loop where the variable is %%a, everything to the left of the first whitespace character is stored in %%a, while everything else is stored in %%b.

To get everything after the first word, simply change set updated=%%a to set updated=%%b

@echo off
set /p text=text: 
for /f "tokens=1,*" %%a in ("%text%") do set updated=%%b
echo %updated%
pause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.