Gerhardpet
asked on
Script to delete files in a folder
I need a script that will delete files in a folder. I want to be able to specify the path and the days to retain. I will save it as a batch file and use Windows task scheduler.
Can someone help me with this?
Can someone help me with this?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
The path is "D:\Documents\Google Drive\xTuple Backups" and I want to retain 5 days.
The files inside the folder are backups of a postgres database
Will the powershell work on a windows 7 computer? Can I run it from a schedule task?
The files inside the folder are backups of a postgres database
Will the powershell work on a windows 7 computer? Can I run it from a schedule task?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thanks Robert. That looks good too. As a system admin, I like powershell but a BAT file will do the same exact thing.
You just have to ask yourself do you like blue or black :)
You just have to ask yourself do you like blue or black :)
lol.
Btw, I think there are some questions left open for you.
Btw, I think there are some questions left open for you.
ASKER
Sorry for the ignorance, as I'm not a system admin, but how would I save and schedule a powershell script?
There is no such thing!
Here is a great tutorial, no sense in rewriting it. Its the stuff i learned with too lol
http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler
Once you have done this a couple of times, it becomes very easy. As far as saving the script, somewhere on a central server would be the best. Maybe make a powershell folder somewhere on the desktop etc.. As long as the pcs that are running it connect to the domain that is :)
Here is a great tutorial, no sense in rewriting it. Its the stuff i learned with too lol
http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler
Once you have done this a couple of times, it becomes very easy. As far as saving the script, somewhere on a central server would be the best. Maybe make a powershell folder somewhere on the desktop etc.. As long as the pcs that are running it connect to the domain that is :)
BTW also you may want to consider just leaving the newest three "x" folders in the folder, or the like then you don't need to actually do anything with the dates as such, e.g. in batch file:
dir /a-d /o-d /b
will show you the files in date order with newest at the top. You can then skip certain number of those and do something on the others,
So that will echo the files to delete. Remove the word echo to delete them:
@echo off
cd /d "C:\mybackupdir"
for /f "skip=7 delims=" %%f in ('dir /a-d /o-d /b *.bak') do (
echo DEL "%%~f"
)
Steve
dir /a-d /o-d /b
will show you the files in date order with newest at the top. You can then skip certain number of those and do something on the others,
So that will echo the files to delete. Remove the word echo to delete them:
@echo off
cd /d "C:\mybackupdir"
for /f "skip=7 delims=" %%f in ('dir /a-d /o-d /b *.bak') do (
echo DEL "%%~f"
)
Steve
I use this everyday for my backup files--you can set you directories set Dir1=Y:; in your case you want this to be "D:\Documents\Google Drive\xTuple Backups" and how many files to keep skip=4--in your case you want to have skip=5
set Dir1="D:\Documents\Google Drive\xTuple Backups":
for /F "tokens=* skip=5" %%A in ('dir /b /a-d /o-d "%Dir1%\*.bak"') do del "%Dir1%\%%~A"
set Dir1="D:\Documents\Google Drive\xTuple Backups":
for /F "tokens=* skip=5" %%A in ('dir /b /a-d /o-d "%Dir1%\*.bak"') do del "%Dir1%\%%~A"
ASKER
The backup creates 2 files with the following extentions
.backup (this is the database)
.sql (there are the globals for the database)
How would that work in the batch file. I want to use the batch file since the backup script is also a batch file.
.backup (this is the database)
.sql (there are the globals for the database)
How would that work in the batch file. I want to use the batch file since the backup script is also a batch file.
lionelmm -- see post above yours too ... I use the same too and unless there is a chance that backup files dates get changed afterwards then works flawlessly.
steve
steve
If they are both in the same directory use this
set Dir1="D:\Documents\Google Drive\xTuple Backups":
for /F "tokens=* skip=5" %%A in ('dir /b /a-d /o-d "%Dir1%\*.backup"') do del "%Dir1%\%%~A"
for /F "tokens=* skip=5" %%A in ('dir /b /a-d /o-d "%Dir1%\*.sql"') do del "%Dir1%\%%~A"
set Dir1="D:\Documents\Google Drive\xTuple Backups":
for /F "tokens=* skip=5" %%A in ('dir /b /a-d /o-d "%Dir1%\*.backup"') do del "%Dir1%\%%~A"
for /F "tokens=* skip=5" %%A in ('dir /b /a-d /o-d "%Dir1%\*.sql"') do del "%Dir1%\%%~A"
@Gerhardpet,
first quick google says you can't do that, use a for loop or just call the forfiles twice... :-(
first quick google says you can't do that, use a for loop or just call the forfiles twice... :-(
ASKER
Ok I got the batch file to work as per @ robert_schutt
This is my batch file
The way it is now it will only delete *.backup files
This is my batch file
forfiles /p "C:\Users\test\xTuple Backups" /s /d -5 /m *.backup /c "cmd /c del @file"
How can I add anther file extension? I also what to delete *.sqlThe way it is now it will only delete *.backup files
ASKER
Thanks @ robert_schutt
You posted just before I did...I saw your post after submitting.
Calling the for the files twice works.
You posted just before I did...I saw your post after submitting.
Calling the for the files twice works.
Should be exactly what you need.