Renaming Files with Mathematica

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











up vote
6
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

















  • 1




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















up vote
6
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

















  • 1




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













up vote
6
down vote

favorite









up vote
6
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 yesterday









james

845418




845418







  • 1




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













  • 1




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








1




1




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





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











3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










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






















  • Very nice ! Thanks a lot!
    – james
    22 hours ago

















up vote
3
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













    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
      6
      down vote



      accepted










      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






















      • Very nice ! Thanks a lot!
        – james
        22 hours ago














      up vote
      6
      down vote



      accepted










      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






















      • Very nice ! Thanks a lot!
        – james
        22 hours ago












      up vote
      6
      down vote



      accepted







      up vote
      6
      down vote



      accepted






      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 yesterday

























      answered yesterday









      Vitaliy Kaurov

      56.3k6157275




      56.3k6157275











      • Very nice ! Thanks a lot!
        – james
        22 hours ago
















      • Very nice ! Thanks a lot!
        – james
        22 hours ago















      Very nice ! Thanks a lot!
      – james
      22 hours ago




      Very nice ! Thanks a lot!
      – james
      22 hours ago










      up vote
      3
      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
        3
        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
          3
          down vote










          up vote
          3
          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 yesterday









          henry

          1,250423




          1,250423




















              up vote
              1
              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
                1
                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
                  1
                  down vote










                  up vote
                  1
                  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 yesterday









                  Henrik Schumacher

                  41.3k259124




                  41.3k259124



























                       

                      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













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?