Skip to content

Instantly share code, notes, and snippets.

@uehaj
Created November 5, 2011 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uehaj/1341279 to your computer and use it in GitHub Desktop.
Save uehaj/1341279 to your computer and use it in GitHub Desktop.
指定したディレクトリ配下の全ファイルをトラバースしてファイル情報をSqliteDBに突っこんで、クエリをかけて抽出(by uehaj)
// 指定したディレクトリ配下の全ファイルをトラバースしてファイル情報をSqliteDBに突っこんで、クエリをかけて抽出
@Grab('org.xerial:sqlite-jdbc:3.7.2')
@GrabConfig(systemClassLoader=true)
import groovy.sql.Sql
//Class.forName("org.sqlite.JDBC");
Class.forName("org.sqlite.SQLite")
if (args.length == 0) {
println "Usage: ${this.class} <dir>"
System.exit(0)
}
sql = Sql.newInstance("jdbc:sqlite:test.db", "org.sqlite.JDBC")
sql.execute('DROP TABLE IF EXISTS FILES')
sql.execute('CREATE TABLE FILES (name, size, lastModified)')
dataSet = sql.dataSet('FILES')
dataSet.with { table ->
new File(args[0]).traverse {
table.add(name:it.name, size:it.length(), lastModified:it.lastModified())
}
}
println "---サイズが10以上のファイル---"
dataSet.findAll { it.size > 10 }.each {
println "${it.name}, ${it.size}"
}
println "---tで始まるファイル名のファイル---"
sql.eachRow("SELECT name,size FROM FILES WHERE name like 't%'") {
println "${it.name}, ${it.size}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment