Code creating folder structure

Creating multi hierarchy folder structure with code

Let say that you need to initialize several folders with one and the same hierarchy. You can create the folders one by one manually or use code for this purpose:
Code advantages:

  • you can manage history
  • you can have comments - if you forget the folder purpose or if you need to do easy knowledge transfer
  • it can be run on several machines keeping one and the same structure(never mind of the operating system).

This is sample groovy code: you can change it depending on your needs.

Note: list structure is prepared and extracted from excel:


listInit = [
       'Project',
       [
               ['0.concept',['0.ideas', '1.inspirations', '2.Info']],
               ['1.management', ['0.projectPlan', '1.Milestones',]],
               ['2.design', ['0.interface', '1.themes', '2.branding',]],
               ['3.functional', [ '0.Requirements', '2.FuncAnal',]],
               ['4.technical', ['0.codes', '1.implementation', '2.connections',]],
               ['5.production', ['0.deployments', '1.publications',]],
               ['6.monetization', ['0.affiliate', '1.advertising', '2.Sales',]],
               ['7.marketing', ['0.advertising', '1.seo', '2.examples',]],
               ['8.tests', ['0.defects','1.Unit Test','2.Func Test']],
               ['9.archive', ['0.old files', '1.old versions',]]]
]
def subroot = ''
project = listInit[0]
def root = /C:\Users\myuser\Downloads/ + '\\' + project
makeFodler(root)
println '################################ project: ' + project
list = listInit[1]
println '################################ list: ' + list
list.each { it->
       println '---' + root + '\\' + it[0]
       subroot = root + '\\' + it[0]
       println subroot
       makeFodler(subroot)
       it[1].each { it1->
               def fol
               fol = subroot + '\\' + it1
               println '------' + fol
               makeFodler(fol)
       }
}
def printList(def elem) {
       if (elem.getClass() == ArrayList) {
               elem.each { it1-> println it1 }
       } else {
               println elem
       }
}
def makeFodler(def path) {
       def subdir = new File(path)
       subdir.mkdir()
}

Related Article