LastWriteTime not seeing some files.

Welcome Forums General PowerShell Q&A LastWriteTime not seeing some files.

Viewing 5 reply threads
  • Author
    Posts
    • #226038
      Participant
      Topics: 2
      Replies: 2
      Points: 15
      Rank: Member

      I am creating a simple script to copy files and directories with modified date gt  Feb. 5, 2018.  To test I am simply trying to get a list of files that match.  The script is not identifying files I know have dates gt the date I have in my script.
      What’s weird to me is this outcome is only for certain files.
      Any ideas?

      Example:
      Get-Item E:\webapps\xml\web.xml | Foreach {[datetime]::parse($_.LastWriteTime)}
      Result = Tuesday, February 13, 2018 10:51:54 AM

      My script does not return this file when I point to the directory containing this file.

      $change = [datetime]::Parse("05/02/2018")
      get-childitem "E:\webapps\xml\" -recurse |
      where-object {$_.mode -notmatch "d"} |
      where-object {$_.lastwritetime -gt $change} |
      format-table lastwritetime, fullname -autosize | out-file e:\TEMP\files.log -append

    • #226059
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Hmmm … I checked it – of course with other files and dates – but it works for me jsut as expected …

      BTW: Get-ChildItem has a parameter -File to list only files … no need to use an extra Where-Object filter for that. 😉

      … I almost forgot … you should use format cmdlets (like Format-Table) always as the last cmdlet in the pipeline … if you want to pipe the outpur to a file you should use Select-Object instead.

    • #226062
      Participant
      Topics: 17
      Replies: 1872
      Points: 3,640
      Helping Hand
      Rank: Community Hero

      Take a look at the Powershell Gotchas in Free Resources. Format-Table stops the pipeline, so nothing is going to Out-File. Recommend using Export-CSV as you are returning an objects, but your code does work:

    • #226068
      Participant
      Topics: 2
      Replies: 2
      Points: 15
      Rank: Member

      I get what folks are saying but it’s not working for some files.   Below is a test I just ran and the output.
      For testing purposes I created a new text file (test.txt) in the same directory so it would have a current time stamp.

      echo "web.xml lastwritetime"
      Get-Item E:\webapps\xml\web.xml | Foreach {[datetime]::parse($_.LastWriteTime)}
      echo "list of files & lastwritetime"
      $change = [datetime]::Parse("05/02/2018")
      get-childitem "E:\webapps\xml\" -recurse |
      where-object {$_.lastwritetime -gt $change}

      Results:  They show the lastwritetime for the web.xml, but it does not show up in the list of files for the query results.

      web.xml lastwritetime

      Tuesday, February 13, 2018 10:51:54 AM

      list of files & lastwritetime

      LastWriteTime : 5/6/2020 11:19:53 AM
      Length : 0
      Name : test.txt

       

    • #226077
      Participant
      Topics: 17
      Replies: 1872
      Points: 3,640
      Helping Hand
      Rank: Community Hero

      Is the file hidden? If it is hidden, you’ll need to add the -Force param to get the file. If it’s not hidden, does the file show up if you do not filter on date?

    • #226080
      Participant
      Topics: 2
      Replies: 2
      Points: 15
      Rank: Member

      Is the file hidden? If it is hidden, you’ll need to add the -Force param to get the file. If it’s not hidden, does the file show up if you do not filter on date?

      File properties show it’s normal, not hidden.  I have hundreds of files and about 8 sub directories in the same path.
      When I query files in the directory it shows the same as the test.txt file I created.   I am totally confused by this one.

      LastWriteTime : 5/6/2020 11:19:53 AM
      Length : 0
      Name : test.txt
      LastWriteTime : 2/13/2018 10:51:54 AM
      Length : 42249
      Name : web.xml

      I just noticed something strange in my test output though.   There are 3 blank lines below the returned results, like it’s indicating there was another result but it’s not displayed.   Notice below test.txt the blank lines, doesn’t really show on the forum, but there are 3 blank lines in my console window.

      web.xml lastwritetime
      Tuesday, February 13, 2018 10:51:54 AM
      list of files & lastwritetime

      LastWriteTime : 5/6/2020 11:19:53 AM
      Length : 0
      Name : test.txt

      PS C:\Users\myuserid>

      • This reply was modified 7 months ago by M ODonnell.
      • This reply was modified 7 months ago by M ODonnell.
Viewing 5 reply threads
  • The topic ‘LastWriteTime not seeing some files.’ is closed to new replies.