views:

79

answers:

4

I'm trying to build a file path in powershell and the string concatenation seems to be a little funky.

I have a list of folders:

c:\code\MyProj1
c:\code\MyProj2

I want to get the path to a dll here:

c:\code\MyProj1\bin\debug\MyProj1.dll
c:\code\MyProj2\bin\debug\MyProj2.dll

Here's what I'm trying to do:

$buildconfig = "Debug" 

Get-ChildItem c:\code | % {
    Write-Host $_.FullName + "\" + $buildconfig + "\" + $_ + ".dll"
}

Anyone who knows powershell can clearly see this doesn't work. I however am not that person. Can someone point me in the right direction?

Thanks!

+2  A: 

Try this

Get-ChildItem  | % { Write-Host "$($_.FullName)\$buildConfig\$($_.Name).dll" }

In your code,

  1. $build-Config is not a valid variable name.
  2. $.FullName should be $_.FullName
  3. $ should be $_.Name
ravikanth
`$_.Name` includes the extension. I suspect the intent was to replace the current extension with `.dll` instead of tacking on an additional `.dll`. :-)
Keith Hill
He says he has a list of folders. So, $_.Name should be OK. Otherwise, $_.BaseName is OK.
ravikanth
+1  A: 

You could use the PowerShell equivilent of String.Format - it's usually the easiest way to build up a string. Place {0} {1} etc. where you want the variables in the string, put a -f immediately after the string and then the list of variables seperated by commas.

Get-ChildItem c:\code|%{'{0}\{1}\{2}.dll' -f $_.fullname,$buildconfig,$_.name}

(I've also taken the dash out of $buildconfig variable name as seen that cause issues before too)

Alisdair Craik
A: 

This will get all dll files and filter ones that match a regex of your directory structure.

Get-ChildItem C:\code -Recurse -filter "*.dll" | where { $_.directory -match 'C:\\code\\myproj.\\bin\\debug'}

If you just want the path, not the object you can add | select fullname to the end like this:

Get-ChildItem C:\code -Recurse -filter "*.dll" | where { $_.directory -match 'C:\\code\\myproj.\\bin\\debug'} | select fullname

Jacob Ballard
A: 

Try the Join-Path cmdlet:

Get-ChildItem c:\code\*\bin\* -Filter *.dll | Foreach-Object {
    Join-Path -Path  $_.DirectoryName -ChildPath "$buildconfig\$($_.Name)" 
}
Shay Levy