mercredi 4 décembre 2019

Is there a way to rename files in a folder in a pattern using powershell?

I have the task to rename a bunch of files/.tif, using a certain pattern. The files in the folder are in correct order. I have to iterate through two variables. From A-R and from 1-20. It should start with "A01" and if it hits "A20" it should move to "B01".. and so on. Until "R20".

I created two variables called letters and numbers and used two for-loops to iterate through them and to print them. This works pretty well. Afterwards I created another variable to store the result from it. The task I'm stuck with is the renaming part. This is the code I am with at the moment.

$letters = @("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R")
$numbers = @("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20")

for($i = 0; $i -lt $numbers.Length; $i++){
 for($j = 0; $j -lt $letters.Length; $j++){
    $Output = $letters[$j], $numbers[$i]
    $newFileName = $Output+".tif"
 }
}

After the last line, I tried something like:

Dir | %{Rename-Item $_ -NewName ($newFileName)}

but this fails in any variation.

Whats the next step in here/is it possible in the first way? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire