SoylentLover2002
SoylentLover2002

Reputation: 3

How to create enumerated directories in subdirectories in PowerShell?

I want to create a folder in each subfolder with a number in it's name.

Lets say I have a following folder structure in my root folder:

\sub1\
\sub2\
\sub3\

after I've ran the command it should look like this:

\sub1\fold1
\sub2\fold2
\sub3\fold3

I'm new to all of this terminal stuff so i could only come up with how to create a folder in each subdirectory:

ls -directory | % {md ($_.name + '\FolderName')}

Thanks in advance.

Upvotes: 0

Views: 67

Answers (1)

mklement0
mklement0

Reputation: 437933

Assuming you want sequence numbers starting with 1, irrespective of the name of the parent directory:

$i=1
ls -directory | % { md (Join-Path $_.FullName "Folder$(($i++))") }

To instead incorporate the number embedded in the parent directory name:

ls -directory | % { md (Join-Path $_.FullName "Folder$($_.Name -replace '\D')") }

Upvotes: 1

Related Questions