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: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

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

      $change = Get-Date -Day 5 -Month 2 -Year 2018
      $path = 'E:\webapps\xml\'
      Get-ChildItem -Path $path -Filter * -Recurse -File |
          Where-Object {$_.LastWriteTime -gt $change } |
              Format-Table lastwritetime, fullname -autosize

      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: 12
      Replies: 1621
      Points: 2,556
      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:

      PS C:\Users\rasim> Get-Item -Path C:\Scripts\file1.txt
      
      
          Directory: C:\Scripts
      
      Mode                 LastWriteTime         Length Name
      ----                 -------------         ------ ----
      -a---           1/29/2020 11:07 PM            157 file1.txt
      
      PS C:\Users\rasim> [datetime]::Parse("1/28/2020")
      
      Tuesday, January 28, 2020 12:00:00 AM
      
      PS C:\Users\rasim> get-childitem "C:\Scripts\" -recurse |
      where-object {$_.mode -notmatch "d"} |
      where-object {$_.lastwritetime -gt $change}
      
      
          Directory: C:\Scripts
      
      Mode                 LastWriteTime         Length Name     
      ----                 -------------         ------ ----     
      -a---           1/29/2020 11:07 PM            157 file1.txt
      -a---           1/29/2020 11:07 PM            729 file2.txt
      -a---           1/30/2020  9:50 AM            140 ls.txt   
      -a---           3/16/2020  4:46 PM         617810 mfile.csv
      -a---           3/12/2020  9:11 AM            374 temp.csv
      -a---           4/14/2020 11:38 AM           1800 temp.xml
      -a---            4/3/2020 10:06 AM          10346 test.csv
      -a---           4/28/2020  4:20 PM           8860 test.xlsx
      -a---           3/16/2020  4:46 PM          10893 tfile.txt
      
    • #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: 12
      Replies: 1621
      Points: 2,556
      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 3 weeks, 5 days ago by M ODonnell.
      • This reply was modified 3 weeks, 5 days ago by M ODonnell.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.