简体   繁体   English

使用 powershell 删除包含“[”和“'”的文件夹

[英]Delete folder which has "[" and "'" using powershell

I want to delete folders using powershell. here is the foldername我想使用 powershell 删除文件夹。这是文件夹名称

C:\File\Coastal [new]
C:\File_new\Coastal.[new]
C:\File\Russia`s.Book
C:\File_new\Russia`s Book

It looks like ' and [] making the problem.它看起来像 ' 和 [] 制造问题。 I tried following command:我尝试了以下命令:

Get-ChildItem "C:\File\" | Where{$_.Name -Match "Coastal[\s\.][new]"} | Remove-Item -recurse -Force -Verbose
Get-ChildItem "C:\File_new\" | Where{$_.Name -Match "Coastal[\s\.][new]"} | Remove-Item -recurse -Force -Verbose

Get-ChildItem "C:\File\" | Where{$_.Name -Match "Russia`s[\s\.]Book"} | Remove-Item -recurse -Force -Verbose
Get-ChildItem "C:\File_new\" | Where{$_.Name -Match "Russia`s[\s\.]Book"} | Remove-Item -recurse -Force -Verbose

In a regex , which is what the -match operator operates on as its RHS, [ is a metacharacter , as evidenced by your use of character set [\s\.] to match either a single whitespace character ( \s ) or a literal \.正则表达式中, -match运算符作为其 RHS 进行操作, [是一个元字符,您使用字符集[\s\.]来匹配单个空白字符( \s )或文字就证明了这一点\. (as an aside: . doesn't need escaping inside [...] ). (顺便说一句: .内部不需要 escaping [...] )。

Therefore, you must use \[ to escape those [ instances you want to be treated as literals , notably in the [new] substring.因此,您必须使用\[来转义那些您希望被视为文字[实例,特别是在[new] substring 中。

Alternatively - though less readably - you could use a character set to represent a literal [ : [[]或者 - 尽管可读性较差 - 您可以使用字符集来表示文字[ : [[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM