Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

2 foreach statement, not working

Dear expert

Please read the code below.
Problem is within the beginning, I want to add $ServerPathC1 and $ServerPathC2 with $list1 and $list2 and add the new list to variable $pathlist and to a foreach on $pathlist.
For exemple:
In text file: c:\folderlist65_27_70.txt I got like
test1\temp
test2\temp
test3\temp
In text file: c:\folderlist04_56.txt I got like
testagain1\temp
testagain2\temp
testagain3\temp
I want to do a $ServerPathC1 + content in list1 so they become: d:\folder1\test1\temp and then add them to $pathlist then from $pathlist do a foreach to it.
Place I stuck with is the foreach, the foreach statement below runs with double result, and its wrong. Anyway to solve this?

Thanks.

$ServerPathC1 = "d:\folder1\" 
$ServerPathC2 = "d:\folder2\"
$list1 = Get-Content 'c:\folderlist65_27_70.txt'
$list2 = Get-Content 'c:\folderlist04_56.txt'
$7daysago = (Get-Date).AddDays(-7)
$Pathlist = @()
$warning = 0

$pathlist = foreach ($1 in $list1) {
foreach ($2 in $list2){
$ServerPathC1 + $1 
$ServerPathC2 + $2 
}
}


$result = ForEach ($path in $Pathlist) {
	$count = Get-ChildItem $path -Exclude 'keep*', 'testfilnamn*', 'tilltestfil*', 'Crend*', 'KFMtest*' |
		Where-Object {!($_.PSIsContainer) -and ($_.LastWriteTime -le $7daysago) -or ($_.Name -like '*failed*')} | Measure-Object |
		Select-Object -ExpandProperty Count
	If ($count -gt 0) {
		
        $warning++
		$out = '' | Select-Object -Property FilesCount, CollectFolder
		$out.FilesCount = $count
		$out.CollectFolder = $path
		$out
	}
}
$result

Open in new window

Avatar of oBdA
oBdA

It's not quite clear to me what it is exactly that you want to combine how.
Assuming

$ServerPathC1 = "d:\folder1\"
$ServerPathC2 = "d:\folder2\"
and c:\folderlist65_27_70.txt as:
test1\temp
test2\temp
and c:\folderlist04_56.txt as:
testagain1\temp
testagain2\temp

Which paths do you finally want to test in the loop altogether?
Avatar of WeTi

ASKER

$ServerPathC1 = "d:\folder1\"
in txt file:
test1\temp
test2\temp

I got to add $ServerPathC1 first with test1\temp so it become: d:\folder1\test1\temp then add this result to a new var $pathlist, then do a foreach on that.
Just remove the second path building. E.g.

$ServerPathC1 = "d:\folder1\"
$PathList = @()
$List1 = @('test1\temp',  'test2\temp',  'test3\temp')
ForEach ($1 In $List1) {
    $PathList += $ServerPathC1 + $1
}

$PathList

Open in new window

Avatar of WeTi

ASKER

Hi ste5an

$ServerPathC1 and $ServerPathC2 is diffirent string (path) It needs to be there
Let me rephrase the question: do you want to combine either
$ServerPathC1 + $list1
$ServerPathC2 + $list2
Or
$ServerPathC1 + $list1
$ServerPathC1 + $list2
$ServerPathC2 + $list1
$ServerPathC2 + $list2
Avatar of WeTi

ASKER

$ServerPathC1 + $list1
$ServerPathC2 + $list2
Avatar of WeTi

ASKER

Well I know there are workaround that you can run two times get-childitem with each diffirent variable,$result1 and $result2 but there must be another way...
Then try this:
$ServerPathC1 = "d:\folder1\" 
$ServerPathC2 = "d:\folder2\"
$list1 = 'c:\folderlist65_27_70.txt'
$list2 = 'c:\folderlist04_56.txt'
$7daysago = (Get-Date).AddDays(-7)
$pathlist = @()
$warning = 0

Get-Content -Path $list1 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC1 -ChildPath $_}
Get-Content -Path $list2 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC2 -ChildPath $_}

$result = ForEach ($path in $pathlist) {
	$count = Get-ChildItem $path -Exclude 'keep*', 'testfilnamn*', 'tilltestfil*', 'Crend*', 'KFMtest*' |
		Where-Object {(-not $_.PSIsContainer) -and (($_.LastWriteTime -le $7daysago) -or ($_.Name -like '*failed*'))} | Measure-Object |
		Select-Object -ExpandProperty Count
	If ($count -gt 0) {
		
        $warning++
		$out = '' | Select-Object -Property FilesCount, CollectFolder
		$out.FilesCount = $count
		$out.CollectFolder = $path
		$out
	}
}
$result

Open in new window

Avatar of WeTi

ASKER

Well the error at

Get-Content -Path $list1 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC1 -ChildPath $_}
Get-Content -Path $list2 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC2 -ChildPath $_}

