Solved

Multiple commands inside a DO LOOP

Posted on 2004-10-04
2
674 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
ID: 12217237
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
ID: 12217653
Thanks Steve, just the job!!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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