string 如何在 PowerShell 中将字符串与变量连接起来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3978639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:52:18  来源:igfitidea点击:

How do I concatenate strings with variables in PowerShell?

scriptingstringpowershell

提问by Micah

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

我正在尝试在 PowerShell 中构建一个文件路径,字符串连接似乎有点时髦。

I have a list of folders:

我有一个文件夹列表:

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

I want to get the path to a DLL file here:

我想在此处获取 DLL 文件的路径:

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"
}

This doesn't work. How can I fix it?

这不起作用。我该如何解决?

回答by ravikanth

Try this

尝试这个

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

In your code,

在您的代码中,

  1. $build-Configis not a valid variable name.
  2. $.FullNameshould be $_.FullName
  3. $should be $_.Name
  1. $build-Config不是有效的变量名。
  2. $.FullName应该 $_.FullName
  3. $应该 $_.Name

回答by craika

You could use the PowerShell equivalent 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 -fimmediately after the string and then the list of variables separated by commas.

您可以使用等效于 String.Format 的 PowerShell - 它通常是构建字符串的最简单方法。将 {0}、{1} 等放置在字符串中您想要变量的位置,-f紧跟在字符串之后放置一个,然后是用逗号分隔的变量列表。

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

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

(我也从 $buildconfig 变量名中去掉了破折号,因为我之前也看到过这会导致问题。)

回答by Shay Levy

Try the Join-Path cmdlet:

试试 Join-Path cmdlet:

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

回答by Jacob Ballard

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

这将获取所有 dll 文件并过滤与您的目录结构的正则表达式匹配的文件。

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

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 fullnameto the end like this:

如果您只想要路径,而不是可以| select fullname像这样添加到末尾的对象:

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

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