Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Checking the Existing of files and their size ...

Reply
 
Old 04-28-2008   #1 (permalink)
follower1


 
 

Checking the Existing of files and their size ...

I need to check the status of three files file1.txt, file2.txt, and file3.txt
to see if they exist in the folder c:\files and if they do, to check if they
were created today and if they are greater than zero size.

If they are created today and greater than zero size to send me and email to
myname@xxxxxx

I have downloaded powershell from MS. Cans someone give me any pointer or
guidance on where to start, weather to use WSH, or VB to do it? The file
needs to be running daily on my Windows Server 2003.

I appreciate any directions.

Thanks,
Vic

My System SpecsSystem Spec
Old 04-28-2008   #2 (permalink)
Shay Levi


 
 

Re: Checking the Existing of files and their size ...



Hi Vic

1. You can test for file existence with the test-path cmdlet. test-path can
check for one or multiple files.
2. To check if a file size is greater then 0 bytes use get-childitem to get
the file and then query the length property.
3. To check when a file was created use get-childitem to get the file and
then query the CreationTime property.
4. Check the help for get-childitem,test-path (help cmdletName -full)

OK, here's the script:


# function to send email

function send-email($SmtpServer,$From,$To,$subject,$Body){
$smtp = new-object system.net.mail.smtpClient($SmtpServer)
$mail = new-object System.Net.Mail.MailMessage
$mail.From = $From
$mail.To.Add($To)
$mail.Subject = $subject
$mail.Body = $Body
#$mail.IsBodyHtml = $true
$smtp.Send($mail)
}



$files = "file1.txt","file2.txt","file3.txt"

# holds a boolean array. each item's value represents if the file exits or
not
$exist = test-path $files

# get the date portion of today's datetime
$today = (get-date).ToShortDateString()


#loop over the $files array
for($i=0; $i -lt $files.length; $i++){

# if $exist's current array index is $true then the file exist
if($exist[$i]) {

#files exist, get it
$file = get-childitem $files[$i]

# check if the file was created today
if($file.CreationTime.ToShortDateString() -eq $today){

if($file.length -gt 0){
$files[$i] +" was created today"
$files[$i] +" is greater than zero"
send-email localhost "anyone@xxxxxx" "you@xxxxxx" "file created
today" $files[$i]
}

}
}

Quote:

> I have downloaded powershell from MS. Cans someone give me any
> pointer or guidance on where to start, weather to use WSH, or VB to do
> it? The file needs to be running daily on my Windows Server 2003.

POWERSHELL ONLY


As for guidance, I can suggest a browaser toolbar that has all the links
you need in one place, check it out:

http://scriptolog.blogspot.com/2008/...r-browser.html



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> I need to check the status of three files file1.txt, file2.txt, and
> file3.txt to see if they exist in the folder c:\files and if they do,
> to check if they were created today and if they are greater than zero
> size.
>
> If they are created today and greater than zero size to send me and
> email to myname@xxxxxx
>
> I have downloaded powershell from MS. Cans someone give me any
> pointer or guidance on where to start, weather to use WSH, or VB to do
> it? The file needs to be running daily on my Windows Server 2003.
>
> I appreciate any directions.
>
> Thanks,
> Vic

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Help required with file size checking script VB Script
Running existing bat files PowerShell
Synctoy and existing files Vista General
adding files to pre-existing CD with files on it Vista music pictures video
Checking Mailbox Size Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46