<pre class="languageName:nocontrols" name="code">
code
</pre>
indent paragraph:
<div class="indentparagraph">
content goes here
</div>
Monday, January 28, 2008
A Groovy sample
Sometimes, I use MS visual studio to write programs. MS VS automatically generates additional files and directories which are not necessary when I release my code. For example, debug directory and .ncb file. When I finish my program, I want to delete these unnecessary files/directories. To delete them manually is not a good choice considering number of programs I have. So I decide to write a Groovy script to do this task and following is my code.
/** * This Groovy script is used to delete directories named 'debug'. * Usually, this directory is generated by MS Visual Studio when * you compile your code in debug mode. After you release your * code, debug information can be discarded. Or else, it occupies * non-trivial amount of hard disk space. * Moreover, MS VS generates .ncb files. * Besides debug directory and .ncb file, you can easily change * the pattern of files/directories you want to delete. */ def baseDir = 'E:\\my_program' dirPattern = /[d|D][e|E][b|B][u|U][g|G]/ fileNamePattern = /.*\.ncb$/ /** This closure delete a specified directory or file * Note: here File means a regular file or a directory. */ deleteFile= { currentObj -> if( currentObj.isDirectory() ){ currentDir.eachFile{ currentFile -> if( currentFile.isFile() ){ //first file if( currentFile.delete() == false ) println "file " + currentFile + " failed to be deleted" }else if( currentFile.isDirectory() ){ //then recursively delete dirs deleteDir( currentFile ) } } } //delete this dir if( currentObj.delete() == false ) println "dir " + currentDir + " failed to be deleted" } /** delele directories corresponding to pattern specified by variable * dirPattern. */ deleteDirs ={ _baseDir -> _baseDir.eachDir{ currentDir -> def dirName = currentDir.getName() if( dirName ==~ dirPattern){ //find it, just delete it deleteFile(currentDir) }else//recursivly look for it deleteDirs(currentDir) } } /** delele files corresponding to pattern specified by variable * fileNamePattern. */ deleteFiles ={ _baseDir -> _baseDir.eachFile{ currentFile -> if( currentFile.isFile() ){ def fileName = currentFile.getName() println fileName if( fileName ==~ fileNamePattern ){ //find it, just delete it deleteFile(currentFile) } }else//recursivly look for it deleteFiles(currentFile) } } /** Sample usage */ deleteDirs(new File(baseDir)) deleteFiles(new File(baseDir));
Subscribe to:
Posts (Atom)