Get-ChildItem doesnt find file when using data parameter

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
c7c4c7Asked:
Who is Participating?
 
SubsunConnect With a Mentor Commented:
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
 
becraigCommented:
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
 
footechCommented:
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
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
c7c4c7Author Commented:
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
 
SubsunCommented:
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
 
QlemoBatchelor and DeveloperCommented:
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
 
c7c4c7Author Commented:
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
 
QlemoConnect With a Mentor Batchelor and DeveloperCommented:
The previous day is (get-date).AddDays(-1).Date
0
 
c7c4c7Author Commented:
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
 
SubsunCommented:
If you use mm then it is considered as minutes.. You can refer the article which I posted in my last comment.
0
 
QlemoBatchelor and DeveloperCommented:
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
All Courses

From novice to tech pro — start learning today.