简体   繁体   中英

how to recursively change name across several folders in powershell

i am trying to recursively rename files in a folder that contains a bunch of folders.

to better clarify, i have a parent folder with 10 child folders, each of the 10 child folders have 17 sound files.

i need to rename each of the 17 sound file 1,2,3...17

i managed to come up with the following code (which for now, writes the file name instead of actually changing it)

$files = gci -Path "D:\PARENT_FOLDER" -Recurse
$i = 1
foreach ($file  in $files) {

$newName = 0
1..$i | % {$newName = $newName+1}
$i++
Write-Host "name is " $newName
}

but i cant figure out how to make it reset the count between folders. right now the code outputs names from 1 to 180...

can anybody help me figure this out? thanks in advance

ok so after a long day at work, i got home and tackled this from scratch and came up with a simpler solution: i just nested "foreach" loops one inside the other to cycle thru all the folders inside the parent folder and all the files inside each folder.

if anybody is interested here is the code:

$path = "MASTER FOLDER PATH"

$list = Get-ChildItem -Path $path -Directory

foreach ($folder in $list)
{
Write-Host "working on Directory" $folder.FullName -ForegroundColor Green

        foreach ($files in $folder)
        {$files = Get-ChildItem -Path $path\$folder
                $i=1
                foreach ($file in $files) {


                $newName = 0
                1..$i | % {$newName = $newName+1}
                $i++

                Write-Host "Changing file " $file.FullName -NoNewline -ForegroundColor Yellow
                Write-Host " ..." -ForegroundColor Yellow
                Rename-Item -Path $file.FullName -NewName $newName


                }
        }
} 

i appreciate your help. :)

I'd recommend leaving out the -Recurse part for now and to do the recursive things manually (AKA in a loop).

$list = Get-ChildItem -Path $path -Name -Directory

for ($i=0;$i -le $list.Length-1;$i++) {
$list[$i] = $path + '\' + $list[$i]
}

Now you have an array of folders, which you can now individually adress.

$path = "(path)" 
$list = Get-ChildItem -Path $path -Name -Directory

for ($i=0;$i -le $list.Length-1;$i++) {
$list[$i] = $path + '\' + $list[$i]
$temp = Get-ChildItem -Path $list[$i] -Name

for ($h=0;$h -le $temp.Length-1;$h++) {
$temp[$h] = $list[$i] + $temp[$h]
Rename-Item -LiteralPath $temp[$h] -NewName $h
}}

Thats what I came up with, hope it was helpful.

Here's another way. I don't see how to recurse and reset the number for each folder. This way I'm doing one folder at a time. Foreach with two script blocks will treat the first one as a "begin" script block.

foreach($dir in get-childitem parent) {
  get-childitem parent\$dir | foreach {$i = 1} { rename-item $_.fullname $i -whatif; $i++ }
}

What if: Performing the operation "Rename File" on target "Item: C:\users\js\parent\foo\a Destination: C:\users\js\parent\foo\1".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\parent\foo\b Destination: C:\users\js\parent\foo\2".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\parent\foo\c Destination: C:\users\js\parent\foo\3".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\parent\foo2\a Destination: C:\users\js\parent\foo2\1".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\parent\foo2\b Destination: C:\users\js\parent\foo2\2".
What if: Performing the operation "Rename File" on target "Item: C:\users\js\parent\foo2\c Destination: C:\users\js\parent\foo2\3".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM