在Powershell路径中

在Powershell路径中

本文介绍了与[和(在Powershell路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次问一个问题,英语不是我的母语,所以请原谅我的错误.

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

我尝试使用Powershell编写脚本来移动3个文件夹,但是由于路径中的"[]"和()"不同,所以我遇到了一些问题.我无法更改路径,所以希望您能帮助您找到解决方案.

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.

目标是检查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!

由于[A-Z]是通配符模式构造,所以文件系统globber实际上不会解析名称中带有文字[]的文件.

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"

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

Remember to quote paths with spaces as well!

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

这篇关于与[和(在Powershell路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:18