Link to home
Start Free TrialLog in
Avatar of Dan
DanFlag for United States of America

asked on

batch file question in deleting files

I currently have a batch file that almost does what I need it to do. This script deletes everything in the specified folder or I can specify a file type to keep.

What I need to do is, I would like for a script to be able to specify to ONLY delete a certain type of file, and keep every other file in the same directory.  I also would like the script to keep for example 5 copies of extension .pdf, but delete the oldest ones and only keep the 5 newest according to the date.  Basically, to be able to specify which extension I need deleted and how many of the newest files to keep.  

So this is what I have so far, but I'm not sure how to modify it, or create a new script to do what I need it to do.  I need something that will work in a batch file.


@echo off (delete all files in this directory)
set BaseDir=C:\Users\dan\Desktop\test
set FilesToKeep=3
set FileFiler=*.pdf
for /R "%BaseDir%" %%D in (.) do (
  echo Processing directory "%%~fD"
  for /F "skip=%FilesToKeep% tokens=*" %%F in ('dir /A-D /O-D /B "%%~fD\%FileFilter%" 2^>NUL') do (
    del "%%~fD\%%~F"
  )
)
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

on phone so cant check in detail but you have set filefiler in the set statement rather than set filefilter= so it would not work as expected.

some other similar ones on http://scripts.dragon-it.co.uk and will look back on pc in a bit if not already fixdd.

steve
Here is one of the examples I meant:
http://scripts.dragon-it.co.uk/links/batch-versioning-backup

What you have at the moment looks for the files in each subdir of your base dir.  Is that what you want, or just the files in one dir?

Steve
If you do just want the one specified directory try this:

@echo off (delete all files in this directory)
set BaseDir="%userprofile%\Desktop\test"
set FilesToKeep=5
set FileFiler=*.pdf
for /F "skip=%FilesToKeep% tokens=*" %%F in ('dir /A-D /O-D /B "%BaseDir%\%FileFilter%" 2^>NUL') do (
    echo Deleting %%~F [Date: %%~tF]
    ECHO del "%BaseDir%\%%~F"
)

Remove the word ECHO to make it do the deletes rather than showing them

Steve
Avatar of Dan

ASKER

well, what your script did is delete everything in that directory except PDF's, but that's not what I need, I need the script to delete ONLY PDF's let say, and not touch any other file.
Avatar of Dan

ASKER

the other thing I need, is basically a script that keeps the newest files, but delete everythings that is older, for example files and the files that are deeper in folders.
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan

ASKER

thanks, I think that works for everything in the directory specified, but how do you make it delete everything in subfolders as well.
Avatar of Bill Prew
Bill Prew

Seems like all you had to do was correct the one typo, as below.  Does this work?

@echo off
set BaseDir=C:\Users\dan\Desktop\test
set FilesToKeep=5
set FileFilter=*.pdf
for /R "%BaseDir%" %%D in (.) do (
  echo Processing directory "%%~fD"
  for /F "skip=%FilesToKeep% tokens=*" %%F in ('dir /A-D /O-D /B "%%~fD\%FileFilter%" 2^>NUL') do (
    del "%%~fD\%%~F"
  )
)

Open in new window

~bp
Yes that is what I said originally before I offered the option of doing just one dir.

Note this is 5 newest files in each subdir, NOT across the directory structure in general, that would be a lot more involved.

steve
Avatar of Dan

ASKER

I'm a little lost guys.  So what happened is I have a folder where I backup all my SQL files/folders to a backup server.  
So to keep that folder from just getting insanly big, I just need this script to run to keep let's say the 1000 newest files and delete anything older than that.

Also, I backup my website files/folders as well, and need to do the same there, but with the website backups, there's sometimes multiple layers deep, so I just need to keep the newest ones as well there as well.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I understand it that way that the folders should be treated as files. So older folders should be removed as a whole, without looking into their files. Am I correct with that?
Avatar of Dan

ASKER

I never thought about that.  Actually, In looking at what my program is doing, it looks like it just over rights the files, so I probalby don't need to run a script for that directory, I'm good there.

I guess I just need two scripts to do the following:

I have a folder MSSQL that stores all the .bak files, so I just need a script to keep the newest 1000 files lets say by date, and delete anything over 1000 files that, that are older than the newest 1000 files.

The second script, I have a folder that has a folder and two diferent kinds of files in the folder.
I just need it to ONLY delete .tib files, that again lets say if there's more than 5 files.  So the 6th olded file and 7th, etc... would get deleted.

Does that make sense?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan

ASKER

Thanks for the help guys!