Renaming Files with Mathematica

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have a lot of video and text files. They all have the name: "Number-Suómething"



So for instance:




02-2.2-raKfcgvtavU.mp4




Is it possible to batch rename all the files using Mathematica to only "Number" ?



So for instance:




02.mp4











share|improve this question





















  • I think it possible. Functions like FileNames, FileBaseName and NotebookDirectory might be helpful.
    – Î‘λέξανδρος Ζεγγ
    47 mins ago















up vote
3
down vote

favorite












I have a lot of video and text files. They all have the name: "Number-Suómething"



So for instance:




02-2.2-raKfcgvtavU.mp4




Is it possible to batch rename all the files using Mathematica to only "Number" ?



So for instance:




02.mp4











share|improve this question





















  • I think it possible. Functions like FileNames, FileBaseName and NotebookDirectory might be helpful.
    – Î‘λέξανδρος Ζεγγ
    47 mins ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a lot of video and text files. They all have the name: "Number-Suómething"



So for instance:




02-2.2-raKfcgvtavU.mp4




Is it possible to batch rename all the files using Mathematica to only "Number" ?



So for instance:




02.mp4











share|improve this question













I have a lot of video and text files. They all have the name: "Number-Suómething"



So for instance:




02-2.2-raKfcgvtavU.mp4




Is it possible to batch rename all the files using Mathematica to only "Number" ?



So for instance:




02.mp4








files-and-directories






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 59 mins ago









james

782418




782418











  • I think it possible. Functions like FileNames, FileBaseName and NotebookDirectory might be helpful.
    – Î‘λέξανδρος Ζεγγ
    47 mins ago

















  • I think it possible. Functions like FileNames, FileBaseName and NotebookDirectory might be helpful.
    – Î‘λέξανδρος Ζεγγ
    47 mins ago
















I think it possible. Functions like FileNames, FileBaseName and NotebookDirectory might be helpful.
– Î‘λέξανδρος Ζεγγ
47 mins ago





I think it possible. Functions like FileNames, FileBaseName and NotebookDirectory might be helpful.
– Î‘λέξανδρος Ζεγγ
47 mins ago











3 Answers
3






active

oldest

votes

















up vote
1
down vote













Simple
Just set the current working directory to the folder which contains the files:



SetDirectory["C\…"]


And then execute this command:



RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@ 
FileNames["*.mp4"]


For .txt just do the same but replace the ".mp4" with ".txt"



Copy-Paste Code:



 SetDirectory["C\…"]
RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@
FileNames["*.mp4"]
RenameFile[#, StringTake[ToString[#] , 2] <> ".txt"] & /@
FileNames["*.txt"]





share|improve this answer



























    up vote
    1
    down vote













    The following method does not require a specific fixed number of digits in the start of file name and does not depend on specific fixed non-numeric separator such as "-".



    rename[name_]:=
    RenameFile[
    name,
    StringCases[name,(x:DigitCharacter..)~~___~~".mp4":>x<>".mp4"][[1]]
    ]


    1) Back up your files, - better make a new working copy of your directory.



    2) Point Wolfram Language to that directory:



    SetDirectory["path to your directory"]


    3) Rename files:



    rename /@ FileNames


    You can also use CopyFile instead of RenameFile in order to keep the original file and place the renamed file in a directory of your choice. Because CopyFile can copy and rename simultaneously.






    share|improve this answer





























      up vote
      0
      down vote













      You can try to map the following function over a list of full file names (with their full path). Use at your own risk.



      rename = file [Function] RenameFile[
      file,
      FileNameJoin[

      DirectoryName[file],
      StringSplit[FileBaseName[file], "-"][[1]] <> "." <> FileExtension[file]

      ]
      ]


      For example, you can find all files with extension "*.mp4" in a given path path with



      FileNames[FileNameJoin[path, "*.mp4"]]





      share|improve this answer




















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183285%2frenaming-files-with-mathematica%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        Simple
        Just set the current working directory to the folder which contains the files:



        SetDirectory["C\…"]


        And then execute this command:



        RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@ 
        FileNames["*.mp4"]


        For .txt just do the same but replace the ".mp4" with ".txt"



        Copy-Paste Code:



         SetDirectory["C\…"]
        RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@
        FileNames["*.mp4"]
        RenameFile[#, StringTake[ToString[#] , 2] <> ".txt"] & /@
        FileNames["*.txt"]





        share|improve this answer
























          up vote
          1
          down vote













          Simple
          Just set the current working directory to the folder which contains the files:



          SetDirectory["C\…"]


          And then execute this command:



          RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@ 
          FileNames["*.mp4"]


          For .txt just do the same but replace the ".mp4" with ".txt"



          Copy-Paste Code:



           SetDirectory["C\…"]
          RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@
          FileNames["*.mp4"]
          RenameFile[#, StringTake[ToString[#] , 2] <> ".txt"] & /@
          FileNames["*.txt"]





          share|improve this answer






















            up vote
            1
            down vote










            up vote
            1
            down vote









            Simple
            Just set the current working directory to the folder which contains the files:



            SetDirectory["C\…"]


            And then execute this command:



            RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@ 
            FileNames["*.mp4"]


            For .txt just do the same but replace the ".mp4" with ".txt"



            Copy-Paste Code:



             SetDirectory["C\…"]
            RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@
            FileNames["*.mp4"]
            RenameFile[#, StringTake[ToString[#] , 2] <> ".txt"] & /@
            FileNames["*.txt"]





            share|improve this answer












            Simple
            Just set the current working directory to the folder which contains the files:



            SetDirectory["C\…"]


            And then execute this command:



            RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@ 
            FileNames["*.mp4"]


            For .txt just do the same but replace the ".mp4" with ".txt"



            Copy-Paste Code:



             SetDirectory["C\…"]
            RenameFile[#, StringTake[ToString[#] , 2] <> ".mp4"] & /@
            FileNames["*.mp4"]
            RenameFile[#, StringTake[ToString[#] , 2] <> ".txt"] & /@
            FileNames["*.txt"]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 40 mins ago









            henry

            1,195423




            1,195423




















                up vote
                1
                down vote













                The following method does not require a specific fixed number of digits in the start of file name and does not depend on specific fixed non-numeric separator such as "-".



                rename[name_]:=
                RenameFile[
                name,
                StringCases[name,(x:DigitCharacter..)~~___~~".mp4":>x<>".mp4"][[1]]
                ]


                1) Back up your files, - better make a new working copy of your directory.



                2) Point Wolfram Language to that directory:



                SetDirectory["path to your directory"]


                3) Rename files:



                rename /@ FileNames


                You can also use CopyFile instead of RenameFile in order to keep the original file and place the renamed file in a directory of your choice. Because CopyFile can copy and rename simultaneously.






                share|improve this answer


























                  up vote
                  1
                  down vote













                  The following method does not require a specific fixed number of digits in the start of file name and does not depend on specific fixed non-numeric separator such as "-".



                  rename[name_]:=
                  RenameFile[
                  name,
                  StringCases[name,(x:DigitCharacter..)~~___~~".mp4":>x<>".mp4"][[1]]
                  ]


                  1) Back up your files, - better make a new working copy of your directory.



                  2) Point Wolfram Language to that directory:



                  SetDirectory["path to your directory"]


                  3) Rename files:



                  rename /@ FileNames


                  You can also use CopyFile instead of RenameFile in order to keep the original file and place the renamed file in a directory of your choice. Because CopyFile can copy and rename simultaneously.






                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    The following method does not require a specific fixed number of digits in the start of file name and does not depend on specific fixed non-numeric separator such as "-".



                    rename[name_]:=
                    RenameFile[
                    name,
                    StringCases[name,(x:DigitCharacter..)~~___~~".mp4":>x<>".mp4"][[1]]
                    ]


                    1) Back up your files, - better make a new working copy of your directory.



                    2) Point Wolfram Language to that directory:



                    SetDirectory["path to your directory"]


                    3) Rename files:



                    rename /@ FileNames


                    You can also use CopyFile instead of RenameFile in order to keep the original file and place the renamed file in a directory of your choice. Because CopyFile can copy and rename simultaneously.






                    share|improve this answer














                    The following method does not require a specific fixed number of digits in the start of file name and does not depend on specific fixed non-numeric separator such as "-".



                    rename[name_]:=
                    RenameFile[
                    name,
                    StringCases[name,(x:DigitCharacter..)~~___~~".mp4":>x<>".mp4"][[1]]
                    ]


                    1) Back up your files, - better make a new working copy of your directory.



                    2) Point Wolfram Language to that directory:



                    SetDirectory["path to your directory"]


                    3) Rename files:



                    rename /@ FileNames


                    You can also use CopyFile instead of RenameFile in order to keep the original file and place the renamed file in a directory of your choice. Because CopyFile can copy and rename simultaneously.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 8 mins ago

























                    answered 25 mins ago









                    Vitaliy Kaurov

                    56.1k6157275




                    56.1k6157275




















                        up vote
                        0
                        down vote













                        You can try to map the following function over a list of full file names (with their full path). Use at your own risk.



                        rename = file [Function] RenameFile[
                        file,
                        FileNameJoin[

                        DirectoryName[file],
                        StringSplit[FileBaseName[file], "-"][[1]] <> "." <> FileExtension[file]

                        ]
                        ]


                        For example, you can find all files with extension "*.mp4" in a given path path with



                        FileNames[FileNameJoin[path, "*.mp4"]]





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          You can try to map the following function over a list of full file names (with their full path). Use at your own risk.



                          rename = file [Function] RenameFile[
                          file,
                          FileNameJoin[

                          DirectoryName[file],
                          StringSplit[FileBaseName[file], "-"][[1]] <> "." <> FileExtension[file]

                          ]
                          ]


                          For example, you can find all files with extension "*.mp4" in a given path path with



                          FileNames[FileNameJoin[path, "*.mp4"]]





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can try to map the following function over a list of full file names (with their full path). Use at your own risk.



                            rename = file [Function] RenameFile[
                            file,
                            FileNameJoin[

                            DirectoryName[file],
                            StringSplit[FileBaseName[file], "-"][[1]] <> "." <> FileExtension[file]

                            ]
                            ]


                            For example, you can find all files with extension "*.mp4" in a given path path with



                            FileNames[FileNameJoin[path, "*.mp4"]]





                            share|improve this answer












                            You can try to map the following function over a list of full file names (with their full path). Use at your own risk.



                            rename = file [Function] RenameFile[
                            file,
                            FileNameJoin[

                            DirectoryName[file],
                            StringSplit[FileBaseName[file], "-"][[1]] <> "." <> FileExtension[file]

                            ]
                            ]


                            For example, you can find all files with extension "*.mp4" in a given path path with



                            FileNames[FileNameJoin[path, "*.mp4"]]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 46 mins ago









                            Henrik Schumacher

                            41k258124




                            41k258124



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183285%2frenaming-files-with-mathematica%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                White Anglo-Saxon Protestant

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                One-line joke