我在Windows 7上使用PowerShell,并编写一个脚本将一堆文件从一个文件夹结构复制到另一个文件夹结构。有点像编译。PowerShell Copy-Item cmdlet认为方括号是某种通配符,由于某种原因,我无法转义它们。
我不能使用-LiteralPath,因为我想使用星号*通配符,因为文件名有一个日期作为文件名的一部分,而且日期会改变。日期用作版本号。
这个职位是有帮助的,但是没有多少滴答(每个括号2倍或4倍)可以转义方括号。
我没有收到错误;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的通配符文件名部分?
发布于 2019-09-03 20:15:04
概述和一些背景信息
`**-escaped 逐字解释为 通配符表达式的字符,必须是_E 219,正如目标cmdlet**_ (其底层PowerShell驱动器提供程序)所看到的那样。` (回勾号)是,也是,用作双引号字符串 (**"..."**)和E 140未引号命令参数e 241(在大多数情况下,这些字符串的行为类似于双引号字符串)。注意:问题中的场景不允许使用-LiteralPath,但是如果您知道路径是具体的,文字路径,则使用(可以缩短为PowerShell中的 -lp E 159E 260 Core)是最好的选择E 264--参见这个答案。
当将一个参数传递给支持通配符的参数时,PowerShell驱动器提供程序相关的cmdlet (Get-ChildItem,Copy-Item,Get-Content,.)您希望[和]被逐字处理,而不是作为字符集/范围表达式处理:
- `'file`[1`].txt'` - ``` chars. are preserved as-is inside `'...'`, so the target cmdlet sees them, as intended.- `"file``[1``].txt"` - ````, i.e. _doubling_ is needed inside `"..."` in order to preserve a _single_ ``` in the resulting string (the _first_ ``` is the (double-quoted) _string-internal_ escape character, and the second ``` is the character it escapes, to be passed through).- `file``[1``].txt` - Ditto for unquoted command arguments, which (for the most part) act like `"..."`- **Caveat**: **Due to a** _**bug**_ - see [this GitHub issue](https://stackoverflow.com/a/54108210/45375) - _**mixing**_ **(unescaped)** **`?`** **or** **`*`** **with escaped** **`[`** **and** **`]`** **requires the latter to be** _**doubly**_ **escaped (with** **````****, as seen by the target cmdlet / provider)**: - If you wanted to match literal filename `file[1].txt` with a wildcard pattern that matches `[` and `]` _literally_ while _also_ containing special character `*` (to match any run of characters), instead of the expected `'file`[1`]*'`, you'll have to use `'file``[1``]*'` (sic); with a double-quoted or unescaped argument you then have to effectively use _quadruple_ backticks: `"file````[1````]*"` / `file````[1````]*` - see [this answer](https://stackoverflow.com/a/54108210/45375) for more. - Note that _direct_ use of wildcards with the `-like` operator is _not_ affected: - `'a[b' -like 'a`[*'` is - correctly - `$true`,
- whereas `'a[b' -like 'a``[*'` - rightfully - complains about an _invalid pattern_. - Similarly, parameters **`-Include`** **and** **`-Exclude`** **are** _**not**_ **affected**. - **`-Filter`** **plays by different rules** to begin with: `[...]` as a construct isn't supported at all, and `[` and `]` chars. are _always_ considered _literals_ (again, see [this answer](https://stackoverflow.com/a/54108210/45375)).[1].txt’发布于 2019-02-19 13:53:00
我用这个:
Copy-Item $file.fullname.replace("[", "``[").replace("]", "``]") $DestDir发布于 2014-01-09 01:13:41
Powershell自动选项卡-完成文件名的方式通常是最好的方式,
示例:
copy-item '.\file`[test`].txt'https://stackoverflow.com/questions/21008180
复制相似问题