Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Batch - Remove first word from a string

Tags:

batch-file

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?

like image 274
cookiechicken32 Avatar asked May 05 '26 16:05

cookiechicken32


2 Answers

You can do it without the for loop too:

@Echo Off
Set/P "text=text: "
Set "updated=%text:* =%"
Echo(%updated%
Timeout -1
like image 116
Compo Avatar answered May 07 '26 04:05

Compo


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
like image 41
SomethingDark Avatar answered May 07 '26 06:05

SomethingDark