0

How do I pass $dir and $file, then concatenate them to a single path?

Output:

posh> 
posh> pwsh ./worker.ps1
dir is /home/nicholas/powershell/regex a.log
file is 
full is /home/nicholas/powershell/regex a.log/
posh> 

library:

function SearchFile($dir,$file)
{
"dir is $dir"
"file is $file"

$full = [string]::Concat("full is ",$dir,"/",$file)
$full
#$pattern='(?=.*?foo)(?=.*?bar)'
#$string = Get-Content $full
#$result = $string | Select-String $pattern
#$result
}

worker:

. /home/nicholas/powershell/functions/library.ps1


$dir = "/home/nicholas/powershell/regex"
$file = "a.log"

SearchFile($dir,$file)

I seem to be passing an array of sorts for $dir and nothing is getting assigned for $file in SearchFile as expected.

1 Answers1

2

Can be done in a simpler way :

function SearchFile ($var_1, $var_2) {
    $full_path = "$var_1/$var_2"
    $full_path # Output --> C:/temp/test.txt
}

$my_dir  = "C:/temp"
$my_file = "test.txt"

SearchFile $my_dir $my_file # Do not use coma when calling a Function