-1

I want to find a word from a text file and copy paste the next word on that line using batch file.

For Ex: my name is xyz. i like science. i like to play volleyball.

This is the text file and I want to find the word "play" and display the next word to it "volleyball".

How can I write windows batch file for it?

2 Answers2

1

the literal answer to your question would be "by using Notepad and your keyboard"

The answer, you want to read:
use substring replacement to remove everything from the start until (including) your search word. (see set /?)
Use a for loop to get the first word from the rest of the string. (see for /?)

@echo off
echo my name is xyz. i like science. i like to play volleyball and drink whiskey. >abc.txt
<abc.txt set /p "text="
for /f "delims=.,;! " %%a in ('echo %text:* play =%') do set result=%%a
echo the found word is: %result%
REM to write the found word into a new file, use redirection:
echo %result% >xyz.txt

Edit to match what needs to do with multiple lines ?:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%z in (abc.txt) do (
  set "text=%%z"
  for /f "delims=.,;! " %%a in ('echo "!text:* play =!"') do set "result=%%~a"
  echo !result! >>xyz.txt
)

Some small changes needed; delayed expansion, another for /f loop wraped around and appending to file with >>.

Note: Please don't make this a endless changing question. When you have another problem, please ask a new question

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    As a side note, Volleyball and whiskey do not go well together.. :) – Gerhard Nov 10 '17 at 09:45
  • I am not getting an answer, as i am new to batch-file, Will you kindly mention the entire code here. Please. – Bhavesh Bhanushali Nov 10 '17 at 09:51
  • For Ex: my name is xyz. i like science. i like to play volleyball. above is a "abc.txt" This is the text file and I want to find the word "play" and copy the next word to it which is "volleyball", and paste it to a new file, say xyz.txt. So, "xyz.txt" should contain "volleyball". – Bhavesh Bhanushali Nov 10 '17 at 09:56
  • What do you mean with "the entire code"? There is no more. @GerhardBarnard: Volleyball is *Fun* after drinking Whiskey. For both players and watchers. :D – Stephan Nov 10 '17 at 09:57
  • @BhaveshBhanushali; added code to get text from file and write to another file. – Stephan Nov 10 '17 at 09:58
  • I am getting this inside xyz.txt :- offREM set "text=my name is xyz. i like science. i like to play volleyball and drink whiskey." set /p "text="for /f "delims=.,;! " %a in ('echo * play = REM to write the found word into a new file, use redirection:echo – Bhavesh Bhanushali Nov 10 '17 at 10:02
  • There is no chance to get that `xyz.txt` with my code. Copy it again. – Stephan Nov 10 '17 at 10:07
  • @stephen. After removing both REM statements, I am getting :- off set /p "text="for /f "delims=.,;! " %a in ('echo * play = echo . in xyz.txt – Bhavesh Bhanushali Nov 10 '17 at 10:07
  • @BhaveshBhanushali Not to be rude or anything, but this looks like it is going to become a long "teaching you to write batch" session. You should learn the basics first as you provided no code to begin with, I suggest you [start by clicking here](https://www.tutorialspoint.com/batch_script/) – Gerhard Nov 10 '17 at 10:35
  • @Gerhard Barnard.. Yes i agree, but i knows basics, and i am learning batch-files. – Bhavesh Bhanushali Nov 10 '17 at 10:48
  • But i am stucked with my work on the given scenario. So i wanted the code. I can only request, reply or not is depends on existing masters over here – Bhavesh Bhanushali Nov 10 '17 at 10:50
  • another try: copy my code as it is now, paste it in a new batchfile without changing it and execute it. What does it say? – Stephan Nov 10 '17 at 11:00
  • Heyy.. Thank you so much dear. I got the answer "volleyball". – Bhavesh Bhanushali Nov 10 '17 at 11:14
  • I feel like i have won the volleyball world cup.. !! Thank you so much :-) – Bhavesh Bhanushali Nov 10 '17 at 11:15
  • So I guess, there is something wrong with your `abc.txt` (I re-wrote it explicitely). Probably a wrong encoding (ANSI needed; Batch does not work well with Unicode files). Remember the Whiskey ;) – Stephan Nov 10 '17 at 11:27
  • Heyy.. one more thing, this code is working only for one line, what needs to do with multiple lines ?? – Bhavesh Bhanushali Nov 10 '17 at 11:42
1

This is a simpler method:

@echo off
for /F "tokens=3" %%a in ('"setx /F file.txt dummyVar /R 0,1 play"') do set "var=%%a" & goto continue
:continue
set "var=%var:~0,-1%"
echo %var%

The /R oL,oT switch of setx command allows to get a word in the file relative to another token/word given after it. In the code above the: /R 0,1 play part means: Search "play" word and return the word placed in the same line (0,) and one word after (1) it. This method allows to get words in lines above or below another line, or words at left or right from another word, or both, in a very simple way.

Further details at this post.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • could not sleep last night. Tried to understand, how programmers think. It must be something like `I have an idea for a fancy thing. Let's implement it in some random command, where nobody would expect it`... Honestly: getting a word from a textfile by position **is** a fancy thing. But why on earth in the `setx` command?! You deserve an upvote just for finding that easter egg. – Stephan Nov 11 '17 at 13:25