batch loop

May 28, 2010 at 08:36:56
Specs: Windows XP
I have a directory where the first 3 characters of every file are the same, followed by a 3 digit identifier (always 3, ie, 001, 002, 099, 121, etc) followed by a unique description of the file. They are all pdf's.

I put together a simple batch file that will identify the exact name of the file when you enter the 3 digit unique identifier and copy that to a text file. The end goal is to get a full list of the exact name of the files in the directory.

Right now I just keep running the batch multiple times in a row and hand enter the #'s: 001, 002, 003, 004, 005, 006, etc.

Is there a way to modify the batch to just do it for me without having to enter each # by hand?

@ echo off
set /p xx="enter the number: "
for /f "tokens=*" %%a in ('dir /b dc#%xx%*.pdf') do set aa=%%a
echo %aa% >> List_of_all_reports.txt

set /p xx="enter the number: "
for /f "tokens=*" %%b in ('dir /b dc#%xx%*.pdf') do set bb=%%b
echo %bb% >> List_of_all_reports.txt
pause

THe result is a text file looking like this:
DC#001 - The_history_of_Arizona.pdf
DC#002 - The Tucson Arizona flood of October 1983.pdf
DC#003 - Rains Swamp Marana Farms.pdf
ETC...

Thanks in advance.


See More: batch loop

Report •


#1
May 28, 2010 at 09:22:16
You are missing your Open and Close parans.

@ echo off
set /p xx="enter the number: "
for /f "tokens=*" %%a in ('dir /b dc#%xx%*.pdf') do (
  echo %%a >> List_of_all_reports.txt)

set /p xx="enter the number: "
for /f "tokens=*" %%b in ('dir /b dc#%xx%*.pdf') do (
  echo %%b >> List_of_all_reports.txt)

The for loops do every thing inside the Parans so you can do multiple commands like...

set /p xx="enter the number: "
for /f "tokens=*" %%b in ('dir /b dc#%xx%*.pdf') do (
  echo %%b >> List_of_all_reports.txt
  echo %%b
  Pause)


Report •

#2
May 28, 2010 at 10:20:01
I guess I'm missing the point. Looks like what you get is a list of the PDFs.


=====================================
Helping others achieve escape felicity

M2


Report •

#3
May 28, 2010 at 13:40:41
Let me see if I understand this correctly.

1. All PDF file names are unique in their first six characters, right? So there is only one file beginning DC#001, and only one file beginning DC#002.

2. All you want to do is loop from 001 to 999, instead of asking the user to enter the next number manually, correct?

3. Referring to these two lines in your batch file:

for /f "tokens=*" %%a in ('dir /b dc#%xx%*.pdf') do set aa=%%a 
echo %aa% >> List_of_all_reports.txt

the second line is not meant to be part of the loop, but is deliberately a separate statement coming after it, executed once the loop had ended. (I think reply #1 mistakenly thinks it was meant to be inside the loop.)

4. The loop is not really a loop because it only iterates once, since there is only one file. The only reason you have a FOR statement is to obtain the full filename matching the given wildcard, right?

So... if I guessed correctly what you want to do, you might want to try this...

setlocal enabledelayedexpansion
for /l %%i in (1 1 999) do (
   set i=00%%i
   set i=!i:~-3!
   echo !i!
)
pause

and then plug your batch file into this loop


Report •

Related Solutions


Ask Question