

InterviewSolution
Saved Bookmarks
1. |
Solve : SET /P command usage inside for loop? |
Answer» <html><body><p>Hi,<br/><br/>Can anyone please <a href="https://interviewquestions.tuteehub.com/tag/help-239643" style="font-weight:bold;" target="_blank" title="Click to know more about HELP">HELP</a> me how to get input from user inside a for loop? An example would help me much better.<br/><br/>That is using SET /<a href="https://interviewquestions.tuteehub.com/tag/p-236832" style="font-weight:bold;" target="_blank" title="Click to know more about P">P</a> command inside a for loop. Based on user input, I want to perform some operation. But the script has to prompt for user input <a href="https://interviewquestions.tuteehub.com/tag/everytime-2623177" style="font-weight:bold;" target="_blank" title="Click to know more about EVERYTIME">EVERYTIME</a> the loops runs.<br/><br/>Thanks in advance..<br/><br/> Quote from: <a href="https://interviewquestions.tuteehub.com/tag/thiru-707808" style="font-weight:bold;" target="_blank" title="Click to know more about THIRU">THIRU</a> on March 09, 2010, 03:43:25 PM</p><blockquote><br/>That is using SET /P command inside a for loop. Based on user input, I want to perform some operation. But the script has to prompt for user input everytime the loops runs.<br/></blockquote> <br/><br/>C:\batch>type Thriu.bat<br/> Code: <a>[Select]</a><a href="https://interviewquestions.tuteehub.com/tag/echo-11626" style="font-weight:bold;" target="_blank" title="Click to know more about ECHO">ECHO</a> off<br/>setlocal enabledelayedexpansion<br/>for /f "delims=" %%i in (thriu.txt) do (<br/>echo %%i<br/>Echo Enter:<br/>set /p variable=<br/>echo variable = !variable!<br/>)<br/><strong>Output:</strong><br/><br/>C:\batch>Thriu.bat<br/>Hello<br/>Enter:<br/>one<br/>variable = one<br/>World<br/>Enter:<br/>two<br/>variable = two<br/>This<br/>Enter:<br/>three<br/>variable = three<br/>is<br/>Enter:<br/>four<br/>variable = four<br/>a<br/>Enter:<br/>five<br/>variable = five<br/>batch<br/>Enter:<br/>six<br/>variable = six<br/>file<br/>Enter:<br/>seven<br/>variable = seven<br/><br/><strong>Input:</strong><br/><br/>C:\batch>type thriu.txt<br/>Hello<br/>World<br/>This<br/>is<br/>a<br/>batch<br/>file<br/><br/>C:\batch> Quote from: Thiru on March 09, 2010, 03:43:25 PM<blockquote>That is using SET /P command inside a for loop. Based on user input, I want to perform some operation. But the script has to prompt for user input everytime the loops runs.<br/></blockquote> <br/><br/>C:\batch>type never.bat<br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /L %%i in (1,1,%1) do (<br/><br/>set /p variable=Enter:<br/>echo variable = !variable!<br/>)<br/>echo Bye<br/><strong>Output:</strong><br/><br/>C:\batch>never.bat 8<br/>Enter:1<br/>variable = 1<br/>Enter:2<br/>variable = 2<br/>Enter:3<br/>variable = 3<br/>Enter:4<br/>variable = 4<br/>Enter:5<br/>variable = 5<br/>Enter:6<br/>variable = 6<br/>Enter:7<br/>variable = 7<br/>Enter:8<br/>variable = 8<br/>Bye<br/><br/>C:\batch><br/><br/>reference:<br/><br/><a href="https://www.roysac.com/blog/2009/10/some-handy-ms-dos-batch-tricks-infinite.html">http://www.roysac.com/blog/2009/10/some-handy-ms-dos-batch-tricks-infinite.html</a><br/> Quote from: Thiru on March 09, 2010, 03:43:25 PM<blockquote>That is using SET /P command inside a for loop. Based on user input, I want to perform some operation. But the script has to prompt for user input everytime the loops runs.<br/></blockquote> <br/><br/>C:\batch>type nevertest.bat<br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /L %%i in (1,1,1000) do (<br/><br/>set /p variable=Enter:<br/>echo variable = !variable!<br/>echo To Quit, Enter: q<br/>if !variable!==q goto end<br/>)<br/>:end<br/>echo Bye<br/><strong>Output:</strong><br/><br/>C:\batch> nevertest.bat<br/>Enter:one<br/>variable = one<br/>To Quit, Enter: q<br/>Enter:two<br/>variable = two<br/>To Quit, Enter: q<br/>Enter:7<br/>variable = 7<br/>To Quit, Enter: q<br/>Enter:q<br/>variable = q<br/>To Quit, Enter: q<br/>Bye<br/><br/>C:\batch></body></html> | |