我在Windows 7上使用PowerShell,并编写脚本将一堆文件从一个文件夹结构复制到另一个文件夹结构。有点像编译。 PowerShell Copy-Item
cmdlet认为方括号[]是某种通配符,并且由于某种原因我无法逃脱它们。
我无法使用-LiteralPath
,因为我想使用星号*通配符,因为文件名的日期是文件名的一部分,日期会发生变化。日期用作版本号。
This post很有帮助,但是方括号中没有任何数量的刻度(每个括号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"\
这就是整个事情:
# 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"\
如何转义方括号并仍然使用Copy-Item
cmdlet的通配符文件名部分?
答案 0 :(得分:15)
是的,你是对的,双重反击方法有效。但是,问题是您使用双引号而不是单引号来包含您的字符串。双反引线解决方案似乎只适用于单引号。
所以固定的代码行是:
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'``[RoomView``] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\'Fusion Server'\
文件路径和有线字符等的另一个好资源是阅读本文:Taking Things (Like File Paths) Literally
答案 1 :(得分:3)
在PowerShell v 2.0及更高版本上,要使用的转义字符是反斜杠。例如,如果我们要从此字符串“[Servername:QA01]”中删除括号,这是我们从System Center Orchestrator中的Exchange Admin PowerShell cmdlet活动获得的输出,我们使用以下逻辑:
$string -replace '\[','' -replace '\]',''
>Servername: QA01
这很奇怪。请注意,您必须使用单引号(通常在PowerShell中暗示'精确地将其评估为写入',因此这是非常奇怪的语法)。
不要因为没有自己解决这个问题而感到难过,这是非常奇怪的语法。
答案 2 :(得分:1)
显然,方括号需要双反推才能逃脱,这是不寻常的。 Reference here
你确定不行吗?我已经看过几次了。
编辑:是的,它有效,你使用双引号而不是反引号。
双引号位于Enter键前面的撇号字符上方。 Backtick正好位于Escape键的下方,与代字号共享键,〜。
答案 3 :(得分:1)
Powershell自动制表文件名的方式通常是最好的方法,
示例:
copy-item '.\file`[test`].txt'
答案 4 :(得分:1)
概述和一些背景信息:
为了有效地转义要解释为wildcard expression一部分的 verbatim 字符,必须将其`
转义由目标cmdlet看到 (其底层PowerShell驱动器提供程序)。
确保可以变得棘手,因为 `
(反引号)也被 用作双引号字符串中的转义字符 ({"..."
)和不带引号的命令参数 (在大多数情况下,它们的行为类似于双引号的字符串)。
注意:问题中的情况不允许使用-LiteralPath
,但是在您知道某个路径是具体的文字路径的情况下,请使用-LiteralPath
(最好在PowerShell Core 中将其缩写为
-lp
)-请参见this answer。
将参数传递给PowerShell驱动器提供商相关cmdlet的通配符支持-Path
参数({{1},Get-ChildItem
,{{1} },...),您希望将Copy-Item
和Get-Content
视为 verbatim 而不是字符集/范围表达式:
字符串-文字表示形式:
[
]
个字符。被保留在'file`[1`].txt'
中,因此目标cmdlet可以按预期看到它们。 `
'...'
,即"file``[1``].txt"
内需要 double ,以便在结果字符串中保留单 ``
( 第一个 "..."
是(双引号)内部字符串的转义字符,第二个`
是其转义的字符,将被传递通过)。 `
`
注意事项:由于出现了 bug -请参见this GitHub issue- 混合(未转义的)file``[1``].txt
或"..."
,且已转义的?
和*
要求后者被 double 转义(使用[
,如目标cmdlet /提供程序所见):
如果您想使用通配符模式将文字文件名]
与通配符模式相匹配,而通配符模式应与{em> 相匹配,而也应 包含特殊字符``
(以匹配任意字符),而不是预期的file[1].txt
,您必须使用[
(原文如此);然后使用双引号或不转义的参数,则必须有效地使用四倍反引号:]
/ *
-有关更多信息,请参见this answer。
请注意,直接通过'file`[1`]*'
运算符直接使用通配符不会受到影响:
'file``[1``]*'
-是-"file````[1````]*"
,file````[1````]*
-理所当然-抱怨无效的模式。类似地,参数 -like
和'a[b' -like 'a`[*'
不受影响。
$true
遵循不同的规则,其开头为:'a[b' -like 'a``[*'
,因为根本不支持构造,-Include
和{{ 1}}个字符。总是 被视为文字(再次参见this answer)。
要通过变量以编程方式转义路径字符串 ,请使用:
-Exclude
答案 5 :(得分:0)
一个选项是使用遗留目录获取文件名,这将允许您使用*通配符,但不会尝试“blob”方括号。然后使用-literalpath
将该列表提供给move-item cmd /c dir *]* /b |
foreach { Move-Item -LiteralPath $_ -Destination <destination path> }
答案 6 :(得分:0)
'
和`
之间存在差异:
这有效:
#to Fusion Server
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'``[RoomView``] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\"Fusion Server"\
答案 7 :(得分:0)
假设没有其他匹配项,则可以使用?而不是括号。名为“ a [h-j]”的文件,复制到目录“ foo”:
copy-item a?h-j? foo
答案 8 :(得分:0)
我用这个:
Copy-Item $file.fullname.replace("[", "``[").replace("]", "``]") $DestDir