help with batch

Microsoft / Dos
March 9, 2010 at 07:57:08
Specs: Windows XP
i need help with some batch programming...
a program generates files in a directory

a dir command in alphabetical order give

thus how can i write a program to know the last file , ie abc-n.htm
the date of creation need not b in order.
thought very much but didnt seem to get any answer.

See More: help with batch

Report •

March 10, 2010 at 01:37:04
For a maximum of 999 files. Probably not very efficient.

1. Extract the filename numbers from each file name
2. Add leading 0's (zeroes) to numbers to bring them all to three digits. i.e. add a single zero to numbers >9 and <100, add double zeroes to numbers <10 so that 9 becomes 009, 40 becomes 040 etc...
3. Reverse sort the filename numbers file (sort /r)
4. Extract the first line of the sorted filename numbers file (Set /p Number=<filename.ext) The highest number will be in the variable %Number%

Hope this helps

Report •

March 10, 2010 at 04:22:49
nice method....

how abt. setting up a loop checking for existence of file with each no. in a order till we find a non existing file...

but i am facin a prob. here, how am i supposed to concatenate a string and a no.???

Report •

March 10, 2010 at 07:57:59
The batch below implements your idea, i.e. to check for the non existing file. It works however if all the numbers in the sequence are present and may be inefficient. Save e.g. as xfile.bat then type

xfile C:\My folder

i.e. xfile followed by the directory holding your sequence

@echo off & setlocal
cd %*
for /L %%j in (1 1 10000) do if not exist ???-%%j.htm (
  set num=%%j
  set /A num-=1
  goto :DONE
echo.  Last generated file is abc-%num%.htm
:: End_Of_Batch

By the way this is not DOS but NT batch working under NT Windows family only.

Report •

Related Solutions

March 10, 2010 at 22:22:30
thanx for the support bro...
all the files generated are in the sequence...
this worked

@echo off
set "str1=x"
set /A str2=1
set "str3=%str1%-%str2%"
echo %str3%.html
if exist "%str3%.html" set /A str2 +=1
if not exist "%str3%.html" set /A str2 -=1 & goto :ending
goto :starting

echo x-%str2%

Report •

Ask Question