How can I bulk rename files in PowerShell?

前端 未结 8 2040
春和景丽
春和景丽 2021-01-30 10:36

I\'m trying to do the following:

Rename-Item c:\\misc\\*.xml *.tmp

I basically want to change the extension on every files within a directory t

相关标签:
8条回答
  • 2021-01-30 10:55

    From example 4 in the help documentation of Rename-Item retrieved with the command:

    get-help Rename-Item -examples
    

    Example:

    Get-ChildItem *.txt| Rename-Item -NewName { $_.Name -replace '\.txt','.log' }
    

    Note the explanation in the help documentation for the escaping backslash in the replace command due to it using regular expressions to find the text to replace.

    To ensure the regex -replace operator matches only an extension at the end of the string, include the regex end-of-string character $.

    Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '\.txt$','.log' }
    

    This takes care of the case mentioned by @OhadSchneider in the comments, where we might have a file named lorem.txt.txt and we want to end up with lorem.txt.log rather than lorem.log.log.

    Now that the regex is sufficiently tightly targeted, and inspired by @etoxin's answer, we could make the command more usable as follows:

    Get-ChildItem | Rename-Item -NewName { $_.Name -replace '\.txt$','.log' }
    

    That is, there is no need to filter before the pipe if our regex sufficiently filters after the pipe. And altering the command string (e.g. if you copy the above command and now want to use it to change the extension of '.xml' files) is no longer required in two places.

    0 讨论(0)
  • 2021-01-30 10:58

    This seems to work and is a pythonic i.e simple is better than complex (https://www.python.org/dev/peps/pep-0020/) way of doing it (once you are in the directory):

    $files = Get-ChildItem -file -Filter *.xml;
      ForEach ($file in $files)
      {
      $n = $file.Basename
      Copy-Item -Path $file -Destination "$n.tmp"
      Remove-Item "$n.xml"
      }
    
    0 讨论(0)
  • 2021-01-30 11:01

    Here's another variant that will work.

    dir *.xml | Rename-Item -NewName {$_.BaseName + ".tmp"}
    

    $_.BaseName will do the "base" name without the (last) extension.

    0 讨论(0)
  • 2021-01-30 11:02

    a shortened version using the alias would be:

    ls *.xml | ren -new {$_.BaseName + ".tmp"}
    
    0 讨论(0)
  • 2021-01-30 11:02
    dir -Recurse | where-object -FilterScript {$_.Extension -eq ".xml"} | Rename-Item -NewName {[System.IO.Path]::GetFileNameWithoutExtension($_.fullname) + ".tmp"}
    

    use -WhatIf to evaluate the result first

    0 讨论(0)
  • 2021-01-30 11:03

    The existing answers suggest the -replace operator, but what if the file is called a.xml.xml? Both .xml substrings will be replaced and the end result would be a.tmp.tmp. Fortunately, there's a .NET method for this:

    Dir *.xml | rename-item -newname { [io.path]::ChangeExtension($_.name, ".tmp") } 
    

    (Manish Kumar was close with GetFileNameWithoutExtension but this is more elegant and probably a bit more efficient, not that it overly matters in this case)

    0 讨论(0)
提交回复
热议问题