Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Challenge

Detect the newest file in a specific folder and pass it as an argument to another process. The first argument to your program will be the folder, and the second argument will be the full path to the process.

Please specify the platform(s) your code works on.

Examples

Windows

newestfile "C:\Public_domain_movies\" "C:\Mplayer\mplayer.exe"

Linux

./newestfile /home/user /bin/ls
share|improve this question
1  
@Geobits I expect the most recent timestamp no matter what, I removed the misleading part. –  Eduard Florinescu Jul 2 at 15:42
    
Actually I think it could have added a very interesting twist to have to figure out when the program was last run. –  Martin Büttner Jul 2 at 15:48
    
@Ventero Yes, no recursive scanning just the specified folder –  Eduard Florinescu Jul 2 at 17:34
2  
The most recently modified file or the most recently created file? –  Joey Jul 2 at 18:05
1  
@Joey The most recently modified –  Eduard Florinescu Jul 2 at 18:09

5 Answers 5

Zsh (15)

Only tested on Linux, but it should work on other platforms too.

$2 $1/*(.om[1])
share|improve this answer

Linux, Ruby, 52 characters

exec$*[1],Dir[$*[0]+"/*"].sort_by{|a|test(?M,a)}[-1]

I'm not sure if I undestood the specifications correctly, so if this doesn't work as intended, please let me know. I only tested this on Linux, but in theory it should run on Windows, too.

share|improve this answer

linux - bash+sed - 18 bytes

$2 `ls -t $1|sed q`

Does not work if the arguments contain blanks.

share|improve this answer

Windows, PowerShell, 35

&$args[1](ls $args[0]|sort *w*)[-1]
share|improve this answer

Windows, cmd, 54

for /f "delims=" %%A in ('dir/b/o-d %1')do %2 %%A&exit

Caveats:

  • Closes the current shell session
  • Will consider directories (as do most other solutions)
share|improve this answer
    
You don't need the options string "delims=" –  nderscore Jul 2 at 18:34
    
Well, have fun with file names with spaces, then. –  Joey Jul 2 at 18:34
    
This doesn't work if the file has spaces in it –  Eduard Florinescu Jul 2 at 19:12
1  
What if you add /x parameter for short names? FOOBAR~1.TXT –  nderscore Jul 2 at 19:13
    
@nderscore: Not every file has to have a short name. –  Joey Jul 2 at 19:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.