Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何使用powershell批量重命名文件以删除特殊字符_Windows_Powershell_Rename - Fatal编程技术网

Windows 如何使用powershell批量重命名文件以删除特殊字符

Windows 如何使用powershell批量重命名文件以删除特殊字符,windows,powershell,rename,Windows,Powershell,Rename,只是想改变一堆以 02. SongName.Mp3 02 - OtherName.Mp3 02 MoreNames.Mp3 03. Song.mp3 03 - Songs.mp3 到 我知道这是一个非常简单的命令,但它已经从我的大脑中消失了。我在文件夹上使用文件>打开>powershell,因此不需要使用dir或cd或其他任何东西 Get-ChildItem *.mp3 | Where-Object Name -match '^.{4}(.+).\.(.+)$' |

只是想改变一堆以

02. SongName.Mp3
02 - OtherName.Mp3
02 MoreNames.Mp3
03. Song.mp3
03 - Songs.mp3

我知道这是一个非常简单的命令,但它已经从我的大脑中消失了。我在文件夹上使用文件>打开>powershell,因此不需要使用dir或cd或其他任何东西

   Get-ChildItem *.mp3 |
     Where-Object Name -match '^.{4}(.+).\.(.+)$' | 
       Copy-Item -Destination { $Matches.1 + '.' + $Matches.2 }
这正是我想要做的。特别注意,它复制文件,而不重命名现有文件

Get-ChildItem *.mp3 | Rename-Item -NewName { $_.Name -replace '^[^\p{L}]+' } -WhatIf
注意:上面命令中的预览操作。一旦确定操作将执行所需操作,请删除
-WhatIf

上述方法通过删除文件名中第一个字母(
\p{L}
)之前的任何字符来重命名
*.mp3
文件;名称已以字母开头的文件将保持不变

使用的技术:

  • 传递给
    -NewName
    的(
    {…}
    ),为
    Get ChildItem
    发出的每个输入对象求值,并绑定到自动变量
    $\ucode>

  • 基于正则表达式的


    • 我要试试看。假设所有文件都是mp3文件,则将数字、点、破折号和空格替换为零。(或“复制项目-目的地”)


      例如,对于
      '02 MoreNames.Mp3'
      ,这不能正常工作;正如我的回答所示,最好不要依赖固定的角色位置和按角色类型匹配。不是我投了反对票,但出于所述原因,我确实认为你的解决方案不太理想(通过使用带有
      Copy Item
      的延迟绑定脚本块,而不是
      Where Object
      调用,也可以提高效率)。也就是说,即使在这种情况下我不推荐,您也可以在发布48小时后接受自己的答案。请允许我在下一篇评论中为您提供给新来者的标准建议。如果答案解决了您的问题,请单击大复选标记(✓) 在它旁边,也可以选择向上投票(向上投票要求至少15个信誉点)。如果您发现其他答案有帮助,请向上投票。接受(您将获得2个信誉点)和向上投票有助于未来的读者。如果您的问题尚未完全回答,请提供反馈或建议。
      Get-ChildItem *.mp3 | Rename-Item -NewName { $_.Name -replace '^[^\p{L}]+' } -WhatIf
      
      # test files
      # echo hi | set-content '02. SongName.Mp3','02 - OtherName.Mp3','02 MoreNames.Mp3','03. Song.mp3','03 - Songs.mp3'
      
      dir *.mp3 | rename-item -newname { ($_.basename -replace '[\d\.\- ]') + '.mp3' } -whatif
      
      What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/02 - OtherName.Mp3 Destination: /Users/js/foo/OtherName.mp3".
      What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/02 MoreNames.Mp3 Destination: /Users/js/foo/MoreNames.mp3".
      What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/02. SongName.Mp3 Destination: /Users/js/foo/SongName.mp3".
      What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/03 - Songs.mp3 Destination: /Users/js/foo/Songs.mp3".
      What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/03. Song.mp3 Destination: /Users/js/foo/Song.mp3".