InterviewSolution
| 1. |
Solve : Bat file help!? |
|
Answer» Before reading i want you to know that my english is not good... IF-THEN-ELSE statements are not a batch strong point If "%condition%"=="TRUE" ( echo Yes ) else ( echo No ) @echo off cls echo Welcome to Haslien Transolater :trans REM The word English starts with a capital E set /p word="[0 to quit] Enter English word: " if "%word%"=="0" goto end REM I use English / French word pairs because REM I don't know what language you are using REM Use /i to ignore case if /i "%word%"=="cat" goto cat if /i "%word%"=="dog" goto dog if /i "%word%"=="man" goto man if /i "%word%"=="hat" goto hat if /i "%word%"=="pig" goto pig REM if you get here then word not in list Echo Error: %word% not in list goto trans :cat echo %word% = chat goto trans :dog echo %word% = chien goto trans :man echo %word% = homme goto trans :hat echo %word% = chapeau goto trans :pig echo %word% = cochon goto trans :end echo Finished Pause Screen output: Welcome to Haslien Transolater [0 to quit] Enter English word: cat cat = chat [0 to quit] Enter English word: hat hat = chapeau [0 to quit] Enter English word: man man = homme [0 to quit] Enter English word: pig pig = cochon [0 to quit] Enter English word: dog dog = chien [0 to quit] Enter English word: TAP Error: tap not in list [0 to quit] Enter English word: 0 Finished Press any key to continue . . . This is much shorter @echo off cls echo Welcome to Haslien Transolater :loop set /p word="[0 to quit] Enter English word: " if "%word%"=="0" goto end set trans=$$$$ REM You only have to EDIT here to add / remove word pairs if /i "%word%"=="cat" set trans=chat if /i "%word%"=="dog" set trans=chien if /i "%word%"=="man" set trans=homme if /i "%word%"=="hat" set trans=chapeau if /i "%word%"=="pig" set trans=cochon if "%trans%"=="$$$$" ( Echo Error: %word% not in list goto loop ) echo %word% = %trans% goto loop :end Echo Finished Pause Suggestion: if "%word%"=="" ( Echo You entered a blank string goto loop )another WAY: (1) words.txt cat,chat dog,chien man,homme hat,chapeau pig,cochon horse,cheval sheep,mouton cow,vache mouse,souris bird,oiseau girl,fille (2) Batch program in same folder @echo off cls echo Welcome to Translater :loop set /p word="[0 to quit] Enter English word: " if "%word%"=="0" goto end set trans=$$$ for /f "tokens=1,2 delims=," %%A in (words.txt) do if /i "%word%"=="%%A" set trans=%%B if "%trans%"=="$$$" ( Echo Bad input or word not in list goto loop ) echo %word% = %trans% goto loop :end Echo Finished Pause |
|