| 1. |
Solve : No more than 8 letters? |
|
Answer» I've been trying to make a batch file and now am stuck. In it, you get an option to type something in, but it cannot be more than 8 letters long (so it will fit on the screen) I want it to be able to type in a word and if it is more than 8 letters goto :toohigh or whatever, and if it's less than/equal to 8 letters to continue. Code: [Select]echo off setlocal set /p var=Enter String: :loop if defined var (set var=%var:~1%& set /a length += 1 & goto loop) if %length% GTR 8 (goto toohigh) echo fell into this (continue) goto :eof :toohigh echo got here from a goto Quote Please include notes on how you did this (or at least try ) so i can manipulate this later and use it for something else. Thank you Explanations are extra. Please pay the cashier on your way out. Basically all the work is done in the if defined line. Each repetition of the loop strips off a single character from the string and adds 1 to the length counter. When no characters exist in the string, the loop ends and the logic flows to either the :toohigh label or simply continues. As with all batch files, if you turn on echo, you can see exactly how it runs. Good luck. Wow thanks A LOT this really helped Will put in my 'batch notes' Awww..i NEED more help now :/ So far my 'sketch' looks similar to this: Code: [Select]echo off setlocal :start cls echo Type in something (No more than 8 letters long) set /p option= :loop if defined option (set option=%option:~1%& set /a length += 1 & goto loop) if %length% GTR 8 goto :toohigh :toohigh echo Too many letters pause >nul goto :start :continued echo Ok pause >nul exit The problem is that when the amount of letters IS lower than 8, it just continues (LIKE i wanted) but not to a specific place. How would you do that? Like if %length% is lower/equal than 8 then goto :continued. Also another problem is if they screw up (more than 8 letters) i want it to go back to :start and try it again. But when i do this, no matter what i type in, it always goes to :toohigh and says too many letters even if it obviously isn't. Please help :/ -----EDIT----- Larrylong helped me on this and it solved both of my problems. It was just changed a little but it works Code: [Select]echo off :start cls echo Enter what you want but no more than 8 letters. set /p MyVar= set A=%MyVar% set length=0 :loop if defined A (set A=%A:~1%&set /A length += 1&goto loop) set /a greater=8 if %length% GTR %greater% goto :toohigh if %length% LSS %greater% goto :continued :toohigh cls echo Too many letters... echo. pause >nul goto :start :continued cls echo Good Job :D pause >nul The only thing that really changed was that he added "set length=0" and if %length% GTR %greater% goto :toohigh if %length% LSS %greater% goto :continued Thanks anyway Sidewinder Quote from: shanked on November 24, 2010, 06:15:07 PM The problem is that when the amount of letters IS lower than 8, it just continues (like i wanted) but not to a specific place. How would you do that? Like if %length% is lower/equal than 8 then goto :continued. Place a goto immediately beneath the if line that goes to "toohigh". for example: Code: [Select] if %length% GTR 8 goto toohigh goto continue Quote from: BC_Programmer on November 24, 2010, 06:25:43 PM Place a goto immediately beneath the if line that goes to "toohigh". for example: Omg i should have known that Thanks though Quote from: shanked on November 24, 2010, 06:15:07 PM Thanks anyway Sidewinder I'm just glad that Larry was able to help you out. Larry has been able to use his multi-faceted personality to help numerous posters. He was wanted by many other forums, so we were truly honored when Larry chose CH. It gives Thanksgiving a whole new meaning. |
|