It trying to find the path in my user profile path not d:\
Which error exactly?
Can't reproduce, works here as expected:
PS C:\PS> $ServerPathC1 = "d:\folder1\"
PS C:\PS> $ServerPathC2 = "d:\folder2\"
PS C:\PS> $list1 = 'D:\folderlist65_27_70.txt'
PS C:\PS> $list2 = 'D:\folderlist04_56.txt'
PS C:\PS> $7daysago = (Get-Date).AddDays(-7)
PS C:\PS> $pathlist = @()
PS C:\PS> $warning = 0
PS C:\PS>
PS C:\PS> Get-Content -Path $list1 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC1 -ChildPath $_}
PS C:\PS> Get-Content -Path $list2 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC2 -ChildPath $_}
PS C:\PS> $pathlist
d:\folder1\test1\temp
d:\folder1\test2\temp
d:\folder1\test3\temp
d:\folder2\testagain1\temp
d:\folder2\testagain2\temp
d:\folder2\testagain3\temp
PS C:\PS>

Open in new window

Avatar of WeTi

ASKER

Error:

Get-Content : Cannot find path 'C:\Users\weti\testagain1\temp' because it does not exist.
At line:10 char:1
+ Get-Content -Path $list2 | ForEach-Object {$pathlist += Join-Path -Pa ...
Avatar of WeTi

ASKER

the variable $ServerPathC1 or $ServerPathC2 seems not working
hmm, your descriptions is still hard to understand... e.g.

#Requires -Version 5.0

Set-StrictMode -Version 2

Function ScriptMain() {
    $pathList = ReadPathList 'C:\Temp\l1.txt' 'd:\folder1\'
    $pathList += ReadPathList 'C:\Temp\l2.txt' 'd:\folder2\'
    $pathList
}

Function ReadPathList() {
    Param([String] $partialPathsFileName, [String] $serverPathPrefix)

    $partialPaths = Get-Content -Path $partialPathsFileName
    ForEach ($partialPath In $partialPaths) {
        $serverPathPrefix + $partialPath
    }
}

ScriptMain

Open in new window

The error is from "Get-Content -Path $list2 ...", so the path you defined in $list2 is incorrect.
Avatar of WeTi

ASKER

$list1 and $list2 there are no path, its just half of the path like: test1\test
the full path is $ServerpathC1 + $list1 that would be the correct path
Note that I redefined $list1 and $list2 as compared to your original script: they must now contain the path to the text files in question, they do not contain the content anymore.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

The error solved, $list1, $list2, content textstring, $list1 contains: 'test1\test1', 'test1\test2', etc, $list2 contains 'test2\test1', 'test2\test2'
This is only part of server path, that is why I wanted to parse it with $ServerPathC1 and with $ServerPathC2.

Now the error come from here:
Get-Content -Path $list1 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC1 -ChildPath $_}
Get-Content -Path $list2 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC2 -ChildPath $_}

Open in new window

Get-Content -Path $list1 or $list2 this means script goes in and check Path: 'test1\test1' and that is where error shows, the 'test1\test1' is only a string not a server path or anything.

The working code looks like this:
$pathList = @(
    ($list1 | ForEach-Object -Process { $ServerPathC1 + $_ }),
    ($list2 | ForEach-Object -Process { $ServerPathC2 + $_ })
)

Open in new window


It took a while those for me to find this, now I encounted another problem but I will create a new question for the next, this one is now solved, thanks all the effort.
Avatar of WeTi

ASKER

Thanks

The error solved, $list1, $list2, content textstring, $list1 contains: 'test1\test1', 'test1\test2', etc, $list2 contains 'test2\test1', 'test2\test2'
That's what I said in https://www.experts-exchange.com/questions/29169625/2-foreach-statement-not-working.html?anchorAnswerId=43011307#a43011307
In your original script, you had
$list1 = Get-Content 'c:\folderlist65_27_70.txt'
$list2 = Get-Content 'c:\folderlist04_56.txt'

Open in new window

I changed that to
$list1 = 'c:\folderlist65_27_70.txt'
$list2 = 'c:\folderlist04_56.txt'
# ...
Get-Content -Path $list1 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC1 -ChildPath $_}
Get-Content -Path $list2 | ForEach-Object {$pathlist += Join-Path -Path $ServerPathC2 -ChildPath $_}

Open in new window

Note that the "Get-Content" was removed from "$list1 = Get-Content ..." and "$list2 = Get-Content ..."
I suspect that you just inserted the lines starting with "Get-Content -Path $list1 ..." into your original script, , but kept the "$list1 = Get-Content ...".
Avatar of WeTi

ASKER

Oh yeah youa re right... I didn't noticed that change :) my bad! Thanks alot!
Avatar of WeTi

ASKER

After changed the thing back, it starts working wihtout error :) or stuck. Thanks alot