Dan
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\Deskt op\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"
)
)
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\Deskt
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"
)
)
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
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%\Des ktop\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
@echo off (delete all files in this directory)
set BaseDir="%userprofile%\Des
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, I think that works for everything in the directory specified, but how do you make it delete everything in subfolders as well.
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"
)
)
~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
Note this is 5 newest files in each subdir, NOT across the directory structure in general, that would be a lot more involved.
steve
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help guys!
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