I have this piece of code that basically checks if one or more files exist. It works when I search for one file, but it doesn't when I try to search for more. I tried to define开发者_如何学JAVA an array that takes a string of the path of the file to look for, and then check its existence. But I noticed because there is a space in the path, it won't work. I tried all the combinations I could with simple quotes, double quotes, escaping quotes, nothing...
I don't want to create a specific function for that, just to make things work, keep it simple. Could you please help ?
EDIT : to make the script work, add this : $LogSb = new-object System.Text.StringBuilder and you might want to add some output because by default everything goes silently into a "log" string builder.
##Checkink files containing my functions
#
$Fonctions = "C:\Program Files\scheduler\jobs\SuivideProd\OLAP\PlusProchesVoisins.ps1", "C:\Program Files\scheduler\jobs\SuivideProd\OLAP\OrdreProcess.ps1"
#Tried something like this before, same error :
#$Fonctions = @()
#$Fonctions += "C:\'Program Files'\scheduler\jobs\SuivideProd\OLAP\PlusProchesVoisins.ps1"
#$Fonctions += "C:\'Program Files'\scheduler\jobs\SuivideProd\OLAP\OrdreProcess.ps1"
foreach ($fonc in $Fonctions) {
if ((Test-Path $fonc) -eq $True) {
[void]$LogSb.append("`n").appendline($("Le fichier contenant la fonction $(split-path $fonc -Leaf) a été trouvé."))
#dot source the file if existing
. $Fonctions
} else {
[void]$LogSb.append("`n").appendline($("Le fichier contenant la fonction $fonc n'a pas été trouvé."))
throw ("Le fichier contenant la fonction $(split-path $fonc -Leaf) n'a pas été trouvé")
}
}
Your first way of defining the array is fine. Your second method (using @()
and +=
) does not need '
s around Program Files
(the extra apostrophes actually make the paths incorrect).
Upon reading through it, I think your problem is that you are dot-sourcing the array rather than an individual file. You are doing . $Fonctions
when you probably mean to do . $fonc
, which is the foreach
variable holding a single file path.
精彩评论