Is there one line of code for changing all folders and sub folders file extensions using powershell?












0















I used the following code to batch change one extension in one folder at a time.



Dir *.mkv | rename-item -newname { $_.name -replace ".mkv",".vlc" }



What I want is to be able to have one line of code that will




  • change all file extension types to one file extension


  • include files inside sub folders



Any help on this would be greatly appreciated, THANKS










share|improve this question




















  • 3





    See Get-ChildItem options. Use this to replace ‘dir’.

    – user2864740
    Jan 2 at 8:34













  • @user2864740 dir is an alias for Get-ChildItem - it makes sense to use proper names instead of aliases but there won't be any effective difference.

    – colsw
    Jan 2 at 10:02
















0















I used the following code to batch change one extension in one folder at a time.



Dir *.mkv | rename-item -newname { $_.name -replace ".mkv",".vlc" }



What I want is to be able to have one line of code that will




  • change all file extension types to one file extension


  • include files inside sub folders



Any help on this would be greatly appreciated, THANKS










share|improve this question




















  • 3





    See Get-ChildItem options. Use this to replace ‘dir’.

    – user2864740
    Jan 2 at 8:34













  • @user2864740 dir is an alias for Get-ChildItem - it makes sense to use proper names instead of aliases but there won't be any effective difference.

    – colsw
    Jan 2 at 10:02














0












0








0








I used the following code to batch change one extension in one folder at a time.



Dir *.mkv | rename-item -newname { $_.name -replace ".mkv",".vlc" }



What I want is to be able to have one line of code that will




  • change all file extension types to one file extension


  • include files inside sub folders



Any help on this would be greatly appreciated, THANKS










share|improve this question
















I used the following code to batch change one extension in one folder at a time.



Dir *.mkv | rename-item -newname { $_.name -replace ".mkv",".vlc" }



What I want is to be able to have one line of code that will




  • change all file extension types to one file extension


  • include files inside sub folders



Any help on this would be greatly appreciated, THANKS







powershell batch-file file-extension subfolder batch-rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 8:49









Joey Mallone

2,26561933




2,26561933










asked Jan 2 at 8:23









George CastanzaGeorge Castanza

1




1








  • 3





    See Get-ChildItem options. Use this to replace ‘dir’.

    – user2864740
    Jan 2 at 8:34













  • @user2864740 dir is an alias for Get-ChildItem - it makes sense to use proper names instead of aliases but there won't be any effective difference.

    – colsw
    Jan 2 at 10:02














  • 3





    See Get-ChildItem options. Use this to replace ‘dir’.

    – user2864740
    Jan 2 at 8:34













  • @user2864740 dir is an alias for Get-ChildItem - it makes sense to use proper names instead of aliases but there won't be any effective difference.

    – colsw
    Jan 2 at 10:02








3




3





See Get-ChildItem options. Use this to replace ‘dir’.

– user2864740
Jan 2 at 8:34







See Get-ChildItem options. Use this to replace ‘dir’.

– user2864740
Jan 2 at 8:34















@user2864740 dir is an alias for Get-ChildItem - it makes sense to use proper names instead of aliases but there won't be any effective difference.

– colsw
Jan 2 at 10:02





@user2864740 dir is an alias for Get-ChildItem - it makes sense to use proper names instead of aliases but there won't be any effective difference.

– colsw
Jan 2 at 10:02












3 Answers
3






active

oldest

votes


















4














Use -Recurse to include subfolders. Use BaseName which is the filename without extension.
Read the documentation for more info on either



Get-ChildItem *.mkv -Recurse | Rename-Item -NewName {"$($_.BaseName).vlc"}


Edit



Apologies, I missed this part:




change all file extension types to one file extension




To change all file extensions you just need to exclude the *mkv but as covered by LotPings's answer this isn't recommended; it's very easy to change the extension of files you didn't mean to. He covers using the -Include parameter, which is the recommended way as it filters early on.



In the interest of showing another way, you could use the Extension property. This allows you to leverage all of the PowerShell comparison operators - examples below.



