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.

4
  • 5
    when calling a function, DO NOT use () around your function parameters. [grin] just put them there something like SearchFile $dir $file. Commented Feb 22, 2021 at 6:20
  • that works @Lee_Dailey thanks Commented Feb 22, 2021 at 7:01
  • 1
    you are most welcome! glad to have helped a bit ... [grin] Commented Feb 22, 2021 at 7:39
  • yes, believe it or not, I actually have a powershell book on my kindle. it covers everything I don't need to know..lol. I just randomly stumbled on how to even declare functions as above. Commented Feb 22, 2021 at 7:43

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.