复制文件名中带方括号[]的文件并使用*通配符

Yet*_*ser 12 powershell copy square-bracket

我在Windows 7上使用PowerShell,并编写脚本将一堆文件从一个文件夹结构复制到另一个文件夹结构.有点像编译.PowerShell Copy-Itemcmdlet认为方括号[]是某种通配符,并且由于某种原因我无法逃脱它们.

我不能使用-LiteralPath,因为我想使用星号*通配符,因为文件名的日期是文件名的一部分,日期会改变.日期用作版本号.

这篇文章很有帮助,但是方括号中没有任何数量的刻度(每个括号2x或4x).

我没有收到错误; PowerShell的行为与我输入错误的文件名时的行为相同.

这是我正在研究的具体方法:

#to Fusion Server
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\"[RoomView] Versions explained*.pdf" -Destination $FSG\$containerFolder\$rootFolder\"Fusion Server"\
Run Code Online (Sandbox Code Playgroud)

这就是整个事情:

# Compiles the Fusion packet for distribution

###############################
###########Variables###########
###############################

#folder structure
$FSG = "F:\FSG"
$containerFolder = "Packet.Fusion for IT and AV Professionals"
$rootFolder      = "Fusion for IT and AV pros $(Get-Date -format “MM-dd-yyyy”)"
$subRoot1        = "Fusion Server"
$subRoot2        = "Scheduling Enhancement and Panels"
$subRoot2sub1    = "Scheduling Panels"
$subRoot3        = "SQL Server"

#source folders
$HW      = "0.Hardware"
$3SMDoc  = "0.Hardware\TPMC-3SM.Documentation"
$4SMDoc  = "0.Hardware\TPMC-4SM.Documentation"
$4SMDDoc = "0.Hardware\TPMC-4SM-FD.Documentation"
$730Doc  = "0.Hardware\TSW-730.Documentation"
$730OLH  = "0.Hardware\TSW-730.OLH"
$CENRVS  = "0.Hardware\CEN-RVS.Notes"

$ProjMgmt = "0.Project Management"

$SW            = "0.Software"
$RVLicensing   = "0.Software\0.RoomView.License"
$RVNotes       = "0.Software\0.RoomView.Notes"
$SQLLicensing  = "0.Software\database.SQL.Licensing"
$SQLNotes      = "0.Software\database.SQL.Notes"
$FRVMarketing  = "0.Software\Fusion RV.Marketing"
$FRVNetworking = "0.Software\Fusion RV.Networking"
$FRVNotes      = "0.Software\Fusion RV.Notes"


###############################
#create the directory structure
###############################

md -Path $FSG\$containerFolder -Name $rootFolder

cd $FSG\$containerFolder\$rootFolder
md "eControl and xPanels"
md "Fusion Server" #$subRoot1
md "Getting Started as a User"
md "Project Management"
md "RoomView Connected Displays"
md "Scheduling Enhancement and Panels" #$subRoot2
md "SQL Server" #$subRoot3

cd $FSG\$containerFolder\$rootFolder\$subRoot1
md "CEN-RVS"
md "Licenseing Information"
md "Networking"
md "Official Documentation"
md "Prerequisites, including powerShell script"
md "Product Info"
md "Requirements"
md "Tech Info"
md "Windows Authentication to Fusion RV"

cd $FSG\$containerFolder\$rootFolder\$subRoot2
md "Outlook Add-in"
md "Scheduling Panels" #$subRoot2sub1

cd $FSG\$containerFolder\$rootFolder\$subRoot2\$subRoot2sub1
md "TPMC-3SM"
md "TPMC-4SM"
md "TPMC-4SM-FD"
md "TSW-730"

cd $FSG\$containerFolder\$rootFolder\$subRoot3
md "Multi-database model only"
md "SQL Licensing"

cd $FSG\$containerFolder
#reset current folder


###############################
#copy the files
###############################

#Copy-Item -Path C:\fso\20110314.log -Destination c:\fsox\mylog.log

#To the root
Copy-item -Path $FSG\$ProjMgmt\starter\"Fusion Support Group Contact info*.pdf" -Destination $FSG\$containerFolder\$rootFolder\
Copy-item -Path $FSG\$containerFolder\"Fusion for IT and AV professionals release notes.txt" -Destination $FSG\$containerFolder\$rootFolder\

#to eControl and xPanels
Copy-item -Path $FSG\$SW\xpanel.Notes\starter\*.* -Destination $FSG\$containerFolder\$rootFolder\"eControl and xPanels"\

#to Fusion Server
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\"[RoomView] Versions explained*.pdf" -Destination $FSG\$containerFolder\$rootFolder\"Fusion Server"\
Run Code Online (Sandbox Code Playgroud)

我可以做什么来逃避方括号并仍然使用Copy-Itemcmdlet 的通配符文件名部分?

HAL*_*256 19

