Solved

Get-ChildItem doesnt find file when using data parameter

Posted on 2014-02-18
11
510 Views
Last Modified: 2014-02-20
I'm using the following to search the directory, eventually by using the current date, but it finds no files when I add theLastWriteTime and a date parameter

$latest = (Get-ChildItem -Path "C:\Program Files (x86)\FileZilla Server\Logs" -Filter *.log | ? {$_.LastWriteTime -eq "2/15/2014"})

But when I don't use the LastWriteTime and a date it finds everything in the directory, what am I doing wrong?  The file with the specified date does exist

Thanks
0
Comment
Question by:c7c4c7
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 125 total points
ID: 39868262
You probably need to format the lastwritetime to compare to a string..
? {$_.lastwritetime.tostring("MM/dd/yyyy") -eq "2/15/2014"}

Open in new window

0
 
LVL 28

Expert Comment

by:becraig
ID: 39868264
Your value is not cast as a date, also the date time is for last write is 01/01/2014 01:01 AM.

You can simply use -like and wildcard it.

$latest = (Get-ChildItem -Path "C:\Program Files (x86)\FileZilla Server\Logs" -Filter *.log | ? {$_.LastWriteTime -like "*2/15/2014*"})
0
 
LVL 39

Expert Comment

by:footech
ID: 39868299
It's very unlikely that you have any files with a LastWriteTime that is exactly equal to "2/15/2014".  The timestamp has greater precision than that, and you would have to have an exact match to get a result.  You either need to use other comparison operators (-lt, -ge, etc.), or change your approach.  For example, if you're trying to find files that were last written to on a specific day, you can filter by a range spanning 24 hours (i.e. ? {$_.LastWriteTime -ge "2/15/2014 00:00:00" -and $_.LastWriteTime -lt "2/16/2014 00:00:00"}), or you could extract just the date from the LastWriteTime property (which is a datetime type) and base your comparison off that.
0
 

Author Comment

by:c7c4c7
ID: 39868460
My intention was to just compare the date portion of the LastWriteTime, so that's probably why Subsun's suggestion did not work and footech and begraig's did.

Couple of questions, it makes sense that my comparison did not work as there is more to the LastWriteTime than just a date.  How can I get a definition of the fields in something like LastWriteTime so I don't waste so much time trying things that will never work.

How do I get just the date value in LastWriteTime, I just want access to the log file written the previous days.

Thanks
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39868490
In my code $_.lastwritetime.tostring("MM/dd/yyyy")  actually remove the time portion of the lastwritetime values.. ie, '2/13/2014  12:27 AM, will be converted to '02/13/2014'. So '02/13/2014' wont be equal to '2/13/2014'.. :-)

$_.lastwritetime.tostring("M/dd/yyyy") would have worked for you..

To compare only date value you can try the following.. But when you use -eq operator, only if there is a exact math then you get an output..
 ? {$_.lastwritetime.tostring("dd")  -eq "15"}

Open in new window


You can find more examples from following article..
http://technet.microsoft.com/en-us/library/ee692801.aspx

Also if your intention is to get the newest log file then you can try..
$latest = Get-ChildItem -Path "C:\Program Files (x86)\FileZilla Server\Logs" -Filter *.log | sort lastwritetime -Descending | Select -First 1

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 68

Expert Comment

by:Qlemo
ID: 39868773
You should stay with native dates, and not convert them to strings, for comparing or calculating, so I wouldn't follow the .ToString approach. It is better to have something like
$date = get-date '2/15/2014'
$latest = Get-ChildItem -Path "C:\Program Files (x86)\FileZilla Server\Logs" -Filter *.log | ? {$_.LastWriteTime.Date -eq $date}

Open in new window

if you want to have a file of exactly one day. For the "latest", the last line of Subsun is spot on.
0
 

Author Comment

by:c7c4c7
ID: 39868806
I currently have the code suggested by subsun but the report runs the following  morning so  I need the previous days and the directory has numerous log files.  That's what got me started on the date parameter

Thx
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 39868822
The previous day is (get-date).AddDays(-1).Date
0
 

Author Closing Comment

by:c7c4c7
ID: 39871669
This is what I ended up with
$latest = Get-ChildItem -Path $FilezillaDir -Filter *.log | ? { $_.LastWriteTime.toString("MM/dd/yyyy") -eq (Get-Date).AddDays(-1).toString("MM/dd/yyyy")}

I also found out that if you use mm instead of MM you get a really weird number for the month

Thanks for the help
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39872347
If you use mm then it is considered as minutes.. You can refer the article which I posted in my last comment.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39872719
Again - you should not compain the string representations, but the .Date values. IN particular if you are checking for later or earlier or between a date (range).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article will help you understand what HashTables are and how to use them in PowerShell.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now