-1

I have a folder containing multiple sub folders named like : 12345 - textfoldername

I want to rename all these sub folders by keeping just the first number (12345) and delete all the rest ( - textfoldername).

How can I build the windows script for that.

Thanks for your help !

Souhail Ouabi
  • 105
  • 3
  • 15
  • 3
    Welcome to Stack Overflow. SO is a question and answer page for professional and enthusiastic programmers. Please add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. – Cyrus Jun 06 '20 at 10:20
  • 1
    Are you sure you want to use `bash` on windows? Did you mean `batch` (as in Windows `cmd`) instead? – Socowi Jun 06 '20 at 10:40

1 Answers1

0

With , use Get-ChildItem to discover all the subfolders, then use Rename-Item to rename:

Get-ChildItem path\to\root\directory -Directory |Rename-Item -NewName {$_.Name -replace ' - .+$'}

The -replace operator with remove - and anything thereafter in the existing name (or, if - something isn't found, ignore it)

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206