Last file in folder, wrong output

Welcome Forums General PowerShell Q&A Last file in folder, wrong output

Viewing 4 reply threads
  • Author
    Posts
    • #28225
      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      When I perform this command on the command line, I receive the correct response

      $Outputfiles = Get-ChildItem ‘D:\TST\log\input’ -Recurse | where-object LastWriteTime | select -First 1

      When I do this in a script I always receive the same result, even there are new files.

      Any idea?

    • #28226
      Participant
      Topics: 0
      Replies: 669
      Points: 0
      Rank: Member

      I’m a little puzzled why you’re using where-object. I’d have thought if you wanted the last file in the folder you would sort on LastWriteTime rather than just test it exists

    • #28227
      Participant
      Topics: 16
      Replies: 108
      Points: 0
      Rank: Member

      Can we see the code – please

    • #28230
      Participant
      Topics: 1
      Replies: 92
      Points: 2
      Rank: Member

      It seems the OP has assumed that the LastWriteTime property means “this is the last file that was written to in the folder.” If you just want the most recently written file, use Sort-Object instead of Where-Object:

      Get-ChildItem 'D:\TST\log\input' -Recurse -File |
          sort lastwritetime -Descending |
          select -First 1
    • #28261
      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      thank you! the problem was caused due to the fact that after I moved the file from another location the modify time was not changed.

Viewing 4 reply threads
  • The topic ‘Last file in folder, wrong output’ is closed to new replies.