laxmi
laxmi

Reputation: 865

How to extract file properties in groovy?

I have gsp which has table and i need to display created date time and last modified time of each file which in drive.

I am not getting how to retrieve file properties.can any body answer me.

Advance thanks laxmi.P

Upvotes: 14

Views: 13765

Answers (3)

mrhaki
mrhaki

Reputation: 3592

To get access to properties not supported by the Java File API we can parse the output of a 'dir' or 'ls' command:

def file = 'sample.txt'
def cmd = ['cmd', '/c', 'dir', file, '/tc'].execute()
cmd.in.eachLine { line ->
    if (line.contains(file)) {
        def created = line.split()[0]
        println "$file is created on $created"
    }
} 

Upvotes: 4

mrhaki
mrhaki

Reputation: 3592

The result of file.lastModified() is a long we can use to construct a new Date object. We can apply formatting on the Date object. The formatting rules of SimpleDateFormat can be applied.

new File('.').eachFileRecurse { file ->
    println new Date(file.lastModified()).format('EEE MMM dd hh:mm:ss a yyyy')
}

Upvotes: 15

virtualeyes
virtualeyes

Reputation: 11237

You probably want something like:

new File(path-to-your-directory).eachFileRecurse{file->
println file.lastModified()
}

Upvotes: 5

Related Questions