Majko Bezeg
Majko Bezeg

Reputation: 23

Powershell: rename-item increment if exist (multiple extensions)

I have a folder with files having multiple extensions and I need to rename them to "thisname".
Renaming should happen like the following

test.txt   -> thisname.txt  
test1.txt  -> thisname1.txt  
bascic.vbs -> thisname.vbs  
basic1.vbs -> thisname1.vbs

The following is what I have tried so far:

Get-ChildItem -Path $subfolder_path\*.* -exclude *.jpg, *.pdf |
    rename-item -newname { -join($jxl) + $_.extension }

This part of code rename only one one file i need to make increment on all duplicity extensions.

Upvotes: 2

Views: 1719

Answers (2)

Majko Bezeg
Majko Bezeg

Reputation: 23

Need to set location first.

Set-Location -Path <path to files>

After this everything goes right.

Upvotes: 0

DAXaholic
DAXaholic

Reputation: 35338

I am not 100% sure if I fully understood your needs but give this a try

Get-ChildItem -Path $subfolder_path\*.* -Exclude *.jpg, *.pdf |
    Rename-Item -NewName {
        $newName = "thisname" + $_.Extension
        for($i = 0; Test-Path $newName; ++$i) {
            $newName = "thisname" + $i + $_.Extension
        }
        $newName
    }

If this does not help you please share some more information about the desired behaviour

Upvotes: 2

Related Questions