The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

    • RT @MarcBerns1: @zeeger Je zou wel gek zijn als je op erfpacht woont en in verduurzaming investeert: wordt meteen je erfpacht verhoogt, kan… 11 minutes ago
    • RT @isotopp: TIL my potato server at home can create a million directories on XFS in 16s, single threaded. Using multiprocessing(10), it t… 12 minutes ago
    • RT @hansdamen: Dat ging snel, het Twitteraccount @MinBijleveld is binnen de kortste keren omgezet in het generieke @DefensieMin. Gemakkelij… 12 minutes ago
    • RT @locuta: OPROEP AAN ALLEN: Kijk vanavond om 20:25 naar 'Alleen Tegen De Staat' op NPO2, documentaire over de Toeslagenaffaire. 13 minutes ago
    • RT @LarsWienand: Ein Kunde in einer Tankstelle gerät wegen der fehlenden Maske in Streit, kommt umgezogen und mit Maske wieder und erschieß… 14 minutes ago
  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,465 other followers

Windows command prompt: decrementing loop

Posted by jpluimers on 2020/12/30

I needed a decrementing loop on the Windows command prompt, but that seems very hard from batch files without programming your own kind of while loop:

PowerShell to the rescue to loop back from and including 463 down to and including 290:

PowerShell -Command "for ($i=463; $i -ge 290; $i--) { Write-Host "Value " $i}"

This outputs:

Value 463
Value 462
Value 291
Value 290

In a similar way, you can execute a cmd command, but then you need to be careful on how to pass parameters: the \" is important to you can have quotes within quoted strings..

PowerShell -Command "for ($i=463; $i -ge 290; $i--) { & echo \"Value $i\"}"

gives this:

Value 463
Value 462
Value 291
Value 290


PowerShell -Command "for ($i=463; $i -ge 290; $i--) { & echo Value $i}"

gives this:


More details are in:


In my case, I needed to execute a batch file cm with some parameters, so this had to be done through cmd /c cm parameters, where the first bit needs to be separate from the last bits:

PowerShell -Command "for ($i=463; $i -ge 290; $i--) { & cmd \"/c cm log cs:$i\"}"

If you want to pause inbetween the the iterations:

PowerShell -Command "for ($i=463; $i -ge 290; $i--) { & cmd \"/c cm log cs:$i\" ; pause }"

If you want specialised output, remember to:

  • escape all double quotes like the \" above
  • use semicolons ; between statements
  • use Write-Host for output (as it is faster than shelling out to echo)
  • use back-ticks for special characters, for instance `r`n for carriage return (CR) followed by newline/line feed (LF)
  • remember what Write-Host will automatically insert a newline, so you only need the `r to make a CR/LF pair
  • redirect output at the end (but remember that a standard redirect only handles stdout, not stderr)
PowerShell -Command "for ($i=463; $i -ge 290; $i--) { Write-Host \"`r`n### cs:$i`r`n`r`n``````text`r\" ; & cmd \"/c cm log cs:$i\" ; Write-Host \"```````r\" }" >

This will get output items like this (apart from a file

The specified changeset cs:443 does not exist.

plus a content of that has the output of each cm command output surrounded with pairs of these:

### cs:293 

cm output

### cs:292 

cm output



One Response to “Windows command prompt: decrementing loop”

  1. Peter Wright said

    Oh, so much easier to use Powershell than cmd’s equivalent:

    FOR /L %%v IN (463,-1,290) DO ECHO Value: %%v

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: