0

thank you for looking. I want to loop through each item in a folder and add qualifying file names and lengths (sizes) to an array so I can send an email out. For example, if there are files without the .txt extension, I do not want to include them. What happens is the email is sent out, but lists the same files in several tables. I know if the issue is how I'm storing the current file to the array, but not sure how to fix it. I just want the current file in the foreach to be added once.

Here is my stripped out code:

$myFolder = "C:\Users\myName\Documents\Temporary"
$ReceivedCount = 0

$a = "<style>BODY{font-family: Verdana; font-size: 9pt;}"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:    black;border-collapse: collapse; }"  
$a = $a + "TH{border-width: 2px;padding: 7px;border-style: solid;border-color:    black;background-color:lightblue;padding-right: 2px;}" 
$a = $a + "TD{border-width: 2px;padding: 5px;border-style: solid;border-color:     black;background-color:white; padding-right: 2px;}"
$a = $a + "</style>" 

foreach ($file in $myFolder)
{
    $FileName = $file.name

    Echo "Curent file: $FileName"

    if($FileName -like "*.txt")
    {
        $ReceivedCount += 1
        # This is the section I'm doing wrong:
        $FilesReceived += @(Select-Object name , length | ConvertTo-HTML -head        $a)
    }

}

Echo "Found $ReceivedCount files."

if ($FilesReceivedCount -gt 0)
{
    #send email...
}
5
  • Where do you clear FilesReceived? What object(s) is Select-Object operating on there? Commented Mar 17, 2015 at 15:59
  • I don't clear $FilesRecieved because once this script runs it is done. The Select-Object should only grab the name and length of the current file in the foreach loop. Commented Mar 17, 2015 at 16:02
  • So what multiple tables are you outputting here there isn't any looping (other than over files) here? Sample input and output might be helpful. Commented Mar 17, 2015 at 16:13
  • So for example, I have four files, taco.txt, nachos.txt, salsa.txt, and order.log. When I do the send-mailmessage, i want to email one table that lists each *.txt file's name and size. What the code is doing now is it lists each of the three files in separate tables when I send the email out. Commented Mar 17, 2015 at 16:18
  • 1
    You are running convertto-html once for each file. If you want a single table I believe you need to run it once over the whole collection. Commented Mar 17, 2015 at 16:23

2 Answers 2

3

Try the following:

#$FilesReceived = @()
$FilesReceived += @($file | Select-Object name , length)

$html = $FilesReceived | Convertto-html -head $a

Btw you can filter the file selection beforehand if you use the -include parameter of gci like this:

Get-Childitem C:\path\*.* -Include *.txt

so you only handle .txt files

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, that definitely works, but when I send out the email each file is listed in a separate table. Any idea how to make them go to one table?
@AaronJohnsen I have edited the code. If you use the $html variable you should only have 1 table
Yes!! Just added -head $a after Convertto-html, and it is exactly what I needed, Thank you so much guys. You are awesome people.
1

If you just want to include the files in the table (and not use them later as in an array) you can just use:

$filenames = Get-ChildItem -Path $myFolder "*.txt" | Select-Object Name | ConvertTo-Html -Fragment

This will create a single table containing just the file names (add parameters to the select-object if you want othets). I use the pipe to select-object rather then -name flag on get-childitem as the flag will include the full path while select-object just includes name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.