2

My source files all reside in one folder whose path is contained in a variable named $template. I need to specify the exact filename as each file goes to a different destination.

My goal is to merely concatenate the filename to the variable.

Example:

$template = "D:\source\templatefiles\"

Filename1 is: "graphic-183.jpg"

I have tried:

Join-Path $template graphic-183.jpg

Issuing this at the cli appears to do what I want.

But now, how do I reference this concatenated file path short of creating a new variable for each file? It isn't as simple as for-nexting my way through a list as depending on the filename that determines where the file goes.

I am toying with case else, elseIf, but surely it isn't this hard.

The bottom line is, I just want to prefix the folder path to each filename and hard code the destination as it will always be the same each time the script is run.


edit

I just edited this as I forgot to mention how I am trying to use this.

In my script I intend to have lines like:

Copy-Item -Path $template filename.ext -Destination $destfolder

It's the highlighted part above that I am trying to join $template to the filename.

Thanks for any advice.

-= Bruce D. Meyer

2
  • 1
    Can't you just call $myFiles = Get-ChildItem $template? Then all of the files in $template will be stored in the array $myFiles. Or am I missing something? Commented Nov 13, 2012 at 22:11
  • Nope. Your not missing a thing. It's all me. Not a tremendous amount of contiguous experience. I seem to only have the pleasure of writing scripts only when I need them, which isn't real often. I tried to google this, but just couldn't find exactly what I needed. Between the three answers provided here, I'm good now. Now, If I can figure out how this voting thing works. The other two are listed as answers, and your reply David, looks like it is a comment or something. Thank you all three. Commented Nov 16, 2012 at 21:36

2 Answers 2

4

maybe this is what you want?

you can call cmdlets in place, using parentheses, like so:

Copy-Item -Path (Join-Path $template filename.ext) -Destination $destfolder

this causes PowerShell to go from "argument mode" to "expression mode" - i.e., it returns the output of the Join-Path cmdlet as an expression.

and yes, David's and Ansgar's suggestions are also helpful - try this to get full paths only:

(get-childitem $template) | select fullname
Sign up to request clarification or add additional context in comments.

1 Comment

These posts are all useful for what I am doing. I appreciate you taking time to instruct me.
1

You could build the path like this:

$template = "D:\source\templatefiles\"
Copy-Item -Path "${template}filename.ext" ...

However, I think David's suggestion might be a better solution for your problem. You could map filenames to destination folders with a hash table and do something like this:

$locations = @{
  "foo" = "C:\some",
  "bar" = "C:\other",
  ...
}

Get-ChildItem $template | % { Copy-Item $_ $location[$_.Name] }

1 Comment

Thank-you for your help. This is still quite useful in my project.

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.