Solved [HELP]Bat file 2 del the oldest %n files from specific foldr

May 21, 2012 at 10:52:01
Specs: Windows 7, 2gb
My school program makes a file EVERY time I send my school to their server. This will create 95 files then deleted a new one every time it makes a new one. No big deal... just that 95 of those files is about 5.8 gb... and that is a lot of space. Could I have a batch file that will delete a specific number of files from a specified folder, and leave the latest 2 or 3 files? If a code is provided, please explain what each part of it does. Thanks

See More: [HELP]Bat file 2 del the oldest %n files from specific foldr

Report •


#1
May 21, 2012 at 22:37:01
✔ Best Answer
:: save the 3 newest files
:: teku14.bat Tue 22-05-2012 12:27:10.34
@echo off & setLocal enableDELAYedeXpansioN


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

goto :eof

::===========================================


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
May 25, 2012 at 12:43:21
Thanks, how do I customize it?

Report •

#3
May 25, 2012 at 14:36:14
In which way? if you want to save more/less than the 3 newest files change the skip 3 to more/less?

or you could use a replaceable variable with a prompt thats user requested?
mike


Report •

Related Solutions

#4
May 31, 2012 at 05:51:48
Like, what does this all mean, specifically the italic parts.

for /f "tokens=* delims= skip=3" %%a in ('dir/b/o-d') do (
echo del %%a
)
goto :eof


Report •

#5
May 31, 2012 at 06:44:04
for /f "tokens=* delims= skip=3" %%a in ('dir/b/o-d') do (
echo del %%a
)
goto :eof

"tokens=* is to pull the whole line for ('dir/b/o-d')
delims= is to use space as a deliminator, kind of like a break
skip=3" is to skip the first 3 lines in ('dir/b/o-d')
('dir/b/o-d') is to pull the directory, sorting in order of created.

so basically you are looping the ('dir/b/o-d') with the for /f command
and setting %%a equal to each line.

If i am not mistaken the echo del %%a was intended to be a safeguard untill you are sure it works. instead of del %%a

:: mike


Report •

Ask Question