Note that there are some explanatory texts on larger screens.

plurals
  1. POBash/DOS/PowerShell script to list most recent versions of files?
    primarykey
    data
    text
    <p>We have a list of (let's say 50) reports that get dumped into various folders depending on certain conditions. All the reports have standard names eg. D099C.LIS, D18A0.LIS etc.</p> <p>Sometimes a report can exist in up to 5 different locations, and I need to generate a list of all the locations of the most recent version of each report.</p> <p>I can do it easily using code, or redirecting "dir" or "ls" output into a text file and then manipulating it in Excel, but I'd prefer a simpler (hopefully a one-liner) solution either using DOS, bash, or PowerShell.</p> <p>The best I've come up with so far in PowerShell (I've done something similar using bash) is:</p> <pre><code>ls -r -fi *.lis | sort @{expression={$_.Name}}, @{expression={$_.LastWriteTime};Descending=$true} | select Directory, Name, lastwritetime </code></pre> <p>That will recursively list all files with *.lis extension, then sort it by name (asc) and date (desc), and then display the directory, name, and date. </p> <p>This gives this sort of output:</p> <pre><code>C:\reports\LESE D057A.LIS 28/01/2009 09:00:43 C:\reports\JCSW D057A.LIS 27/01/2009 10:50:21 C:\reports\ALID D075A.LIS 04/02/2009 12:34:12 C:\reports\JCSW D075B.LIS 05/02/2009 10:07:15 C:\reports\ALID D075B.LIS 30/01/2009 09:14:57 C:\reports\BMA3 D081A.LIS 01/09/2008 14:51:36 </code></pre> <p>What I obviously need to do now is remove the files that aren't the most recent versions, so that the output looks like this (not too worried about formatting yet):</p> <pre><code>C:\reports\LESE D057A.LIS 28/01/2009 09:00:43 C:\reports\JCSW D075B.LIS 05/02/2009 10:07:15 C:\reports\BMA3 D081A.LIS 01/09/2008 14:51:36 </code></pre> <p>Anyone have any ideas?</p> <p>[edit] Some good ideas and answers to this question. Unfortunately I can't mark all as accepted, but EBGreen's (edited) answer worked without modification. I'll add working solutions here as I verify them.</p> <p>bash:</p> <pre><code> ls -lR --time-style=long-iso | awk 'BEGIN{OFS="\t"}{print $5,$6,$7,$8}' | grep ".LIS" | sort -k4 -k2r -k3r | uniq -f3 ls -lR --time-style=long-iso | awk 'BEGIN{OFS="\t"}{print $5,$6,$7,$8}' | grep ".LIS" | sort -k4 -k2r -k3r | awk '!x[$4]++' </code></pre> <p>PowerShell:</p> <pre><code> ls -r -fi *.lis | sort @{expression={$_.Name}}, @{expression={$_.LastWriteTime};Descending=$true} | select Directory, Name, lastwritetime | Group-Object Name | %{$_.Group | Select -first 1} ls -r . *.lis | sort -desc LastWriteTime | group Name | %{$_.Group[0]} | ft Directory,Name,LastWriteTime ls -r -fi *.lis | sort @{expression={$_.Name}}, @{expression={$_.LastWriteTime};Descending=$true} | unique | ft Directory,Name,LastWriteTime </code></pre>
    singulars
    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