Created
May 21, 2014 15:14
-
-
Save AHarazim/1b3585cda22124ab6ade to your computer and use it in GitHub Desktop.
get content of folder to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.io.FileType | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
} | |
task rename(type: Copy) { | |
from 'in' | |
into 'out' | |
// Use a closure to map the file name | |
rename { String fileName -> | |
generateMD5(fileName)+fileExtension(fileName); | |
} | |
} | |
task createList2() { | |
File srcDir | |
File list = new File("root.js") | |
list.createNewFile() | |
list.write("[") | |
FilenameFilter fnf = {d, f-> f ==~ '\\.*' } as FilenameFilter | |
// Create a file collection using a closure | |
FileCollection collection = files { srcDir.listFiles([accept:{d, f-> f.indexOf(".") > 0 }] as FilenameFilter) } | |
Collection c = [] | |
def dirs = [] | |
def dir = new File(".") | |
dir.eachFileRecurse (FileType.DIRECTORIES) { file -> | |
dirs << file | |
} | |
dirs.each { | |
println it.path | |
createJSON(it.name) | |
c.add(it) | |
} | |
if(!c.isEmpty()) | |
{ | |
Object last = c.pop(); | |
c.each { list <<"{\"folder\":\"" + it +"\"}," } | |
list <<"{\"folder\":\"" + last +"\"}" | |
} | |
list << "]" | |
} | |
def createJSON(String folder) | |
{ | |
File srcDir | |
File list = new File(folder+".js") | |
list.createNewFile() | |
list.write("[") | |
//FilenameFilter fnf = {d, f-> f ==~ /.*.docx/ } as FilenameFilter | |
// Create a file collection using a closure | |
FileCollection collection = files { srcDir.listFiles([accept:{d, f-> f.indexOf(".docx") < 0 }] as FilenameFilter) } | |
Collection c = [] | |
srcDir = file(folder) | |
println "Contents of $srcDir.name" | |
collection.collect { it.name }.sort().each { c.add(it); } | |
if(!c.isEmpty()) | |
{ | |
Object last = c.pop(); | |
c.each { list <<"{\"name\":\"" + it +"\",\"text\":\"\"}," } | |
list <<"{\"name\":\"" + last +"\",\"text\":\"\"}" | |
} | |
list << "]" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment