Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Powershell - Insert a Number Before folder Name - Increment

    Hey folks,


    does any one have any idea how i can use power shell or a bat file to add a number to a folder

    example folders before

    Affenpinscher
    Alaskan Malamute
    Staffordshire Bull Terrier
    Shepherd Dog


    After

    1. Affenpinscher
    2. Alaskan Malamute
    3. Staffordshire Bull Terrier
    4. Shepherd Dog

    Ive looked everywhere and i just cant seem to figure it out

    I thought this may be a simple common operation people do rename folders with numbers

    get-childitem | % { rename-item $_ " "} - and stuck after many combinations



    thanks

    pb

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

    This PS will do the trick:
    Code:
    $BasePath = "G:\Test\Dogs"
    $Index = 0
    $Folds = Get-ChildItem -Path $BasePath -Attributes "D"
    
    ForEach ($Folder in $Folds) {
      
      $Index += 1
      Rename-Item -Path "$($Folder.fullname)" -NewName "$Index.$Folder"
    
    }
    
    "$Index folders were processed."
    Before:
    DogsBefore.PNG

    After:
    DogsAfter.PNG

    Didn't know if you wanted a space after the period but if you do just add it here: "$Index. $Folder"

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    pb89 (2016-05-12)

  4. #3
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    Thanks RG,

    you are a SUPER CHAMPION -

    you beat the mighty stack exchange - been lurking on their for hours fiddling about trying to get something to work

    I know you can use the bulk rename utility but that was not installed on the other machine - so i had to think of a script

    Im still a powershell baby - but my mini family of scripts are growing

    Have a fantastic evening



    pb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •