Becky
Becky

Reputation: 5585

Rename files to match filenames in another folder

I have 2 directories.

D:\test\get\f

D:\test\set\f

Inside get folder and set folders there are equal amount of files. All files in set are the same (it's a common image file duplicated multiple times to match the number of files in set folder).

My question is, how do I rename all files in set folder same as in get folder?

e.g.:

BEFORE

get folder

apple.jpg
mango.jpg
lychee.jpg

set folder

123.jpg
12!.jpg
asdasd.jpg

AFTER

get folder

apple.jpg
mango.jpg
lychee.jpg

set folder

apple.jpg
mango.jpg
lychee.jpg

Upvotes: 1

Views: 2029

Answers (1)

Alec  Collier
Alec Collier

Reputation: 1523

A simple example which is unoptimised and assumes best case. It shows the basic steps of iterating through files in a path, getting file names and renaming files.

$names = @()
$getPath = "C:\Temp\get"
$setPath = "C:\Temp\set"
Get-ChildItem $getPath |
    Foreach-object{
    $names += $_.Name
}
$i = 0
Get-ChildItem $setPath |
Foreach-object{
    Rename-Item -Path $_.FullName -NewName $names[$i]
    $i++
}

Upvotes: 2

Related Questions