Solved

Multiple commands inside a DO LOOP

Posted on 2004-10-04
2
666 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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