rakkad
asked on
vbs script to delete folders/files which are present for more than 30 days
We have log files which occur daily in the format c:\test\yyyy\mm\dd date format in one of our systems
However, I need to delete previous months folders/files so the most recent files/folders are present
Thanks
However, I need to delete previous months folders/files so the most recent files/folders are present
Thanks
So, do you want to delete all days and the month folder where the month is less than the current month?
Or do you need to keep say exactly 30 days of folders, and delete back from there?
~bp
Or do you need to keep say exactly 30 days of folders, and delete back from there?
~bp
You don't need a VBScript, you could use forfiles command. The command could be as simple as below:
forfiles -p "C:\what\ever" -s -m *.* -d 30 -c "cmd /c del @path"
forfiles -p "C:\what\ever" -s -m *.* -d 30 -c "cmd /c del @path"
ASKER
~bp
Yes I want to delete all files where the month is less than the current month
Thanks
Yes I want to delete all files where the month is less than the current month
Thanks
Here's a BAT script approach, which does what you described. Run it from the command prompt. Currently the RD command will just ECHO to the screen to allow testing so you can validate it works. If the folders shown look right then remove the word ECHO before the RD command and run for real.
Keep in mind all days folders and files in that month folder will be removed.
Keep in mind all days folders and files in that month folder will be removed.
@echo off
setlocal EnableDelayedExpansion
REM Define base folder for log folders
set BaseDir=B:\EE\EE28517538\test
REM Get current Month in YYYYMM format
set CurrentMonth=%DATE:~-4%%DATE:~-10,2%
set CurrentMonth=%CurrentMonth: =0%
REM Get month folders, and if older than current month remove them
for /d %%Y in ("%BaseDir%\????") do (
set YYYY=%%~nY
for /d %%M in ("%%~Y\??") do (
set FolderMonth=%%~nY%%~nM
if !FolderMonth! LSS %CurrentMonth% (
ECHO rd /q /s "%%~M"
)
)
)
~bp
ASKER
Thanks ~bp
Do you have this as a vbscript format
Can you describe how the script works ?
Thanks
Do you have this as a vbscript format
Can you describe how the script works ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the vbscript works brilliant, I would like to include some error trapping, i.e. if the code fails can you include any error trapping messages
Thanks
Thanks
ASKER
I need to modify the script yet again, so... in addition to removing the month folder which is not the current month, I would also like to remove dd folders within the current month so the most recent dd folder is present
thanks once again.. :-)
thanks once again.. :-)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi ~bp
I would like to include some error trapping, i.e. if the code fails can you include any error trapping messages
Thanks
I would like to include some error trapping, i.e. if the code fails can you include any error trapping messages
Thanks
What types of errors do you want to trap for?
~bp
~bp
ASKER
say if the script fails ?
Well, if the script itself fails, this it would not be able to trap that condition, you would have to trap that in whatever launched the script initially.
~bp
~bp
ASKER
Or can you include a log file to indicate which files have been removed ?
Thanks
Thanks
Simple way to get that (since I was already writing the folders removed to standard output) would be to send the output from the script to a file when you run it, something like this:
cscript //nologo yourscript.vbs >c:\logs\yourscript.txt
~bp
cscript //nologo yourscript.vbs >c:\logs\yourscript.txt
~bp
this is removing all zip files older than 6 days from this d:\backup folder.
--
Open in new window
--Sascha