Powershell 如何将文件名添加到变量中
我有这个Powershell 如何将文件名添加到变量中,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我有这个 Get-ChildItem -Path E:\workspace\TestPowershell\ -Recurse -Filter "app.config" | ForEach-Object { $AppConfigPath = $_.Directory } 现在我想要两个变量 $AppConfigPath +"App.config" $AppConfigPath +"App.11111.config 如何将其转换为变量 我尝试过不同的方法,但都没能成功 rename-it
Get-ChildItem -Path E:\workspace\TestPowershell\ -Recurse -Filter "app.config" | ForEach-Object {
$AppConfigPath = $_.Directory
}
现在我想要两个变量
$AppConfigPath +"App.config"
$AppConfigPath +"App.11111.config
如何将其转换为变量
我尝试过不同的方法,但都没能成功
rename-item –path $_.Directory –Newname ( $_.basename + "App.config)
我不确定我是否正确理解了这个问题,但是如果你想要两个额外的变量,那就把你需要的经验分配给两个新的变量吧 父目录的路径可以在
FullName
属性中找到,您可以使用Join path
将目录路径与文件名组合,如下所示:
Get-ChildItem -Path E:\workspace\TestPowershell\ -Recurse -Filter "app.config" | ForEach-Object {
$AppConfigPath = $_.Directory
$FirstFilePath = Join-Path $AppConfigPath.FullName 'App.config'
$SecondFilePath = Join-Path $AppConfigPath.FullName 'App.11111.config'
}
你能举个例子说明你希望输出是什么样子的吗?基于您的问题,我不太确定您想要实现什么。
重命名项–路径$\u.Directory–Newname($\u.basename+“App.config”)
-->重命名项–路径$AppConfigPath.FullName–Newname($\u.basename+”App.config)
。还有,你说你想要两个变量。您想要两个变量还是两个路径,因为$AppConfigPath的类型为System.IO.FileSystemInfo
请查看连接路径
cmdlet我想要两个类似于$AppConfigPath+App.config和$AppConfigPath+App.11111.config的变量/路径。这将是我将使用的两个不同的路径。那么为什么要使用重命名项
?正如@gvee所建议的,您正在寻找cmdlet!