how to rename files with a if

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of jim shupert jim shupert 4 months, 3 weeks ago.

  • Author
    Posts
  • #61314
    Profile photo of jim shupert
    jim shupert
    Participant

    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

  • #61315
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    
  • #61324
    Profile photo of random commandline
    random commandline
    Participant
    Get-ChildItem \\path\to\files -Filter *.mxf -Exclude *.j2k.mxf -Recurse |
    Rename-Item -NewName {$_.name -replace '\.mxf','.j2k.mxf'}
    
  • #61332
    Profile photo of jim shupert
    jim shupert
    Participant

    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

  • #61414
    Profile photo of jim shupert
    jim shupert
    Participant

    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

  • #61428
    Profile photo of jim shupert
    jim shupert
    Participant

    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

You must be logged in to reply to this topic.