使用 -Path 时包含 [ 和 ( 的路径的问题 [英] Issue with path containing [ and ( when using -Path
问题描述
我尝试用 PowerShell 编写一个脚本来移动 3 个文件夹.由于路径中的 []
和 ()
不同,我遇到了一些问题.我无法改变路径,想找到解决方案.
I try to write a script with PowerShell to move 3 folders. I've got some issue due to the different []
and ()
in my path. I can't change the path and want to find a solution.
目标是检查 3 个文件夹的位置并更改它们的位置.
The goal is to check where the 3 folders are and to change their position.
这是我的代码:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
if(((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[TMX] IF2 Red Shed") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[CP] Immersive Farm 2") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[FTM] Immersive Farm 2 Forage+Ore") -eq $True ))
{
Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[TMX] IF2 Red Shed -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement
Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[CP] Immersive Farm 2 -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement
Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[FTM] Immersive Farm 2 Forage+Ore -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement
}
Elseif(((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[TMX] IF2 Red Shed") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[CP] Immersive Farm 2") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[FTM] Immersive Farm 2 Forage+Ore") -eq $True ))
{
Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[TMX] IF2 Red Shed -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[CP] Immersive Farm 2 -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[FTM] Immersive Farm 2 Forage+Ore -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
}
Else
{
$oReturn=[System.Windows.Forms.Messagebox]::Show("No folders")
}
推荐答案
改用 -LiteralPath
当您将 -Path
与 Item/ChildItem 提供程序 cmdlet 一起使用时,PowerShell 会将参数视为潜在的通配符模式!
Use -LiteralPath
instead
When you use -Path
with the Item/ChildItem provider cmdlets, PowerShell treats the argument as a potential wildcard pattern!
由于 [AZ]
是通配符模式结构,文件系统通配符实际上不会使用文字 [
或 ]<解析文件/code> 在名称中.
Since [A-Z]
is a wildcard pattern construct, the file system globber won't actually resolve file(s) with a literal [
or ]
in the name.
使用 -LiteralPath
将抑制任何扩展通配符的尝试:
Using -LiteralPath
will supress any attempt to expand wildcards:
Test-Path -LiteralPath "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[TMX] IF2 Red Shed"
记得用空格引用路径!
Move-Item -LiteralPath 'C:\path with [wildcards]\in\the\name.ext' -Destination 'C:\Destination\path\'
这篇关于使用 -Path 时包含 [ 和 ( 的路径的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!