1.

Solve : Looping in a PC .bat file:?

Answer» <html><body><p>Can any kind fellow help me write the Loop in my PC .bat file?<br/><br/>echo off<br/>change user /install<br/>pushd  \\servername\installlib\<br/>Set Environment=<br/>Set /P Environment=Environment Name DV DX PY PD PZ or PY: %=%<br/><br/>Set NumberOfUpdates=<br/>Set /P NumberOfUpdates=Number of package updates ( Where A=1 and Z=26 ): %=%<br/><br/>Echo Updating %Environment%7333 Environment with %NumberOfUpdates% update packages:<br/>Pause<br/><br/>Set Update1=%Environment%7333UGA<br/>Set Update2=%Environment%7333UGB<br/>.<br/>.<br/>.<br/>Set Update26=%Environment%7333UGZ<br/><br/><strong><em>For X = 1 to %NumberOfUpdates%<br/>       Echo Now applying update: %UpdateX%<br/>       C:\Setup.exe –s –p %UpdateX% -D d:\b7<br/><a href="https://interviewquestions.tuteehub.com/tag/next-578185" style="font-weight:bold;" target="_blank" title="Click to know more about NEXT">NEXT</a> X</em></strong><br/><br/>change user /execute<br/>PAUSE<br/><br/><br/><em>My Scenario: I got to rollout 6 Environments across 4 servers each with between 10-20 updates. That’s about 300 updates, luckily I found out how to do it from the command line and not go through the silly GUI Click/Point/Browse path/<a href="https://interviewquestions.tuteehub.com/tag/select-630282" style="font-weight:bold;" target="_blank" title="Click to know more about SELECT">SELECT</a>/ double-click etc etc  for each update. Now I want to write a .BAT to make it even easier for next time.</em><br/><br/>Batch loop syntax<br/><br/>FOR %I in (start,stop,<a href="https://interviewquestions.tuteehub.com/tag/step-25533" style="font-weight:bold;" target="_blank" title="Click to know more about STEP">STEP</a>) DO command<br/><br/>I usually put the loop commands themselves in their own batch file<br/><br/>FOR %I in (1,1,%NumberofUpdates%) DO UpdateMachine %I<br/><br/>One litlle 'gotcha' is then when using a FOR Loop IN the batch file, you put %I, but if exec from the command prompt directly you put %%I so that the "%I" get's passed to the 'FOR' and not the value of I<br/><br/>Clear as mud :-) Quote from: BradDet on August 02, 2010, 02:40:32 PM</p><blockquote>Batch loop syntax<br/><br/>FOR %I in (start,stop,step) DO command<br/><br/>I usually put the loop commands themselves in their own batch file<br/><br/>FOR %I in (1,1,%NumberofUpdates%) DO UpdateMachine %I<br/><br/>One litlle 'gotcha' is then when using a FOR Loop IN the batch file, you put %I, but if exec from the command prompt directly you put %%I so that the "%I" get's passed to the 'FOR' and not the value of I<br/><br/>Clear as mud :-)<br/></blockquote> <br/>Yeah, that won't work...<br/><br/>Here, NewCNCguy, this code will do what you want:<br/><br/>For /l %%A in (1,1,%numberofupdates%) do (<br/>       Echo Now applying update: %UpdateX%<br/>       C:\Setup.exe –s –p %UpdateX% -D d:\b7<br/>)<br/> Quote from: BradDet on August 02, 2010, 02:40:32 PM<blockquote>Batch loop syntax<br/><br/>FOR %I in (start,stop,step) DO command<br/><br/></blockquote> <br/>BradDet, you are quite a new member. You need to be aware that posting like that is going to cause you to get posts like this that I am writing here now. You should check what you are posting, as that way you don't post stuff which is wrong and which does the opposite of helping. With command line keywords you can just type the command followed by a space and then a slash and a question mark. Here is an example<br/><br/> Code: <a>[Select]</a>FOR /?<br/>You get several screens of help including this<br/><br/> Code: <a>[Select]</a>FOR /L %<a href="https://interviewquestions.tuteehub.com/tag/variable-772077" style="font-weight:bold;" target="_blank" title="Click to know more about VARIABLE">VARIABLE</a> IN (start,step,end) DO command [command-parameters]<br/><br/>    The set is a sequence of numbers from start to end, by step amount.<br/>    So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would<br/>    generate the sequence (5 4 3 2 1)<br/>As you will note, this is a lot different from what you posted. To summarise: don't post off the top of your head, check it first, preferably running any code you are going to suggest. This should the minimum standard for posting help on CH. Anything less is low quality.<br/><br/>Why exactly is it "a lot different?"<br/><br/>I admit I left out the "/L" by mistake,<br/><br/>and technically if I run a batch in the command I should put "call program.bat" by after the do, but I wouldn't say it was "a lot different", just a typo.<br/><br/>I make typo's quite frequently actually, sorry if this causes too much confusion Quote from: BradDet on August 02, 2010, 04:45:49 PM<blockquote>Why exactly is it "a lot different?"<br/><br/>I admit I left out the "/L" by mistake,<br/><br/>and technically if I run a batch in the command I should put "call program.bat" by after the do, but I wouldn't say it was "a lot different", just a typo.<br/><br/>I make typo's quite frequently actually, sorry if this causes too much confusion<br/></blockquote> <br/>It's a "lot different" in the sense that unlike a post that includes the /L it doesn't help the OP in their question, and is more likely to cause more issues and confusion. Without the /L, the standard for command is:<br/><br/> Code: <a>[Select]</a>for %%var in (set) do command<br/><br/>which is clearly quite a bit different from yours. therefore, if somebody tried your Speshul version that accidentally omitted the switch, they would not have a loop going from start to end by step, but rather a 3 item loop that executes with start, step, and then end. For example:<br/><br/> Code: <a>[Select]</a>for %P in (1,1,5) do echo %P<br/><br/><br/>this gives us:<br/><br/> Code: <a>[Select]</a>C:\&gt;for %P in (1,1,%numberofupdates%) do echo %P<br/><br/>C:\&gt;echo 1<br/>1<br/><br/>C:\&gt;echo 1<br/>1<br/><br/>C:\&gt;echo 5<br/>5<br/><br/>C:\&gt;<br/><br/><br/>Which is clearly "a lot different" then what was intended. The OP might then assume that they made a mistake of their own- or, in the worst case, not notice that only three of their 26 updates ran through and accept the solution. Then, later down the road, much to their chagrin they discover that for every update they performed, the first update was performed twice and the last update was performed once and all the others were ignored. <br/><br/> Quote from: BC_Programmer on August 02, 2010, 11:23:11 PM<blockquote>It's a "lot different" in the sense that unlike a post that includes the /L it doesn't help the OP in their question</blockquote> <br/>There is that, which is bad enough, but another thing just as bad in its way is this <br/><br/> Quote<blockquote>(start,stop,step)</blockquote> <br/>We would have to guess that he meant "(start, step, end)". Assuming when he wrote "stop" he meant "end", that's a completely different order. <br/><br/>It seems clear from his reply that he still does not see what is wrong with his attitude.<br/><br/><br/><br/><br/><br/>, and is more likely to cause more issues and confusion. Without the /L, the standard for command is:<br/><br/> Code: <a>[Select]</a>for %%var in (set) do command<br/><br/>which is clearly quite a bit different from yours. therefore, if somebody tried your Speshul version that accidentally omitted the switch, they would not have a loop going from start to end by step, but rather a 3 item loop that executes with start, step, and then end. For example:<br/><br/> Code: <a>[Select]</a>for %P in (1,1,5) do echo %P<br/><br/><br/>this gives us:<br/><br/> Code: <a>[Select]</a>C:\&gt;for %P in (1,1,%numberofupdates%) do echo %P<br/><br/>C:\&gt;echo 1<br/>1<br/><br/>C:\&gt;echo 1<br/>1<br/><br/>C:\&gt;echo 5<br/>5<br/><br/>C:\&gt;<br/><br/><br/>Which is clearly "a lot different" then what was intended. The OP might then assume that they made a mistake of their own- or, in the worst case, not notice that only three of their 26 updates ran through and accept the solution. Then, later down the road, much to their chagrin they discover that for every update they performed, the first update was performed twice and the last update was performed once and all the others were ignored. <br/><br/><br/>[/quote]Ah... My misunderstanding,<br/><br/>You meant that the functional execution of was a lot different, which  I agree with.  I had thought you meant literally the statement itself was a lot different.<br/><br/>This is indeed a very beginner mistake on my part!<br/><br/>Computers will ALWAYS do you tell them to do,  not necessarily what you WANT them to do  It might be worth noting that <br/><br/>for /L %%N in (1,1,5) do something <br/><br/>is functionally equivalent to<br/><br/>for %%N in (1 2 3 4 5) do something <br/><br/>the difference being that if you explicitly list every item you can do stuff like this<br/><br/>for %%N in (1 2 2a 3 4 5 7 99 cat dog) do something <br/><br/><br/><br/><br/></body></html>


Discussion

No Comment Found