If statement in foreach loop

by Nov 7, 2014

Hello,

I'm trying to write a script to see if a file exists in a folder and if it does write the name of the directory out to the console. Two things are happening, I sometimes get the right output, but it's not consistent and the $directories variable is getting overwritten after the loop is run reflecting only c:folder2 as it's value.  I can't seem to figure out what I'm doing wrong (code sample below).

$filename = "fatalerror.log"
$directories = "c:folder1", "c:folder2"
$combinepath = test-path (join-path $directories -childpath $filename)
foreach ($directories in $directories){if ($combinepath -eq $True){write-host $directories}}