使用 -Path 时包含 [ 和 ( 的路径出现问题



我尝试用PowerShell编写一个脚本来移动3个文件夹。由于我的路径中的不同[](),我遇到了一些问题。我无法改变路径,想找到解决方案。

目标是检查 3 个文件夹的位置并更改它们的位置。

这是我的代码:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
if(((Test-Path "D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[TMX] IF2 Red Shed") -eq $True ) -and ((Test-Path "D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[CP] Immersive Farm 2") -eq $True ) -and ((Test-Path "D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[FTM] Immersive Farm 2 Forage+Ore") -eq $True ))
{
Move-Item -Path D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[TMX] IF2 Red Shed -Destination D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement
Move-Item -Path D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[CP] Immersive Farm 2 -Destination D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement
Move-Item -Path D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[FTM] Immersive Farm 2 Forage+Ore -Destination D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement
}
Elseif(((Test-Path "D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement[TMX] IF2 Red Shed") -eq $True ) -and ((Test-Path "D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement[CP] Immersive Farm 2") -eq $True ) -and ((Test-Path "D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement[FTM] Immersive Farm 2 Forage+Ore") -eq $True ))
{
Move-Item -Path D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement[TMX] IF2 Red Shed -Destination D:Program Files (x86)SteamsteamappscommonStardew ValleyMods
Move-Item -Path D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement[CP] Immersive Farm 2 -Destination D:Program Files (x86)SteamsteamappscommonStardew ValleyMods
Move-Item -Path D:Program Files (x86)SteamsteamappscommonStardew ValleyMap immersivedossier de changement[FTM] Immersive Farm 2 Forage+Ore -Destination D:Program Files (x86)SteamsteamappscommonStardew ValleyMods
}
Else
{
$oReturn=[System.Windows.Forms.Messagebox]::Show("No folders")
}

>改用-LiteralPath

-Path与项/子项提供程序 cmdlet 一起使用时,PowerShell 会将参数视为潜在的通配符模式!

由于[A-Z]是通配符模式构造,因此文件系统 globber 实际上不会解析名称中包含文字[]的文件。

使用-LiteralPath将阻止任何扩展通配符的尝试:

Test-Path -LiteralPath "D:Program Files (x86)SteamsteamappscommonStardew ValleyMods[TMX] IF2 Red Shed"

请记住也要引用带有空格的路径!

Move-Item -LiteralPath 'C:path with [wildcards]inthename.ext' -Destination 'C:Destinationpath'

我发现通过制表符补全,您可以使用反引号转义方括号:

test-path '.a`[hi`]'
move-item '.a`[hi`]' foo
set-location '.a`[hi`]'

相关内容

  • 没有找到相关文章

最新更新