【问题标题】:Renaming multiple filenames in PowerShell sometimes doesn't work在 PowerShell 中重命名多个文件名有时不起作用
【发布时间】:2020-12-21 16:15:33
【问题描述】:

我问了一个问题,我尝试使用 PowerShell 从众多文件夹中的众多文件的名称中删除部分内容。可能需要删除的部分是“-xx_xx”“-yy_yy”“-zz_zz”等。

为此,我正在使用这个:

Get-Childitem -Recurse | Rename-Item -NewName { $_.Name -replace " - xx_xx","" -replace " - yy_yy","" -replace " - zz_zz",""}

效果很好。

但是,我收到一条说明,指出对于特定项目,命名约定将会改变。类似于“-xx_xx-yy_yy-A-B”或“-xx_xx_zz_zz-A-B”或“-xx_xx_yz_yz-A-B”等...注意没有空格。我以为我可以简单地将其放入其中,它会起作用,例如:

Get-Childitem -Recurse | Rename-Item -NewName { $.Name -replace "-xx_xx-yy_yy-A-B","" -replace "-xx_xx_zz_zz-A-B","" -replace "-xx_xx_yz_yz-A-B","" }

但是......它不起作用。我认为这可能与连字符有关,但除非我以某种方式错误地这样做,否则将它们转义也不起作用,这是很有可能的。

这是我收到的错误消息:

Rename-Item : The input to the script block for parameter 'NewName' failed. The term '$.Name' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:47
+ ... em -NewName { $.Name -replace "-xx_xx-yy_yy-A-B","" -replace "-xx_xx- ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (True Dialog Requests-xx_xx-yy_yy-A-B.htm:PSObject) [Rename-Item], Para
   meterBindingException
    + FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.RenameItemCommand
  1. 如何让它发挥作用
  2. 为什么它可以与“-xx_xx”等一起使用,而不是现在?
  3. 是否有更好的方法可以同时兼顾两者?

【问题讨论】:

  • 最好先完成get-childitem。 (Get-Childitem -Recurse) | rename-item

标签: powershell rename


【解决方案1】:

我目前正在使用 VBA 来完成这项任务。 在使用它之前,您必须从参考安装:
Microsoft 脚本运行时

A 列中是文件夹中的现有名称。
D列是他们应该有的名字。

Sub RenameMultipleFiles()
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = -1 Then
        selectDirectory = .SelectedItems(1)
        dFileList = Dir(selectDirectory & Application.PathSeparator & "*")
    
        Do Until dFileList = ""
            curRow = 0
            On Error Resume Next
            curRow = Application.Match(dFileList, Range("A:A"), 0)
            If curRow > 0 Then
                Name selectDirectory & Application.PathSeparator & dFileList As _
                selectDirectory & Application.PathSeparator & Cells(curRow, "D").Value
            End If
    
            dFileList = Dir
        Loop
    End If
End With
End Sub

如果 A 列中没有现有文件名列表,这里是如何收集它们的示例:

Option Explicit
Sub ListAllFiles()
Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("Your Path")
Call getFilesDetails(objFolder)

End Sub

Function getFilesDetails(objFolder As Scripting.Folder)
Dim objFile As Scripting.File
Dim nextRow As Long
nextRow = Cells(Rows.count, 1).End(xlUp).Row + 1
For Each objFile In objFolder.Files
   Cells(nextRow, 1) = objFile.Name
   nextRow = nextRow + 1
Next
End Function

【讨论】:

    【解决方案2】:

    你需要使用$_.Name

    Get-Childitem -Recurse | Rename-Item -NewName { $_.Name -replace "-xx_xx-yy_yy-A-B","" -replace "-xx_xx_zz_zz-A-B","" -replace "-xx_xx_yz_yz-A-B","" }
    

    【讨论】:

    • 就是这样。非常感谢。不敢相信我刚刚遗漏了一个_
    猜你喜欢
    • 2018-12-17
    • 1970-01-01
    • 2011-10-21
    • 2020-11-02
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2015-05-26
    相关资源
    最近更新 更多