Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get-ChildItem doesnt find file when using data parameter

Posted on 2014-02-18
11
Medium Priority
?
536 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 500 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 41

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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 

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 71

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 71

Assisted Solution

by:Qlemo
Qlemo earned 500 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 71

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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