• Powershell – Insert a Number Before folder Name – Increment

    Home » Forums » Developers, developers, developers » DevOps Lounge » Powershell – Insert a Number Before folder Name – Increment

    Author
    Topic
    #505581

    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

    Viewing 2 reply threads
    Author
    Replies
    • #1563641

      PB,

      This PS will do the trick:

      Code:
      $BasePath = "G:TestDogs"
      $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:
      44460-DogsBefore

      After:
      44461-DogsAfter

      Didn’t know if you wanted a space after the period but if you do just add it here: [noparse]”$Index. $Folder”[/noparse]

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1563643

      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:rolleyes:

      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:p

      Have a fantastic evening

      :cheers:

      pb

    • #2524123

      HI All,

       

      I was wondering if you could me with the code to do the same with the directory below?

      PS C:\Users\Chris Hewitt\OneDrive\Documents\Tax Project\Action Log & Project Stuff\2022>

      $BasePath = “C:\Users\Chris Hewitt\OneDrive \Documents\Project\Action Log & Project Stuff\2022”

      >> $Index +0
      >> $Folds = Get-Childitem -Path $Basepath – Attributes “D”
      >> ForEach ($Folder in $Folds) {
      >>
      >> $Index += 1
      >> Rename-Item -Path “$($Folder.fullname)” -NewName “$Index.$Folder”
      >>
      >> }

    Viewing 2 reply threads
    Reply To: Powershell – Insert a Number Before folder Name – Increment

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: