Note that there are some explanatory texts on larger screens.

plurals
  1. POGroovy read most recent file in directory
    primarykey
    data
    text
    <p>I just have a question about writing a function that will search a directory for the most recent log in a directory. I currently came up with one, but I'm wondering if there is a better (perhaps more proper) way of doing this.</p> <p>I'm currently using hdsentinel to create logs on computer and placing the log in a directory. The logs are saved like so:</p> <pre><code>/directory/hdsentinel-computername-date ie. C:/hdsentinel-owner-2010-11-11.txt </code></pre> <p>So I wrote a quick script that loops through certain variables to check for the most recent (within the past week) but after looking at it, I'm question how efficient and proper it is to do things this way.</p> <p>Here is the script: </p> <pre><code>String directoryPath = "D:" def computerName = InetAddress.getLocalHost().hostName def dateToday = new Date() def dateToString = String.format('%tm-%&lt;td-%&lt;tY', dateToday) def fileExtension = ".txt" def theFile for(int i = 0; i &lt; 7; i++) { dateToString = String.format('%tY-%&lt;tm-%&lt;td', dateToday.minus(i)) fileName = "$directoryPath\\hdsentinel-$computerName-$dateToString$fileExtension" theFile = new File(fileName) if(theFile.exists()) { println fileName break; } else { println "Couldn't find the file: " + fileName } } theFile.eachLine { print it } </code></pre> <p>The script works fine, perhaps it has some flaws. I felt I should go ahead and ask what the typical route is for this type of thing before I continue with it.</p> <p>All input is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload