Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PowerShell
Hello,
I need to list the files of a certain folder with the following conditions.
*If Monday, check for 5 files that got modified today. If there are not 5 files or if the counting is less than five, send an email about files missing
 
On Tuesday til Friday, check for 4 files that got modified today. If there are not 4 files or if the counting is less than four, send an email about files missing
$now = Get-Date
$strDate = "{0:yyyyMMdd}" -f ($now)
$folder = "\\up3\ftp\Backup"
If ($now.DayOfWeek -eq "Monday") {
   #Look for 5 files modified today
   #If 5 files modified today is true 
   # { All files found }
   #Else if there are no 5 files or are less than 5, send an email Files missing
}
 #Look for 4 files modified today
 #If 4 files modified today is true 
 # { All files found }
 #Else if there are no 4 files or are less than 4, send an email Files missing
Thanks for your help,
Posted 2-Apr-13 8:11am
namerg389
Comments
Nelek at 2-Apr-13 13:22pm
   
And where are you having problems? You just explained your requirements, but did no question at all.
Sergey Alexandrovich Kryukov at 2-Apr-13 14:20pm
   
I was able to see at least one problem, explained how to fix it. Please see my answer...
—SA
namerg at 2-Apr-13 13:50pm
   
well do not know how to do it...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The first problem I can see is that your calculated $now is not really the date. Yes, this is a bit confusing…
 
"Today" means that you need to check up if some time is greater of equal the any time of the same date. This is not what happens in your code. Let me run this code right now:
$date = Get-Date
# printing it:
$date
# and, to compare:
$date.Date
Output:
Tuesday, April 02, 2013 2:06:38 PM
Tuesday, April 02, 2013 12:00:00 AM
Can you see what's going on? In first case, you get not the very beginning of the day, but just the current time. The function Get-Date happens to wrap the static property System.DateTime.Now (which you can also use); and its name is very confusing.
 
The correct comparison of the date with the file time can be like this:
$date = Get-Date # now
# same as:
# [System.DateTime]::Now
$date = $date.Date
 
# ...
 
$file_time = # file creation time...
 
# ...
 
$select = $file_time -ge $date
 
Now, you can use it in the query like this:
$date = Get-Date # now
$date = $date.Date
$file_array = Get-ChildItem | where { $_.CreationTime.Date -ge $date } # any time today and later
 
In your final code, you should also apply your logic taking into account the day of week, which you already have.
 
Pay attention for the sample of the syntax [System.DateTime]::Now. This is how you can use almost all power of .NET FCL, and more: you can additionally load extra GAC assemblies, that is, everything else which was installed in GAC on top of .NET FCL, and use it all.
 
I tested all my code samples…
 
—SA
  Permalink  
v7
Comments
namerg at 2-Apr-13 14:50pm
   
Let me rephrase my goal:
The are five files: YYYYMMDD_whatever_clm.txt, YYYYMMDD_whatever_proc.txt,YYYYMMDD_whatever_pym.txt,YYYYMMDD_whatever_pymt_li.txt,YYYYMMDD_whatever_cc.txt
20130327_14401_clm.txt
20130327_14401_proc.txt
20130327_14401_pymt.txt
20130327_14401_pymt_li.txt
20130325_14401_cc.txt
To Do: Need to check the existence of *_clm.txt,*_proc.txt,*_pymt.txt and *_pymt_li.txt from Monday to Friday. But, on Monday need to check the existence of only the *_cc.txt
Sergey Alexandrovich Kryukov at 2-Apr-13 16:42pm
   
You have everything to complete the task. I explained how to resolve the problem you have, and you already implemented the day-of-week logic. Can you put it all together? I think you can...
—SA
namerg at 2-Apr-13 14:57pm
   
14401 is variable and they are time stamped daily
Sergey Alexandrovich Kryukov at 2-Apr-13 16:42pm
   
So?
—SA
namerg at 2-Apr-13 17:11pm
   
I really do not know very well powershell but i figured it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100