how to rename files with a if

Friends,

I have many many files named like aaaaa.mxf I wish to have them named aaaaa.j2k.mxf
the aaaa in this example is a big long number that varies in length.

I have limited success with

Dir *.mxf | rename-item -newname { [io.path]::ChangeExtention($_.name, “j2k.mxf”) }

my problem is These files go into a dir and I wish to update it daily
so - with the above solution
monday works fine but on tue the monday load gets aaaa.j2k.j2k.mxf etc etc.

so i need a IF or something that …maybe uses -no_match

basically like
If instring is NOT already j2k.mxf
then
Dir *.mxf | rename-item -newname { [io.path]::ChangeExtention($_.name, “j2k.mxf”) }

I have tried some things … all so far off that I hesitate to mention.

thanks for help in advance.

I am just starting with powershell

thanks again

You don’t necessarily need an IF, you can just exclude files currently named with j2k.mxf. You should validate it’s working as expected with -WhatIf and then you can remove it to actually make changes. Try something like this:

Get-ChildItem *.mxf | 
Where {$_.Name -notlike "*j2k.mxf"} |
Rename-Item -NewName ("{0}.j2k{1}" -f $_.BaseName, $_.Extension) -WhatIf
Get-ChildItem \\path\to\files -Filter *.mxf -Exclude *.j2k.mxf -Recurse |
Rename-Item -NewName {$_.name -replace '\.mxf','.j2k.mxf'}

Thanks Bro — the notion of a -WhatIf is intresting

I am happily working with
Get-ChildItem D:\0000\ -Filter *.mxf -Exclude *.j2k.mxf -Recurse |
Rename-Item -NewName {$_.name -replace ‘.mxf’,‘.j2k.mxf’}

much thanks to you both

it seems that
Get-ChildItem *.mxf |
Where {$.Name -notlike “*j2k.mxf”} |
Rename-Item -NewName (“{0}.j2k{1}” -f $
.BaseName, $_.Extension) -WhatIf

does not work

meaning
a file a.xmf will Not be renamed a.j2k.mxf but rather a.j2k

I have fooled around with it … have not gotten it to work

in D:\0000
i have 2 files a.mxf , b.j2k.mxf
goal : skip the already fixed b.j2k.mxf and Fix a.mxf to a.j2k.mxf

Get-ChildItem D:\0000*.mxf |
Where {$.Name -notlike “*j2k.mxf”} |
Rename-Item -NewName (“.j2k.mxf” -f $
.BaseName, $_.Extension) -WhatIf

at if: Performing the operation “Rename File” on target “Item: D:\0000\a.mxf Destination: D:\0000.j2k.mxf”.

we are missing the basefilename