是的,你是对的,双重反击方法有效.但是,问题是您使用双引号而不是单引号来包含您的字符串.双反引线解决方案似乎只适用于单引号.

所以固定的代码行是:

Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'``[RoomView``] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\'Fusion Server'\
Run Code Online (Sandbox Code Playgroud)

文件路径和有线字符等的另一个好资源是阅读本文:从字面上看事物(如文件路径)

  • 如果这解决了您的问题,您可以接受答案. (3认同)
  • 同意[hal9256](/sf/users/150504441/).不酷OP复制了你的答案的本质[进入他自己的](/sf/ask/1470572631/# 21054644)并标记为正确.一点都不酷. (3认同)
  • @HAL9256:你是对的(+1):由于_bug_ - 请参阅[此 GitHub 问题](/sf/answers/3787574731/) - 将(未转义的)“?”或“*”与转义的“[”和“]”要求对后者进行双重转义(如目标 cmdlet/提供程序所见)。这种晦涩的行为绝对没有理由(我实际上[遇到过这个](/sf/answers/3787574731/))。该错误一定存在于提供程序代码中,因为通配符本身可以正常工作:`'a[b' -like 'a\`[*'` 是 `$true`,而 `'a[b' -like 'a\` \`[*'` - 正确地 - 抱怨_无效模式_。 (2认同)

mkl*_*nt0 6

一个概述和一些背景资料

  • 为了有效地转义您希望逐字解释为通配符表达式的一部分的字符,必须将其`转义为目标 cmdlet(其底层 PowerShell 驱动器提供程序)所见

  • 确保这会变得很棘手,因为`(backtick)用作双引号字符串( "...") 和不带引号的命令参数(大多数情况下,其行为类似于双引号字符串)中的转义字符

注意:问题中的场景不允许使用-LiteralPath,但在您知道路径是具体的文字路径的情况下,使用-LiteralPath-lp在 PowerShell Core 中可以缩写为)是最佳选择- 请参阅此回答

将参数传递-Path给与 PowerShell 驱动器提供程序相关的 cmdlet ( Get-ChildItem, Copy-Item, Get-Content, ...)的通配符支持参数并且您希望[并被逐字]处理而不是作为字符集/范围表达式时:

  • 字符串文字表示

    • 'file`[1`].txt'

      • `字符。内按原样保留'...',因此目标 cmdlet 可以按预期看到它们。
    • "file``[1``].txt"

      • ``,即内部需要加倍以在结果字符串中"..."保留单个 `(第一个 `是(双引号)字符串内部转义字符,第二个`是它转义的字符,要通过)。
    • file``[1``].txt

      • 未加引号的命令参数同上,(在大多数情况下)其作用类似于 "..."
    • 警告由于错误- 请参阅此 GitHub 问题-混合(未转义)?*与转义[]要求后者双重转义(与``,如目标 cmdlet/提供者所见)

      • 如果你想文本文件名匹配file[1].txt用通配符模式匹配[] 字面上,同时含有特殊字符*(匹配任何字符运行),而不是预期的'file`[1`]*',你就必须使用'file``[1``]*'(原文如此); 使用双引号或未转义的参数,您必须有效地使用四重反引号:"file````[1````]*"/ file````[1````]*-有关更多信息,请参阅此答案

      • 需要注意的是直接使用通配符与-like操作者没有受到影响:

        • 'a[b' -like 'a`[*'是 - 正确 - $true
        • 'a[b' -like 'a``[*'- 理所当然地 - 抱怨无效模式
      • 同样,参数-Include-Exclude不会受到影响

      • -Filter开始时遵循不同的规则[...]因为根本不支持构造,并且[]字符。都始终认为文字(同样,看到这个答案)。

  • 通过变量以编程方式转义路径字符串,请使用:

      $literalName = 'file[1].txt'
      $escapedName = [WildcardPattern]::Escape($literalName) # -> 'file`[1`].txt'
    
    Run Code Online (Sandbox Code Playgroud)


小智 5

我用这个:

 Copy-Item  $file.fullname.replace("[", "``[").replace("]", "``]") $DestDir
Run Code Online (Sandbox Code Playgroud)


Yet*_*ser -3

'和 之间有区别`

  • 第一个是单引号,它是键上的非移位字符"
  • 第二个是我以为我正在使用但实际上没有使用的反引号。它是按键上的非移位字符~

这有效:

# to Fusion Server
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'``[RoomView``] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\"Fusion Server"\
Run Code Online (Sandbox Code Playgroud)

  • -1 [hal9256](/sf/users/150504441/) 提供了完全相同的[本页其他地方的相同答案](/sf/ask/1470572631/ -文件名中的括号和使用通配符/#21009701),没有一些格式差异。通过将他们的答案(而不是您复制的副本)标记为答案来奖励那些回答人们问题的人。 (2认同)