Solved

Multiple commands inside a DO LOOP

Posted on 2004-10-04
2
650 Views
Last Modified: 2008-02-26
I'm trying to write a batch file that writes another file that is based on two things: 1) A list of files 2) a Count

I want the batch file to list files of a certain type (.jpg in this intsnace) and then write those files to a temporary file. Then, line by line, read in those file names and write them to another file with an incremented number.

ie.

Batch start

dir *.jpg /b > files.txt  
                       - Writes a list of jpg files to files .txt

for /F "DELIMS=" %%F IN (files.txt) DO (Echo %%F %count >> %1.txt)
                       - Writes a line to my file (given by %1) containing a file name (from DIR command earlier) and  a count.

I can't see how to facilitate a "Count" variable in this DO loop as I don't know how to do multiple commends inside the paraentheses.

I guess I need to use "SET /A COUNT=COUNT+1", but I can't see where or how as I only seem to be able to do one thing in the DO() bit.

Thanks, flaxius
0
Comment
Question by:flaxius
2 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 125 total points
Comment Utility
Give this a try:

@echo off

setlocal enabledelayedexpansion

dir *.jpg /b > files.txt  

set count=0

for /F "DELIMS=" %%F IN (files.txt) DO (
  set /a count+=1
  Echo %%F !count! >> %1.txt
)

The key to getting this working is the enabledelayedexpansion and the use of the exclamation (!) around the environment variable in the do loop. Check out setlocal /? for more information.

Good Luck,
Steve
0
 

Author Comment

by:flaxius
Comment Utility
Thanks Steve, just the job!!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now