Home > Blockchain >  How to delete all words after a specific word in batch file
How to delete all words after a specific word in batch file

Time:07-01

I have a text file named myfile.txt and it has the following text-

Hi my cry die fly   

I want to delete all the words after cry.

Please help

CodePudding user response:

Something like this do:

@echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%i in ("myfile.txt") do (
    set "line=%%i"
    set "line_excl=!line:*cry=!"
    call echo %%line:!line_excl!=%%
)

It sets each line in the file as a value to the a variable called %line%, delayedexpansion is needed however, so we enable it and then use the variable as !line!.

We set a search replace variable to be everything up to cry and then simply set echo the !line! by excluding the remaining part, which is everything after cry.

  • Related