#1 04 Jan 2012 22:24

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Get file size using foreach loop

Hi All,

I have been running around pulling my hair out on this... the easy part is to get the file size of a single file, but what I need to do is:

validate the file size for only a specific extension (.log)
send an email out if the size is greater than the max indicated
then send an email out for those under the indicated size limit or missing altogether

please forgive the formatting and the lack of {}, but i just wanted to provide an idea as to what i am trying to do.
I would like to use something like this

foreach ($filename in $thisdirectory)
{

find out what the length of the .log files are

If ($filename length > X)
{
send this email message, but i need to get the file length first
}
Else
{
if ($filename does not exist)
{
send this email
}
else
{
send this email if there are files present
}

}

Thanks, in advance

Offline

#2 05 Jan 2012 23:25

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Re: Get file size using foreach loop

This is the Key to it all

$stringgoeshere = "{0:N2}" -f ((Get-item $filename).length/1kb) + " KB" | Out-string

Offline

#3 17 Mar 2012 15:08

urbanbaby
Member
Registered: 17 Mar 2012
Posts: 1
Website

Re: Get file size using foreach loop

I need this solution too.

Offline

#4 19 Mar 2012 16:23

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Re: Get file size using foreach loop

Ok, To add to this dilemma,  I have been successful having this work, but my script now needs to send a separate email based on the filesize.  Out-string isn't helping. i also tried to convert this to an integer to use -gt or -lt to no avail. i've also tried to use the error level(true or false), but I cannot get the integer to display using write-host and the script will not respect the string as an integer. to send emails on a conditional basis.  any ideas?

I am currently using the above answer ($stringgoeshere= "{0:N2}" -f ((Get-item $log).length/1mb) + " MB" | Out-string) and If($stringgoeshere -gt 10){send-mailmessage... etc}

I need assistance.

Thanks,
cgman

Offline

#5 19 Mar 2012 17:48

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Re: Get file size using foreach loop

This seems to have resolved this issue

"{0:N2}" -f ((Get-item $log).length/1mb) -as [Int]

Offline

#6 11 Sep 2014 11:27

camila
New Member
Registered: 11 Sep 2014
Posts: 1

Re: Get file size using foreach loop

Thanks for your reply. That worked for me. I'm having trouble with the basics of powershell scripting, but this worked for a Windows 8.1 project I am working on, as autoit and psexec was not able to runas another user.

Offline

Board footer

Powered by FluxBB