Reputation: 43
I am gathering data about emails and exporting to CSV. In this part of the script, I need to generate a text file with what will become 2 columns in the CSV file. The 1st column (email subject line text) already exists in the text file and I need to add the following to every line of the file: a hash tag PLUS part of the file name PLUS a hashtag. Hashtags are my delimiter for the CSV file.
In my first ForEach loop, I iterate through the folder to generate the list of file names, then split to the part of the name that I need. This works. Then in the 2nd ForEach loop, I try to iterate through the files in the folder and add the appropriate file name section to every line. Instead of appending just the one relevant file name section, it appends ALL the filenames to every line.
Here is sample of current data in file "Inbox_Subject_Reports & Timekeeping.txt":
Re: your weekly business reports Re: submission of timesheet
Desired output:
Re: your weekly business reports#Reports & Timekeeping.txt#
Re: submission of timesheet#Reports & Timekeeping.txt#
But there are additional files in the same folder, so I am getting all the file names and also the data is copying 4 times:
Re: your weekly business reports#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: submission of timesheet#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: your weekly business reports#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: submission of timesheet#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: your weekly business reports#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: submission of timesheet#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: your weekly business reports#Reports & Timekeeping.txt#Fourth Quarter Returns#
Re: submission of timesheet#Reports & Timekeeping.txt#Fourth Quarter Returns#
Is is the nesting of my ForEach loops? Do I need some kind of "break" function? I love PowerShell and have been able to do so much with it, but I have hit a limit to my knowledge here. I'd rather learn how to do this correctly than create a cumbersome work-around.
Here is the section of script:
$dir = "$Filelocation\Inbox_Subfolders\"
$filelist = @(Get-ChildItem $dir)
ForEach ($file in $filelist){
$folder = $file.Name.Split("_")[2]
}
{
$TextToAdd = "#" + $folder + "#"
$output = @()
$fileContent = Get-Content "$Filelocation\Inbox_Subfolders\Inbox_Subject_*.txt"
}
ForEach ($line in $filecontent){
$output += $line + $TextToAdd
$output | Set-Content "$Filelocation\Inbox_Subfolders\Inbox_Subject_*.txt"
}
I've spent days trying to figure out nested ForEach loops and where I am going wrong. I am open to different ways of achieving same results. Any help is greatly appreciated.
Upvotes: 2
Views: 1139
Reputation: 17035
First of all: Use proper indentation. It will make it easier for you to see where nested code blocks start and end.
Does this do what you want?
$dir = "$Filelocation\Inbox_Subfolders"
Get-ChildItem $dir -Filter *.txt | foreach {
$folder = $_.Name.Split("_")[2]
$TextToAdd = "#" + $folder + "#"
(Get-Content $_.FullName) | foreach {
$_ + $TextToAdd
} | Set-Content $_.FullName
}
Upvotes: 2