Batch file loop through all files in folder

I am looking for some help writing a batch file to run on Windows Server 2008.

I have many pairs of files in a folder.  Let's say these files names are:


(R1 and R2 denote the pairs)

I have a commend that I need to run on each pair of files in this folder.  To complicate things, that command outputs new files that I would like to have placed in a folder with the base name of the original R1 file (for example, AAA).  The command I want to run is:

FLASH --min-overlap=20 --output-directory=[outdir] --output-prefix=MERGED [R1-file] [R2-file]

[R1-file] = the R1 file in the pair (eg. AAA_R1_001.SIX)
[R2-file] = the R2 file in the pair (eg. AAA_R2_001.SIX)
[outdir] = the destination directory (eg. AAA)

How would I write a batch that that can run this command for each file pair, detecting the base filename and passing that on to the command as the [outdir] parameter?  I assume this will be using a for loop, but I've never written one before.


EDIT:  Also, the entire output of this batch file should be captured to a text file, as the command outputs parameters I need to know.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Can we be sure not to see something like
@echo off
pushd c:\FlashFiles
(for /F "tokens=1-2* delims=_" %%A in ('dir /b *_R1_*.SIX') do (
  md %%A 2> nul
  FLASH --min-overlap=20 --output-directory=%%A --output-prefix=MERGED %%A_R1_%%C %%A_R2_%%C
))  > Flash.Ahaaa.log

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITEnd-user supportCommented:
Try this. It sends the FLASH line to variable FNLog. Adjust it as you like.
If you like the output and want to run for real, remove the ECHO fronting FLASH:
@echo off
set FNLog=%temp%\results.txt
for /f %%a in ('dir /b *_r1_*') do (
   echo %%a
   for /f "tokens=1-4 delims=._" %%B in ("%%a") do (
      ECHO FLASH --min-overlap=20 --output-directory=[%%B] --output-prefix=MERGED [%%B_%%C_%%D.%%E] [%%B_R2_%%D.%%E]>>"%FNLog%"
goto :eof

Open in new window

EDIT: I think it works, assuming Qlemo's question is not a factor.
I_play_with_DNAAuthor Commented:
Hmmm...for the current job I'm trying to do, I don't forsee anything after the R1/R2 changing, however, if I wanted to reuse this script in the future I can't necessarily control that.  If we assume that the R1 and R2 will always be constant, but the prefix and suffix could change (although they'll always be the same for the two files in each file pair), how would that change things?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The scripts should still work, if FLASH creates unique file names. Otherwise AAA_R*_002 will override 001, as the result is stored in the same folder AAA.
I_play_with_DNAAuthor Commented:
Both of these solutions worked for me, and are easy to modify to fit the exact situation should the filename structure change.  Thanks folks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.