Forum Discussion

derekman1's avatar
derekman1
Copper Contributor
Sep 13, 2024

Change file extensions

I have a folder with thousands of files all of which have different extensions.  I need to change all the extensions to "jpg" and looked at Rename and replace, but since every extension is different I cannot provide all for the replace function.  Sounds weird but the files were produced by a vendors application and all extensions are numeric, "001", "002", "003" and on.  

Any help would be appreciated.

  • derekman1 

     

    Hi, David.

     

    I'm unclear on whether you're working in a single directory or a directory tree. I'm also unclear on whether or not there are other files within these directory(ies) that you do not want renamed.

     

    For the sake of simplicity, I'm going to assume that:

     

    1. All files exist within the same directory (i.e. not child directories);
    2. All files in that directory are going to have their extension renamed.

     

    If these assumptions are incorrect, please provide more detail on the scenario.

     

    In this assumed scenario, a single, simple command can rename everything to have the extension of .jpg:

     

    (Get-ChildItem -File -Path "C:\Data\Temp\*") | ForEach-Object { Rename-Item -Path $_.FullName -NewName "$($_.BaseName).jpg" };

     

    Here's an illustrated example, where I first show a few properties of a file I intend to rename (I'm not looking to rename every file this example as I have items I don't want renamed), then rename the shown file before finally running another check on the file to illustrate how the properties have change when compared to the first command.

     

     

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    derekman1 

     

    Hi, David.

     

    I'm unclear on whether you're working in a single directory or a directory tree. I'm also unclear on whether or not there are other files within these directory(ies) that you do not want renamed.

     

    For the sake of simplicity, I'm going to assume that:

     

    1. All files exist within the same directory (i.e. not child directories);
    2. All files in that directory are going to have their extension renamed.

     

    If these assumptions are incorrect, please provide more detail on the scenario.

     

    In this assumed scenario, a single, simple command can rename everything to have the extension of .jpg:

     

    (Get-ChildItem -File -Path "C:\Data\Temp\*") | ForEach-Object { Rename-Item -Path $_.FullName -NewName "$($_.BaseName).jpg" };

     

    Here's an illustrated example, where I first show a few properties of a file I intend to rename (I'm not looking to rename every file this example as I have items I don't want renamed), then rename the shown file before finally running another check on the file to illustrate how the properties have change when compared to the first command.

     

     

     

    Cheers,

    Lain

    • derekman1's avatar
      derekman1
      Copper Contributor
      Sorry for the vagueness. This worked perfectly! Thank you!

Resources