For loop in windows batch file with command to loop




















Active Oldest Votes. Improve this answer. Dave Jarvis Anders Anders This solution looks clean and readable. I would also warn any programmer who uses this solution that the round bracket after the DO must NOT be placed next line otherwise it brakes the batch file.

Also -- be wary if any of your nested commands have brackets in for example, something like "echo Something something something else " EDIT: ah, see bk1e's answer : — Tim Barrass. The key is: The round brackets after DO must be placed on the same line, otherwise the batch file will be incorrect! Steven 1, 2 2 gold badges 13 13 silver badges 28 28 bronze badges. Kalle Kalle 2, 1 1 gold badge 26 26 silver badges 29 29 bronze badges. Actually, I think the parentheses are unnecessary.

If this solution makes a syntax error for you, check out if you got an unescaped paren between the DO -parens. That's a really good point. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. Learn more. How do you loop in a Windows batch file?

Ask Question. Asked 12 years, 4 months ago. Active 3 years, 3 months ago. Viewed k times. What is the syntax for a FOR loop in a Windows batch file? Improve this question. Pradeep Pradeep 3, 11 11 gold badges 33 33 silver badges 38 38 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Peter Mortensen Thanks for adding. Might be more clear if you change the starting value in your example to something different than the increment.

The end value is inclusive. From FOR loops. Andreas Rejbrand 99k 8 8 gold badges silver badges bronze badges. Can I give the range 1 TO in list? I'm fighting with these bloody loops right now, working completely unexpectedly, and I've no idea what is controlling anything looking at the output. Horrid stuff. Useful, but horrid. Greg Hewgill Greg Hewgill k gold badges silver badges bronze badges. Conditionally perform a command several times.

Thank you very much foxidrive, you've been very helpful pushing me in the right direction! The code in my loop does exactly what your final edit does - without using delayed expansion.

Woops, you're absolutely right! That code does work, except for the fact that I keep getting an error in the command window saying it cannot find the drive specified. I can't seem to figure out where it's getting this. I'll do a bit more digging, but either way it's working overall now. Hmm, that was strange.

I took out some of my code that was commented out and it worked like a charm Is it common for the system to try to run commands that have been commented out using ::? Perhaps I should use REM instead? When using :: inside a loop it will cause issues only in certain lines, like before the last parenthesis so it's always safer to use REM inside a loop.

I'm glad you got it sorted out. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Visit chat. Linked 4. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings.



0コメント

  • 1000 / 1000