Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How can I reset a counter within each subfolder level in a loop that access recursively a parent directory?

$
0
0

I made a script to add a sequential prefix to all folders within a parent folder, but the counter keeps incrementing despite being in a level different than its parent, like...

Testing script:

# Create sample folders:ni -path $home\parent_folder -itemtype "directory"ni -path $home\parent_folder\child_a, $home\parent_folder\child_b -itemtype "directory"ni -path $home\parent_folder\child_a\grandchild_a, $home\parent_folder\child_b\grandchild_b -itemtype "directory"ni -path $home\parent_folder\child_a\grandchild_a\n_child_a, $home\parent_folder\child_b\grandchild_b\n_child_b -itemtype "directory"# Run loop recursively:dir $home\parent_folder -directory -recurse | foreach -begin { $i = 1 } -process { ren $_.fullname -newname ( "{0}_{1}" -f $i, $_.name ) ; $i++ }# Print output:dir $home\parent_folder -recurse | % {""} { write-host $_.fullname } {""} # Output:C:\Users\victo\parent_folder\1_child_aC:\Users\victo\parent_folder\2_child_bC:\Users\victo\parent_folder\1_child_a\3_grandchild_aC:\Users\victo\parent_folder\1_child_a\3_grandchild_a\4_n_child_aC:\Users\victo\parent_folder\2_child_b\5_grandchild_bC:\Users\victo\parent_folder\2_child_b\5_grandchild_b\6_n_child_b

I found two ways to handle this, but none of them are as efficient as I want them to be...

  1. Nested foreach loop function (not scalable):
Function prefix_dir ($parent_dir) # $parent_dir is the path to parent_folder{    $i = 1    foreach ($child_dir in (Get-ChildItem $parent_dir -directory))    {        $j = 1        foreach ($mini_dir in (Get-ChildItem $parent_dir\$child_dir -directory))        {            Rename-Item $mini_dir.fullname -newname ( "{0}_{1}" -f $j, $mini_dir.name )            $j++        }    Rename-Item $child_dir.fullname -newname ( "{0}_{1}" -f $i, $child_dir.name )    $i++    }}# Print output:dir $home\parent_folder -recurse | % {""} { write-host $_.fullname } {""} # Output:C:\Users\victo\parent_folder\1_child_aC:\Users\victo\parent_folder\2_child_bC:\Users\victo\parent_folder\1_child_a\1_grandchild_aC:\Users\victo\parent_folder\1_child_a\1_grandchild_a\n_child_aC:\Users\victo\parent_folder\2_child_b\1_grandchild_bC:\Users\victo\parent_folder\2_child_b\1_grandchild_b\n_child_b

As you can see from the function above the last level of folders is not affected coz another foreach loop is necessary (with another counter/iterator). I'd have to manually nest loops depending on a folder's levels—clearly not worthy.

  1. The other way is just to omit the recurse parameter in the first loop (on "testing script") and use cd or sl (aliases for Set-Location) to access each subfolder and run the loop again, only affecting the current directory.

My goal is to be able to add the prefix to all folders within the given parent folder and reset the counter to 1 in each sublevel. All in one script, similar to the first loop, recursively. And I know I'm probably missing a big concept here, but I thought it was about time to ask for help.

My desired output would be:

C:\Users\victo\parent_folder\1_child_aC:\Users\victo\parent_folder\2_child_bC:\Users\victo\parent_folder\1_child_a\1_grandchild_aC:\Users\victo\parent_folder\1_child_a\1_grandchild_a\1_n_child_aC:\Users\victo\parent_folder\2_child_b\1_grandchild_bC:\Users\victo\parent_folder\2_child_b\1_grandchild_b\1_n_child_b

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>