Get file size using foreach loop

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Get file size using foreach loop

Post by MigrationUser »

04 Jan 2012 22:24
cgman


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

Code: Select all

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

----------------------------

#05 Jan 2012 23:25
cgman


This is the Key to it all

Code: Select all

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

#17 Mar 2012 15:08
urbanbaby


I need this solution too.

----------------------------

#19 Mar 2012 16:23
cgman


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

----------------------------

#19 Mar 2012 17:48
cgman


This seems to have resolved this issue

Code: Select all

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

#11 Sep 2014 11:27
camila


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.
Post Reply