Return the recent file among other by subdirectory

by Feb 8, 2013


Some users synchronize their data with synctoy (computer <-> server). I just want to scan folders to find their synctoy files (synctoyxxxxx.dat) and compare its LastWrite Time to compare to de date of the day.

I have 2 problems now:

-1) Synctoy generate several .Dat files. I want to get the more recent. I easily found the command for this but it runs with a single directory. When it exists several subfolders containing .dat files, the script returns just the last one .dat file ! I want to return the more recent .dat file for each folder containing a .dat file and write-host (or log) "more recent file – directory" for each.

-2) How return the number of days beetwen today and the more recents .dat file ?

 This is thmy script below (for 1 subfolder)

$PathFile =  "\networkSecteur Est"

$File = gci $PathFile -Include synctoy*.dat -Recurse -Force -ErrorAction SilentlyContinue |

            Sort-Object {$_.lastwritetime} -Descending | Select-Object -First 1

I tried a lot of test with Foreach and If but more I try and more it seems to be difficult for me….

Thanks a lot for your answers..