Skip to content
Fix Code Error

Batch script loop

March 13, 2021 by Code Error
Posted By: Anonymous

I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a for loop, but the for loop expects a list of items, hence I would need 200 files to operate on, or a list of 200 items, defeating the point.

I would rather not have to write a C program and go through the length of documenting why I had to write another program to execute my program for test purposes. Modification of my program itself is also not an option.

So, given a command, a, how would I execute it N times via a batch script?

Note: I don’t want an infinite loop

For example, here is what it would look like in Javascript:

var i;
for (i = 0; i < 100; i++) {
  console.log( i );
} 

What would it look like in a batch script running on Windows?

Solution

for /l is your friend:

for /l %x in (1, 1, 100) do echo %x

Starts at 1, steps by one, and finishes at 100.

Use two %s if it’s in a batch file

for /l %%x in (1, 1, 100) do echo %%x

(which is one of the things I really really hate about windows scripting)

If you have multiple commands for each iteration of the loop, do this:

for /l %x in (1, 1, 100) do (
   echo %x
   copy %x.txt z:whateveretc
)

or in a batch file

for /l %%x in (1, 1, 100) do (
   echo %%x
   copy %%x.txt z:whateveretc
)

Key:
/l denotes that the for command will operate in a numerical fashion, rather than operating on a set of files
%x is the loops variable
(starting value, increment of value, end condition[inclusive] )

Answered By: Anonymous

Related Articles

  • How many times the loss function is triggered from…
  • How does PHP 'foreach' actually work?
  • Is it possible to apply CSS to half of a character?
  • Ukkonen's suffix tree algorithm in plain English
  • What is your most productive shortcut with Vim?
  • Gradle error: Execution failed for task…
  • org.gradle.api.tasks.TaskExecutionException:…
  • How to calculate Cohen's D across 50 points in R
  • What is the copy-and-swap idiom?
  • Fastest way to iterate over all the chars in a String
  • Unexpected end of JSON input while parsing
  • Could not autowire. No beans of…
  • Jetpack Compose and Hilt Conflict
  • "The import org.springframework cannot be resolved."
  • C threads corrupting each other
  • Execution failed for task…
  • What are the undocumented features and limitations…
  • Batch files : How to leave the console window open
  • Build Polymer App with multiple elements using one…
  • easiest way to extract Oracle form xml format data
  • Firebase cloud function onUpdate is triggered but…
  • how to toggle class for multiple elements in vue js
  • How to make onClick card to move to another page in react
  • Smart way to truncate long strings
  • How do I remove single children in a tree?
  • ComboBox.SelectedItem giving Null value
  • data.table vs dplyr: can one do something well the…
  • Tensorflow: how to save/restore a model?
  • Checkout another branch when there are uncommitted…
  • How can I determine whether a 2D Point is within a Polygon?
  • Binding between two arbitrary objects in Ember.js
  • Dynamically allocating an array of objects
  • How can I manually compile a svelte component down…
  • What is The Rule of Three?
  • How can I find the product GUID of an installed MSI setup?
  • How do I merge two dictionaries in a single…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • What is the easiest way to remove the first…
  • Consider defining a bean of type…
  • how to use value of a column as input to a spatial operation
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How can I programmaticaly copy text from notepad and…
  • Increasing size of semi circle using css
  • List changes unexpectedly after assignment. How do I…
  • How to make vim paste from (and copy to) system's clipboard?
  • The definitive guide to form-based website authentication
  • Copy a variable's value into another
  • Inserting into Oracle and retrieving the generated…
  • polymer paper-scroll-header-panel doesn't display
  • In CSS Flexbox, why are there no "justify-items" and…
  • JavaScript - XMLHttpRequest,…
  • How to copy and paste code without rich text formatting?
  • How to upload images to an auto-generated doc…
  • Windows XP or later Windows: How can I run a batch…
  • Keras input explanation: input_shape, units,…
  • Simplest way to create Unix-like continuous pipeline…
  • Dynamically update values of a chartjs chart
  • When I'm testing a web app by JUnit and Mockito I…
  • WAVE file unexpected behaviour
  • How do I correctly clone a JavaScript object?
  • What is move semantics?
  • A regex for version number parsing
  • Logging best practices
  • For-each over an array in JavaScript
  • Why do batch_normalization produce all-zero output…
  • Creating job instances concurrently causes MySQL…
  • How to print a number with commas as thousands…
  • Why does C++ code for testing the Collatz conjecture…
  • How to add geojson points to the map
  • Database development mistakes made by application developers
  • Do you (really) write exception safe code?
  • Start redis-server with config file
  • Fix top buttons on scroll of list below
  • How do I escape ampersands in batch files?
  • Google Forms file upload complete example
  • Unable to use helper classes within unit tests of a…
  • How to use Polymer 2 Build Process?
  • Secure FTP using Windows batch script
  • How to automatically position multiple model…
  • How do you sign a Certificate Signing Request with…
  • CALL command vs. START with /WAIT option
  • How to use html template with vue.js
  • Mutate column using array names
  • Vue: How do you add E2E tests after not including…
  • Sort table rows In Bootstrap
  • Can I construct base class to allocate while…
  • Polymer 1.x: Observers
  • Clang vs GCC - which produces faster binaries?
  • What's the best way to get the last element of an…
  • javascript replace all next text
  • Using multiple IF statements in a batch file
  • How to prevent scrolling the whole page?
  • How can I pass a wct test while rearranging children…
  • What is Ember RunLoop and how does it work?
  • Why am I having image overlap issues while using…
  • Removing double quotes from variables in batch file…
  • Azure Batch API rising 'AttributeError' in ML notebook
  • load and execute order of scripts
  • OpenCV Object detection with Feature Detection and…
  • Click button copy to clipboard using jQuery

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

MySQL: How to allow remote connection to mysql

Next Post:

Open firewall port on CentOS 7

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error