Does anyone have a script, perl, batch or anything, that will clean up the file/folder structure automatically? I am on 6.1 currently so not sure if this much asked for request made it to v7.
Does anyone have a script, perl, batch or anything, that will clean up the file/folder structure automatically? I am on 6.1 currently so not sure if this much asked for request made it to v7.
Ok, so wrote my own. It is not perfect, but works good enough.
First one will run interactively, and you put in the date name of the folder to remove, ie: purge all folders named 3-6-2012 or all of march 2012 by using wildcards like 3-*-2011. This one runs just fine, no issues.
@echo off
SET NCMPATH=\\server\share\SolarWinds\Configuration Management\Config-Archive\Imported
cls
echo **********************
echo * Orion NCM Cleanup *
echo **********************
echo.
echo.
echo When typing a folder, a ? or * is allowed [ie: *-?-2011]
echo.
:GETNAME
set /p TODELETE=Enter the folder name to delete:
IF (%TODELETE%)==() GOTO GETNAME
echo.
echo Your about to delete all folders named: %TODELETE%
echo From path: %NCMPATH%
echo.
:QUESTION
SET /P ANSWER=Continue? [y/n]:
IF (%ANSWER%)==() GOTO QUESTION
IF %ANSWER%==n GOTO NO
IF %ANSWER%==y GOTO RUN
GOTO QUESTION
:: Run the purge
:RUN
echo Purging %TODELETE% folders..please wait!
for /D /R "%NCMPATH%" %%i IN (%TODELETE%) DO (RD /s /q "%%i")
echo
GOTO EXIT
:NO
echo.
echo If you wish to change the path, edit this BAT file and update the NCMPATH on 2nd line
pause
:EXIT
Second one was setup to run in a Task Scheduler. It will get today's date and go back 1 month and purge that date. So the idea being once you are cleaned up to only 1 months worth laying around, you can run this daily and it will purge out the folders older than 30 days from the date run. The only issue I came up with as I was testing it, is if you want to purge 30 days older than March 30th, well...February 30th doesn't exist...and you won't be able to purge January 29,30,31 b/c February 29,30,31 doesn't exist. THis is just because how I am doing the date (- 1 month), not working with days. If I get more time, I will work on a true go back 30 days version.
@echo off
:: Script will go back 1 month from today's date and delete any folders it finds.
:: Adjust NCMPATH below to match your environment
SET NCMPATH=\\server\share\SolarWinds\Configuration Management\Config-Archive\Imported
:: DO NOT EDIT BELOW
:: Get current year
set year=%DATE:~10,4%
:: Get current month while removing the 0 padding
set /a m1=%DATE:~4,1%
IF %m1% ==0 (set month=%DATE:~5,1%) ELSE (set month=%DATE:~4,2%)
:: Get current day while removing the 0 padding
set /a d1=%DATE:~7,1%
IF %d1% ==0 (set day=%DATE:~8,1%) ELSE (set day=%DATE:~7,2%)
:: Set month to 1 month back and year if necessary
IF %month% ==1 (set /a year=%year%-1)
IF %month% ==1 (set /a month=12) ELSE (set /a month=%month%-1)
rem echo Purging all %month%-%day%-%year% folders from %NCMPATH%\ recursively.
echo.
for /D /R "%NCMPATH%" %%i IN (%month%-%day%-%year%) DO (RD /s /q "%%i")
Hope this helps. If you have better coding skills and can fix or improve it, I'd love to see it!
Wow, that's impressive! We are always looking for developers, don't you want to join SWI?
Jiri
LOL..yes, I am in! Well as long as you like a half baked dos batch writer!
I'm with Jiri on this one; impressive! I write code in a lot of languages but I always love it when someone dusts off the old school batch files. What language would you like to see this written in, Casper? Perl, Powershell, Python? Let me know and I'll hammer it out for you.
Hey Casper, no worries - it's my pleasure. Here are the two scripts in Powershell. You'll have to fix the path and remove the -WhatIf option from the Remove-Item cmdlet in order to actually go through with the deletion in both scripts. Let me know if you run into any problems or if I missed something.
Edit: I was missing the $ncmpath variable in the second, smaller script. All fixed now.
Pretty significant bug in the first script - there was NO FILTER. I've applied the $todelete filter to the things getting removed now. I guess I'd tested it manually and it didn't make it into the code. Screenshot and attachment updated.
Yeah, the date math is especially tasty. If you want a good primer, this task-based guide is really good. It got me hooked on it on Saturday afternoon.
What Can I Do With Windows PowerShell? - A Task-Based Guide to Windows PowerShell Cmdlets
SolarWinds solutions are rooted in our deep connection to our user base in the THWACK® online community. More than 195,000 members are here to solve problems, share technology and best practices, and directly contribute to our product development process.