Get-ChildItem -Recurse | Where-Object {$_.Extension -eq ".mkv"}
Get-ChildItem -Recurse | Where-Object {$_.Extension -in @(".mkv",".avi")}
Get-ChildItem -Recurse | Where-Object {$_.Extension -ne ".vlc"}





share|improve this answer

































    1














    as you also tagged it batch-file:



    for /r %%a in (*.mkv) do @ECHO ren "%%a" "%%~na.vlc"


    (this is batch-file syntax. If you want to use it directly on command line)



    for /r %a in (*.mkv) do @ECHO ren "%a" "%~na.vlc"


    Remove @ECHO, if the output fits your needs.






    share|improve this answer































      1














      I wouldn't change ALL extensions, just use -Include and present a list



      Get-ChildItem -Path X:starthere -Recurse -Include ('*.mkv','*.xyz')| Rename-Item -NewName {$_.BaseName+'.vlc'} -WhatIf


      If the output looks OK, remove the trailing -WhatIf






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003220%2fis-there-one-line-of-code-for-changing-all-folders-and-sub-folders-file-extensio%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Use -Recurse to include subfolders. Use BaseName which is the filename without extension.
        Read the documentation for more info on either



        Get-ChildItem *.mkv -Recurse | Rename-Item -NewName {"$($_.BaseName).vlc"}


        Edit



        Apologies, I missed this part:




        change all file extension types to one file extension




        To change all file extensions you just need to exclude the *mkv but as covered by LotPings's answer this isn't recommended; it's very easy to change the extension of files you didn't mean to. He covers using the -Include parameter, which is the recommended way as it filters early on.



        In the interest of showing another way, you could use the Extension property. This allows you to leverage all of the PowerShell comparison operators - examples below.



        Get-ChildItem -Recurse | Where-Object {$_.Extension -eq ".mkv"}
        Get-ChildItem -Recurse | Where-Object {$_.Extension -in @(".mkv",".avi")}
        Get-ChildItem -Recurse | Where-Object {$_.Extension -ne ".vlc"}





        share|improve this answer






























          4














          Use -Recurse to include subfolders. Use BaseName which is the filename without extension.
          Read the documentation for more info on either



          Get-ChildItem *.mkv -Recurse | Rename-Item -NewName {"$($_.BaseName).vlc"}


          Edit



          Apologies, I missed this part:




          change all file extension types to one file extension




          To change all file extensions you just need to exclude the *mkv but as covered by LotPings's answer this isn't recommended; it's very easy to change the extension of files you didn't mean to. He covers using the -Include parameter, which is the recommended way as it filters early on.



          In the interest of showing another way, you could use the Extension property. This allows you to leverage all of the PowerShell comparison operators - examples below.



          Get-ChildItem -Recurse | Where-Object {$_.Extension -eq ".mkv"}
          Get-ChildItem -Recurse | Where-Object {$_.Extension -in @(".mkv",".avi")}
          Get-ChildItem -Recurse | Where-Object {$_.Extension -ne ".vlc"}





          share|improve this answer




























            4












            4








            4







            Use -Recurse to include subfolders. Use BaseName which is the filename without extension.
            Read the documentation for more info on either



            Get-ChildItem *.mkv -Recurse | Rename-Item -NewName {"$($_.BaseName).vlc"}


            Edit



            Apologies, I missed this part:




            change all file extension types to one file extension




            To change all file extensions you just need to exclude the *mkv but as covered by LotPings's answer this isn't recommended; it's very easy to change the extension of files you didn't mean to. He covers using the -Include parameter, which is the recommended way as it filters early on.



            In the interest of showing another way, you could use the Extension property. This allows you to leverage all of the PowerShell comparison operators - examples below.



            Get-ChildItem -Recurse | Where-Object {$_.Extension -eq ".mkv"}
            Get-ChildItem -Recurse | Where-Object {$_.Extension -in @(".mkv",".avi")}
            Get-ChildItem -Recurse | Where-Object {$_.Extension -ne ".vlc"}





            share|improve this answer















            Use -Recurse to include subfolders. Use BaseName which is the filename without extension.
            Read the documentation for more info on either



            Get-ChildItem *.mkv -Recurse | Rename-Item -NewName {"$($_.BaseName).vlc"}


            Edit



            Apologies, I missed this part:




            change all file extension types to one file extension




            To change all file extensions you just need to exclude the *mkv but as covered by LotPings's answer this isn't recommended; it's very easy to change the extension of files you didn't mean to. He covers using the -Include parameter, which is the recommended way as it filters early on.



            In the interest of showing another way, you could use the Extension property. This allows you to leverage all of the PowerShell comparison operators - examples below.



            Get-ChildItem -Recurse | Where-Object {$_.Extension -eq ".mkv"}
            Get-ChildItem -Recurse | Where-Object {$_.Extension -in @(".mkv",".avi")}
            Get-ChildItem -Recurse | Where-Object {$_.Extension -ne ".vlc"}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 2 at 15:37

























            answered Jan 2 at 9:13









            gms0ulmangms0ulman

            7,68521128




            7,68521128

























                1














                as you also tagged it batch-file:



                for /r %%a in (*.mkv) do @ECHO ren "%%a" "%%~na.vlc"


                (this is batch-file syntax. If you want to use it directly on command line)



                for /r %a in (*.mkv) do @ECHO ren "%a" "%~na.vlc"


                Remove @ECHO, if the output fits your needs.






                share|improve this answer




























                  1














                  as you also tagged it batch-file:



                  for /r %%a in (*.mkv) do @ECHO ren "%%a" "%%~na.vlc"


                  (this is batch-file syntax. If you want to use it directly on command line)



                  for /r %a in (*.mkv) do @ECHO ren "%a" "%~na.vlc"


                  Remove @ECHO, if the output fits your needs.






                  share|improve this answer


























                    1












                    1








                    1







                    as you also tagged it batch-file:



                    for /r %%a in (*.mkv) do @ECHO ren "%%a" "%%~na.vlc"


                    (this is batch-file syntax. If you want to use it directly on command line)



                    for /r %a in (*.mkv) do @ECHO ren "%a" "%~na.vlc"


                    Remove @ECHO, if the output fits your needs.






                    share|improve this answer













                    as you also tagged it batch-file:



                    for /r %%a in (*.mkv) do @ECHO ren "%%a" "%%~na.vlc"


                    (this is batch-file syntax. If you want to use it directly on command line)



                    for /r %a in (*.mkv) do @ECHO ren "%a" "%~na.vlc"


                    Remove @ECHO, if the output fits your needs.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 8:59









                    StephanStephan

                    35.9k43457




                    35.9k43457























                        1














                        I wouldn't change ALL extensions, just use -Include and present a list



                        Get-ChildItem -Path X:starthere -Recurse -Include ('*.mkv','*.xyz')| Rename-Item -NewName {$_.BaseName+'.vlc'} -WhatIf


                        If the output looks OK, remove the trailing -WhatIf






                        share|improve this answer




























                          1














                          I wouldn't change ALL extensions, just use -Include and present a list



                          Get-ChildItem -Path X:starthere -Recurse -Include ('*.mkv','*.xyz')| Rename-Item -NewName {$_.BaseName+'.vlc'} -WhatIf


                          If the output looks OK, remove the trailing -WhatIf






                          share|improve this answer


























                            1












                            1








                            1







                            I wouldn't change ALL extensions, just use -Include and present a list



                            Get-ChildItem -Path X:starthere -Recurse -Include ('*.mkv','*.xyz')| Rename-Item -NewName {$_.BaseName+'.vlc'} -WhatIf


                            If the output looks OK, remove the trailing -WhatIf






                            share|improve this answer













                            I wouldn't change ALL extensions, just use -Include and present a list



                            Get-ChildItem -Path X:starthere -Recurse -Include ('*.mkv','*.xyz')| Rename-Item -NewName {$_.BaseName+'.vlc'} -WhatIf


                            If the output looks OK, remove the trailing -WhatIf







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 12:19









                            LotPingsLotPings

                            19.8k61633




                            19.8k61633






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003220%2fis-there-one-line-of-code-for-changing-all-folders-and-sub-folders-file-extensio%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith