Solved

Get-ChildItem doesnt find file when using data parameter

Posted on 2014-02-18
11
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 29

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 40

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

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 70

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 70

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

696 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