locked
Open the most recent file RRS feed

  • Question

  • Hello everybody,

    In a folder, I have got several sub-folders, of which a certain number contain each a file named johndoe.doc

    I should like to open the johndoe.doc file from the most recent sub-folder. That is easy to find as the sub-folders are named from their dates of creation. I put a filter on the sub-folder name, because some of them have a more specific indication after the date, and they are out of scope here.

    This is how I could list the johndoe.doc files. The first listed one is the one I want to open.

    Get-ChildItem | Where-Object Name -like ????-??-?? | 
    Sort-Object -Property Name -Descending | 
    Get-ChildItem | Where-Object Name -eq johndoe.doc
    
    

    (all that is supposed to be on the same line, but more readable on three)

    So, what you could help me to do is stop the script as soon as a file is found (useless to list the other ones), and to open it, if possible automatically.

    Monday, October 22, 2018 6:00 PM

Answers

  • How about this?

    Get-ChildItem | Where-Object Name -like ????-??-?? | 
    Sort-Object -Property Name -Descending | 
    Get-ChildItem | Where-Object Name -eq johndoe.doc | Foreach {exit}


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Gloops Monday, October 22, 2018 8:24 PM
    Monday, October 22, 2018 6:17 PM
  • The answer you marked does not do what you asked.

    The following opens the document named "johndoe.doc"  in the most recent subfolder of a root folder.

    Get-ChildItem c:\rootpath -Directory -Recurse |
        Sort CreationTime |
        select -Last 1 |
        ForEach-Object{ 
            # open the file in your program of choice
            Start-Proess johndoe.doc -WorkingDirectory $_.Fullname
        }

    If your question is other than what you wrote then please fix your question.


    \_(ツ)_/

    • Marked as answer by Gloops Monday, October 22, 2018 8:44 PM
    Monday, October 22, 2018 8:34 PM
  • You're correct. It's not the answer. I read the last part of the question too quickly and completely skipped the "open the file" part.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Hello,

    Right, but as I said just after, I corrected it like this :

    ForEach { $_.Fullname; & $_.Fullname ; exit; }

    the first appearance of $_.Fullname displays the path, and the second one opens it, thanks to the & character.

    jrv's answer is for sure cleaner, but yours was quicker ;)

    • Marked as answer by Gloops Tuesday, October 23, 2018 3:55 PM
    Tuesday, October 23, 2018 3:49 PM

All replies

  • How about this?

    Get-ChildItem | Where-Object Name -like ????-??-?? | 
    Sort-Object -Property Name -Descending | 
    Get-ChildItem | Where-Object Name -eq johndoe.doc | Foreach {exit}


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Gloops Monday, October 22, 2018 8:24 PM
    Monday, October 22, 2018 6:17 PM
  • Oh, clever !

    To open it, I presume I have to put the name of the sub-folder in a variable ?

    **

    Got it !

    ForEach {& $_.Fullname ; exit; }

    Thank you.

    • Edited by Gloops Monday, October 22, 2018 7:15 PM
    Monday, October 22, 2018 6:46 PM
  • What does "open" it mean?  Open in notepad?  Open in Excel? 

    To find the most recent file in the most recent folder.

    Get-ChildItem c:\rootpath -Directory -Recurse |
        Sort CreationTime |
        select -Last 1 |
        ForEach-Object{ 
            # open the file in your program of choice
            notepad johndoe.doc
        }


    \_(ツ)_/

    Monday, October 22, 2018 8:08 PM
  • Well, no, open with the default application for the file type.

    And for that,

    & $_.Fullname

    does the job.

    I added it a second time before, without the ampersand, to display the path.

    Monday, October 22, 2018 8:27 PM
  • The answer you marked does not do what you asked.

    The following opens the document named "johndoe.doc"  in the most recent subfolder of a root folder.

    Get-ChildItem c:\rootpath -Directory -Recurse |
        Sort CreationTime |
        select -Last 1 |
        ForEach-Object{ 
            # open the file in your program of choice
            Start-Proess johndoe.doc -WorkingDirectory $_.Fullname
        }

    If your question is other than what you wrote then please fix your question.


    \_(ツ)_/

    • Marked as answer by Gloops Monday, October 22, 2018 8:44 PM
    Monday, October 22, 2018 8:34 PM
  • Oh, yes, that avoids an exit on the first one, we can say it is more elegant.

    I presume you meant Start-Process.

    Monday, October 22, 2018 8:44 PM
  • You're correct. It's not the answer. I read the last part of the question too quickly and completely skipped the "open the file" part.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, October 23, 2018 3:05 PM
  • You're correct. It's not the answer. I read the last part of the question too quickly and completely skipped the "open the file" part.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Hello,

    Right, but as I said just after, I corrected it like this :

    ForEach { $_.Fullname; & $_.Fullname ; exit; }

    the first appearance of $_.Fullname displays the path, and the second one opens it, thanks to the & character.

    jrv's answer is for sure cleaner, but yours was quicker ;)

    • Marked as answer by Gloops Tuesday, October 23, 2018 3:55 PM
    Tuesday, October 23, 2018 3:49 PM