none
Rename files recursively RRS feed

  • Question

  • Hi Expert,

    I am having a hard time renaming files within subfolders, i have this script:

    Get-ChildItem  -Recurse | where {$_.name -match "^(_2014-|2014-)"} | % { rename-item $_ "_000000$($_.name)"}

    it does rename the files on the current folder but in the subfolders keeps throwing this error;

    Rename-Item : Cannot rename because item at filename  does not exist. much appreciated

    Regards

    Friday, November 14, 2014 11:47 PM

Answers

  • Here it is again:

    Get-ChildItem  -Recurse -file -Include '2014*','_2014*' | 
         ForEach-Object{
             Rename-Item $_.Fullname ('{0}\_000000{1}' -f $_.Directory.Fullname,$_.name)
         }
    Watch out ther is no WhatIf this time. 


    ¯\_(ツ)_/¯


    • Edited by jrvModerator Saturday, November 15, 2014 10:21 AM
    • Marked as answer by brascon Saturday, November 15, 2014 1:02 PM
    Saturday, November 15, 2014 10:21 AM
    Moderator
  • I fixed that  long time ago but you are still using the old copy.

    Get-ChildItem  -Recurse -file -Include '2014*','_2014*' | 
         ForEach-Object{
             Rename-Item $_.Fullname ('{0}\_000000{1}' -f $_.Directory.Fullname,$_.name)
         }

    There was an obvious extra quote.


    ¯\_(ツ)_/¯

    • Marked as answer by brascon Saturday, November 15, 2014 12:36 PM
    Saturday, November 15, 2014 12:31 PM
    Moderator

All replies

  • If it works for the current folder then here is a function that will step through a folder structure, rename the appropriate files at each level in the folder structure.

    function Recurse-Folders([string]$path) {
    
      Set-Location -Path $Path
      
      #  $L = Get-Location
      #  Rename files at this location
    
      Get-ChildItem | where {$_.name -match "^(_2014-|2014-)"} | % { rename-item $_ "_000000$($_.name)"} 
        
      [String[]]$Folders  = Get-ChildItem "." -Directory
      ForEach ($F in $Folders)
      {
        Recurse-Folders($F)
        Set-Location -Path ".."
      }
    }
    

    Saturday, November 15, 2014 1:38 AM
  • C'mon guys do it the easy way:

    Get-ChildItem  -Recurse -file |
         ForEach-Object{
            Rename-Item $_.Fullname ('{0}\"_000000{1}' -f $_.Directory.Fullname,$_.name) -whatif
        }


    ¯\_(ツ)_/¯


    • Edited by jrvModerator Saturday, November 15, 2014 10:16 AM
    Saturday, November 15, 2014 3:10 AM
    Moderator
  • Thanks for the function Brian, but it did not work, regards
    Saturday, November 15, 2014 10:00 AM
  • Thanks jrv did not work either, regards
    Saturday, November 15, 2014 10:00 AM
  • Thanks jrv did not work either, regards

    What does "it did not work" mean?  You have to be clear.

    Notice the WHATIF!


    ¯\_(ツ)_/¯


    • Edited by jrvModerator Saturday, November 15, 2014 10:15 AM
    Saturday, November 15, 2014 10:15 AM
    Moderator
  • Here it is again:

    Get-ChildItem  -Recurse -file -Include '2014*','_2014*' | 
         ForEach-Object{
             Rename-Item $_.Fullname ('{0}\_000000{1}' -f $_.Directory.Fullname,$_.name)
         }
    Watch out ther is no WhatIf this time. 


    ¯\_(ツ)_/¯


    • Edited by jrvModerator Saturday, November 15, 2014 10:21 AM
    • Marked as answer by brascon Saturday, November 15, 2014 1:02 PM
    Saturday, November 15, 2014 10:21 AM
    Moderator
  • Thanks for the quick response, below is the error msg:

    Get-ChildItem : A parameter cannot be found that matches parameter name 'file'.
    char:30
    + Get-ChildItem  -Recurse -file <<<<  |
        + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    Regards


    • Edited by brascon Saturday, November 15, 2014 10:21 AM
    Saturday, November 15, 2014 10:21 AM
  • That explains why the function fails, you're using PS 2.0

    Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Exclude <string[]>] [-Force] [-Include <string[]>] [-Name] [-Recurse] [-U
        seTransaction] [<CommonParameters>]
        
    Get-ChildItem [-LiteralPath] <string[]> [[-Filter] <string>] [-Exclude <string[]>] [-Force] [-Include <string[]>] [-Name] [-Recurse
        ] [-UseTransaction] [<CommonParameters>]


    Saturday, November 15, 2014 10:27 AM
  • Thanks Brian i am upgrading now and report back, thanks a lot
    Saturday, November 15, 2014 10:30 AM
  • I have upgarde to powershell 4 but none of the scripts works,

    1-  the first script there were no error msgs,  i had executed the script as it is just changed the path of the folder, 

    Set-Location -Path "C:\test"

    2- Rename-Item : Illegal characters in path.
    At C:\rename1\test.ps1:3 char:9
    +         Rename-Item $_.Fullname ('{0}\"_000000{1}' -f $_.Directory.Fullname,$_.n ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Rename-Item], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemComman

    Rename-Item : Illegal characters in path.
    At C:\rename1\test.ps1:3 char:9
    +         Rename-Item $_.Fullname ('{0}\"_000000{1}' -f $_.Directory.Fullname,$_.n ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Rename-Item], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemComman

    Saturday, November 15, 2014 12:25 PM
  • I fixed that  long time ago but you are still using the old copy.

    Get-ChildItem  -Recurse -file -Include '2014*','_2014*' | 
         ForEach-Object{
             Rename-Item $_.Fullname ('{0}\_000000{1}' -f $_.Directory.Fullname,$_.name)
         }

    There was an obvious extra quote.


    ¯\_(ツ)_/¯

    • Marked as answer by brascon Saturday, November 15, 2014 12:36 PM
    Saturday, November 15, 2014 12:31 PM
    Moderator
  • Hey thank you very much worked exactly as intended sorry didnt see your reply,  much appreciated

    • Edited by brascon Saturday, November 15, 2014 12:38 PM
    Saturday, November 15, 2014 12:37 PM
  • Good - I am glad you got it.

    I think the takeaway here is the method of name construction.  Using a format string eliminates a lot of playing around.  Put it in you toolbox for other projects.

    Good lck and happy poshing.


    ¯\_(ツ)_/¯

    Saturday, November 15, 2014 12:43 PM
    Moderator
  • Thanks def will put it my scripts inventory,

    Thanks again,

    Saturday, November 15, 2014 12:44 PM