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)
文件路径和有线字符等的另一个好资源是阅读本文:从字面上看事物(如文件路径)
一个概述和一些背景资料:
为了有效地转义您希望逐字解释为通配符表达式的一部分的字符,必须将其`转义为目标 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)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           23434 次  |  
        
|   最近记录:  |