windows - Parsing the text file line-by-line using batch script (batch file) -


so, programming in batch script , came across issue. following code take yourwords.txt file , parse it. existedword variable have last word of text file. so, everytime when run program compare user's input last word on text file, want compare beginning , if word exist in text file display message "repeated word". if word not in text file add .txt file , display message "that new word."

the .txt file contain words per line. example,

apple banana cat dog elephant fish gone home ignorant 

note: number of words in text file hundreds.

my goal compare every word in text file , see if user input match words existed in .txt file.

@echo off  :start cls echo. echo enter word: set /p "cho=->" /f "tokens=*" %%a in (yourwords.txt) (set existedword=%%a)  if /i %cho%==%existedword% (goto repeatedword) echo %cho% >> yourwords.txt )  :successful echo. echo  new word. ping -n 5 localhost >nul goto start  :repeatedword echo. echo  sorry! word repeated word. echo. pause goto start 

you don't need parse file line line.

@echo off :start cls echo. set /p "cho=enter word: -> " findstr /i "\<%cho%\>" yourwords.txt >nul 2>&1 if %errorlevel%==0 (   echo.   echo  sorry! word repeated word.   echo. ) else (  echo.   echo  new word.   echo %cho%>>yourwords.txt   ping -n 5 localhost >nul ) goto start 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -