简体   繁体   中英

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 AR 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!

The problem with your code is that within the loops to create the new file name, you do nothing with the file to rename itself, and you are overwriting the same variable $newFileName each time.

You could do like below:

$filePath = 'X:\tifs'  # put the path of the folder where the tif files are here
$letters  = "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R"

# initialize two counters
$i = $j = 0
Get-ChildItem -Path $filePath -Filter '*.tif' | ForEach-Object {
    $newFileName = '{0}{1:00}.tif' -f $letters[$i], $j++
    $_ | Rename-Item -NewName $newFileName -WhatIf
    if ($j -gt 20) {
        $i++    # go to the next letter
        $j = 0  # reset the number count
    }
    # if the letter counter has exceeded the number of letters, break out of the loop
    if ($i -gt $letters.Count) { break }
}

The -WhatIf switch is there to test first. In the console window you can see what the Rename-Item cmdlet would do. If you are satisfied with what is shown, remove the Whatif switch to actually start renaming.

This should do the trick:

$directory  = 'C:\directory\test'

$files      = Get-ChildItem -Path $directory -Filter '*.tif'
$char       = 65
$counter    = 1

foreach( $file in $files ) {

    $newFilename = [char]$char + ( "{0:00}" -f $counter)

    Move-Item -Path ($file.FullName) -Destination ($newFilename + $file.Extension) | Out-Null

    $char++
    $counter++

    if( $counter -gt 20 ) {
        $char++
        $counter = 1
    }
}

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