Link to home
Start Free TrialLog in
Avatar of c7c4c7
c7c4c7Flag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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*"})
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.
Avatar of c7c4c7

ASKER

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
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

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.
Avatar of c7c4c7

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of c7c4c7

ASKER

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
If you use mm then it is considered as minutes.. You can refer the article which I posted in my last comment.
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).