top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use a for loop in windows command line?

+2 votes
504 views

Any sample example would be helpful, I want to use all files from a directory and want to apply certain operation on it.

posted Jun 20, 2016 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+5 votes
 
Best answer

Yes, For is definitely available in command prompt / batch file.
Most common way of using for loop is with a data set. It works more or less like a foreach.

General syntax goes like this,

FOR %i IN <DataSet> DO <method> %i

Here we pass a custom data set (an array),

FOR %i IN (1,2,3,4) DO echo %i

To loop through executable files,

FOR %i IN (*.exe) DO echo %i

For additional information, type "help for" in command prompt.

Hope this helps.

answer Jun 20, 2016 by Vinod Kumar K V
Similar Questions
+1 vote

I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work.

import subprocess

p = subprocess.Popen("D:PythonPython27Scriptspip.exe list -o",
 stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT,
 bufsize=1,
 universal_newlines=True,
 shell=False)
for line in p.stdout:
 print line

When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command
line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking?

0 votes

I need any software which makes my pen drive boot-able for any OS like Win XP/7/Vista...any pointer?

0 votes

How to bypass windows 7 password when it is forgotten without use of third party application.

...