【问题标题】:How to remove emojis from a filename using PowerShell?如何使用 PowerShell 从文件名中删除表情符号?
【发布时间】:2018-08-30 10:01:03
【问题描述】:

如何使用 PowerShell 从文件名中查找和删除表情符号? 例如,我想删除 ???? 之类的表情符号还有??????。

我尝试了以下代码,但它不起作用。看来 PowerShell 无法处理 utf32 编码。

Get-ChildItem -recurse . | where {$_.Name -match "[\u1F600\u1F64F]"}

【问题讨论】:

    标签: powershell filenames emoji


    【解决方案1】:

    表情符号在 Powershell 中表示为该范围内的 16 位代理对。像 0x1F600 这样的代码太高了,无法用 16 位表示,这是 powershell 使用的。表情符号实际上是 2 个字符长。单独它们是不可打印的。 -cmatch 是一种预防措施,因为有几个 unicode 字符在 ascii 范围 İ K 中具有小写版本。无论如何,将不区分大小写的 -match 与 unicode 范围一起使用是没有意义的。请注意,使用非 ascii 字符编码的“utf8 no bom”脚本在 powershell 5 中不起作用。

    # U+D800 to U+DBFF (called "high surrogate") gets combined with another 
    # Unicode code point from range U+DC00 to U+DFFF (called "low surrogate")
    
    echo hi > file??
    dir | where name -cmatch '[\uD800-\uDFFF]' | 
      rename-item -newname { $_.name -creplace '[\uD800-\uDFFF]' } -whatif
    
    What if: Performing the operation "Rename File" on target "Item:
     C:\Users\js\foo\file?? Destination: C:\Users\js\foo\file".
    

    Surrogate Pairs and Variation Selectors

    【讨论】:

      【解决方案2】:

      您可以使用该网站上的正则表达式:Emojis in Javascript。我尝试了几种不同的表情,似乎效果很好。

      正则表达式字符串:

      (?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32-\ude3a]|[\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])
      

      【讨论】:

        【解决方案3】:

        您可以只粘贴符号(在 v5.1 上运行);我想如果 Windows 可以解释它,那么 PowerShell 也可以。

        Example

        【讨论】:

        • 我知道,这是可能的。但不能解决我的问题。我想用任何表情符号查找文件名。这就是我尝试使用范围的原因。
        • 对不起,我误会了。我会添加评论,但不能直到 50 代表 :( 也许你可以使用正则表达式 \W 过滤?
        • gci | ?{$_.name -match '[^ -~]+'} 看起来效果不错
        • 注意:除非您将脚本保存为 utf-bom 格式,否则您的 unicode 将不会被保留。
        猜你喜欢
        • 1970-01-01
        • 2014-09-14
        • 2013-05-03
        • 1970-01-01
        • 2016-10-02
        • 2018-09-22
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多