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.
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.
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 offSET NCMPATH=\\server\share\SolarWinds\Configuration Management\Config-Archive\Importedclsecho **********************echo * Orion NCM Cleanup *echo **********************echo.echo.echo When typing a folder, a ? or * is allowed [ie: *-?-2011]echo.:GETNAMEset /p TODELETE=Enter the folder name to delete:IF (%TODELETE%)==() GOTO GETNAMEecho.echo Your about to delete all folders named: %TODELETE%echo From path: %NCMPATH%echo.:QUESTIONSET /P ANSWER=Continue? [y/n]:IF (%ANSWER%)==() GOTO QUESTIONIF %ANSWER%==n GOTO NOIF %ANSWER%==y GOTO RUNGOTO QUESTION:: Run the purge:RUNecho Purging %TODELETE% folders..please wait!for /D /R "%NCMPATH%" %%i IN (%TODELETE%) DO (RD /s /q "%%i")echo GOTO EXIT:NOecho.echo If you wish to change the path, edit this BAT file and update the NCMPATH on 2nd linepause: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 environmentSET NCMPATH=\\server\share\SolarWinds\Configuration Management\Config-Archive\Imported:: DO NOT EDIT BELOW:: Get current yearset year=%DATE:~10,4%:: Get current month while removing the 0 paddingset /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 paddingset /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 necessaryIF %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.
Awesome Steven, well if I am teh only one with a request...I guess I'd say Powershell. I appreciate your time for sure!