how to count number of files in cmd

February 16, 2009 at 23:25:11
Specs: Windows XP
how to count files in a directory using a batch file and delete the previous files if the number of files are more than 10.
what i did is
set count=0
cd data2000|find "*.mdb"/c>nofiles.###
count<nofiles
if(count GTR 10)
for *.mdb in data2000
if(count>10)
delete *.mdb
count=count-1
else
exit
end
else
exit

See More: how to count number of files in cmd

Report •


#1
February 17, 2009 at 02:29:32
If you just want to keep the 10 newest:

=========================================
@echo off & setLocal EnableDelayedExpansion

for /f "skip=10 tokens=* delims= " %%a in ('dir/b/o-d *.mdb') do (
del %%a
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
February 17, 2009 at 03:04:26
sir
its not working the old files are not deleted
its prompting files not found error

Report •

#3
February 17, 2009 at 03:41:59
If there are no mdb files it won't find any. Did you change to the directory where the files are?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
February 17, 2009 at 23:53:37
sir

the command u gave is to delete new files.i want to delete the old files and keep the 10 new files. is the logic to copy all the files in to new folder and delete all the files in the previous folder and copy the 10 new files into older one.

sir don't mind
i am new to this field so i am unable to get please reply with patience


Report •

#5
February 18, 2009 at 00:18:41
It keeps the 10 newest.


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question