Issue with [ and ( in Path with Powershell

我怕爱的太早我们不能终老 提交于 2020-06-06 08:22:31

问题


It's my first time asking a question and english isn't my native language so please excuse my mistakes.

I try to write a script with Powershell to Move 3 folders, but i've got some issue due to the different "[]" and "()" in my Path. I can't change the Path so I hope you could Help find a solution.

The goal is to check where the 3 folders are and to change their position.

Here my code :

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

回答1:


Use -LiteralPath instead

When you use -Path with the Item/ChildItem provider cmdlets, PowerShell treats the argument as a potential wildcard pattern!

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.

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"

Remember to quote paths with spaces as well!

Move-Item -LiteralPath 'C:\path with [wildcards]\in\the\name.ext' -Destination 'C:\Destination\path\'



回答2:


I found through tab completion that you can escape the square brackets with backquotes:

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


来源:https://stackoverflow.com/questions/61752129/issue-with-and-in-path-with-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!