Created
November 5, 2011 08:36
-
-
Save uehaj/1341279 to your computer and use it in GitHub Desktop.
指定したディレクトリ配下の全ファイルをトラバースしてファイル情報をSqliteDBに突っこんで、クエリをかけて抽出(by uehaj)
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
// 指定したディレクトリ配下の全ファイルをトラバースしてファイル情報を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