Using Windows REN to insert text into a filename, maintaining the rest of the name?












-1















I'm currently trying to use the REN command to add text in the middle of a filename, while maintaining the rest of the filename.



Example:



testfile_2018-11-14-06-06-23.pdf -> testfile_ABCD_2018-11-14-06-06-23.pdf



The last six digits are subject to change, so I need to represent them with wildcards.



Currently, I have the following:



REN testfile_2018-11-14*.pdf testfile_ABCD_2018-11-14*.pdf



Result is:
testfile_ABCD_2018-11-146-23.pdf



The last six digits are not being maintained, and I cannot figure out why.










share|improve this question























  • PowerShell with string splitting or regex would be ideally suited to address this. Any reason you're not using it?

    – thepip3r
    Nov 14 '18 at 17:51








  • 2





    As @Squashman says in his answer, this cannot be done with REN alone. See How does the Windows RENAME command interpret wildcards? at SuperUser for an explanation of exactly what can and cannot be done.

    – dbenham
    Nov 14 '18 at 18:02











  • @dbenham, how about a JREN.bat answer. I know it is kind of overkill for this task but would be a useful answer.

    – Squashman
    Nov 14 '18 at 18:55
















-1















I'm currently trying to use the REN command to add text in the middle of a filename, while maintaining the rest of the filename.



Example:



testfile_2018-11-14-06-06-23.pdf -> testfile_ABCD_2018-11-14-06-06-23.pdf



The last six digits are subject to change, so I need to represent them with wildcards.



Currently, I have the following:



REN testfile_2018-11-14*.pdf testfile_ABCD_2018-11-14*.pdf



Result is:
testfile_ABCD_2018-11-146-23.pdf



The last six digits are not being maintained, and I cannot figure out why.










share|improve this question























  • PowerShell with string splitting or regex would be ideally suited to address this. Any reason you're not using it?

    – thepip3r
    Nov 14 '18 at 17:51








  • 2





    As @Squashman says in his answer, this cannot be done with REN alone. See How does the Windows RENAME command interpret wildcards? at SuperUser for an explanation of exactly what can and cannot be done.

    – dbenham
    Nov 14 '18 at 18:02











  • @dbenham, how about a JREN.bat answer. I know it is kind of overkill for this task but would be a useful answer.

    – Squashman
    Nov 14 '18 at 18:55














-1












-1








-1








I'm currently trying to use the REN command to add text in the middle of a filename, while maintaining the rest of the filename.



Example:



testfile_2018-11-14-06-06-23.pdf -> testfile_ABCD_2018-11-14-06-06-23.pdf



The last six digits are subject to change, so I need to represent them with wildcards.



Currently, I have the following:



REN testfile_2018-11-14*.pdf testfile_ABCD_2018-11-14*.pdf



Result is:
testfile_ABCD_2018-11-146-23.pdf



The last six digits are not being maintained, and I cannot figure out why.










share|improve this question














I'm currently trying to use the REN command to add text in the middle of a filename, while maintaining the rest of the filename.



Example:



testfile_2018-11-14-06-06-23.pdf -> testfile_ABCD_2018-11-14-06-06-23.pdf



The last six digits are subject to change, so I need to represent them with wildcards.



Currently, I have the following:



REN testfile_2018-11-14*.pdf testfile_ABCD_2018-11-14*.pdf



Result is:
testfile_ABCD_2018-11-146-23.pdf



The last six digits are not being maintained, and I cannot figure out why.







windows batch-file cmd rename






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 17:29









BobazonskiBobazonski

7331335




7331335













  • PowerShell with string splitting or regex would be ideally suited to address this. Any reason you're not using it?

    – thepip3r
    Nov 14 '18 at 17:51








  • 2





    As @Squashman says in his answer, this cannot be done with REN alone. See How does the Windows RENAME command interpret wildcards? at SuperUser for an explanation of exactly what can and cannot be done.

    – dbenham
    Nov 14 '18 at 18:02











  • @dbenham, how about a JREN.bat answer. I know it is kind of overkill for this task but would be a useful answer.

    – Squashman
    Nov 14 '18 at 18:55



















  • PowerShell with string splitting or regex would be ideally suited to address this. Any reason you're not using it?

    – thepip3r
    Nov 14 '18 at 17:51








  • 2





    As @Squashman says in his answer, this cannot be done with REN alone. See How does the Windows RENAME command interpret wildcards? at SuperUser for an explanation of exactly what can and cannot be done.

    – dbenham
    Nov 14 '18 at 18:02











  • @dbenham, how about a JREN.bat answer. I know it is kind of overkill for this task but would be a useful answer.

    – Squashman
    Nov 14 '18 at 18:55

















PowerShell with string splitting or regex would be ideally suited to address this. Any reason you're not using it?

– thepip3r
Nov 14 '18 at 17:51







PowerShell with string splitting or regex would be ideally suited to address this. Any reason you're not using it?

– thepip3r
Nov 14 '18 at 17:51






2




2





As @Squashman says in his answer, this cannot be done with REN alone. See How does the Windows RENAME command interpret wildcards? at SuperUser for an explanation of exactly what can and cannot be done.

– dbenham
Nov 14 '18 at 18:02





As @Squashman says in his answer, this cannot be done with REN alone. See How does the Windows RENAME command interpret wildcards? at SuperUser for an explanation of exactly what can and cannot be done.

– dbenham
Nov 14 '18 at 18:02













@dbenham, how about a JREN.bat answer. I know it is kind of overkill for this task but would be a useful answer.

– Squashman
Nov 14 '18 at 18:55





@dbenham, how about a JREN.bat answer. I know it is kind of overkill for this task but would be a useful answer.

– Squashman
Nov 14 '18 at 18:55












2 Answers
2






active

oldest

votes


















3














Pretty sure it cannot be done with a simple REN command. You can however use the power of the FOR /F command to manipulate the file name.



From the command prompt you can run this.



for /f "tokens=1* delims=_" %G IN ('dir /a-d /b "testfile_2018-11-14*.pdf"') do ren "%G_%H" "%G_ABCD_%H"


This finds the file and then splits the file name by the underscore. It then renames it with the extra string in the new file name.



If you are going to run this in a batch file you must double the percent symbols.






share|improve this answer































    2














    If we're offering alternative solutions to REN, here are a few ways in PowerShell:



    String Splitting:



    ## Get a System.IO.FileInfo object to the file
    $f = Get-Item path-to-the-testfile

    ## Split up the name by the underscore so the zeroth entry is 'testfile' and the first entry is the remaining name
    $s = $f.Name.Split("_")

    ## Use String tokenization to recombine the different parts in the desired order during the rename
    Rename-Item $f.FullName ("{0}{1}_{2}_{3}" -f $f.DirectoryName, $s[0], 'ABCD', $s[1])


    String Replace:



    ## Get a System.IO.FileInfo object to the file
    $f = Get-Item path-to-the-testfile

    ## Use string replace to fix the name during the rename operation
    Rename-Item $f.FullName ($f.FullName.Replace('testfile_', 'testfile_ABCD_'))


    Using regex is possible but is probably overly complicated if you're not familiar with the above methods.






    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%2f53305766%2fusing-windows-ren-to-insert-text-into-a-filename-maintaining-the-rest-of-the-na%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Pretty sure it cannot be done with a simple REN command. You can however use the power of the FOR /F command to manipulate the file name.



      From the command prompt you can run this.



      for /f "tokens=1* delims=_" %G IN ('dir /a-d /b "testfile_2018-11-14*.pdf"') do ren "%G_%H" "%G_ABCD_%H"


      This finds the file and then splits the file name by the underscore. It then renames it with the extra string in the new file name.



      If you are going to run this in a batch file you must double the percent symbols.






      share|improve this answer




























        3














        Pretty sure it cannot be done with a simple REN command. You can however use the power of the FOR /F command to manipulate the file name.



        From the command prompt you can run this.



        for /f "tokens=1* delims=_" %G IN ('dir /a-d /b "testfile_2018-11-14*.pdf"') do ren "%G_%H" "%G_ABCD_%H"


        This finds the file and then splits the file name by the underscore. It then renames it with the extra string in the new file name.



        If you are going to run this in a batch file you must double the percent symbols.






        share|improve this answer


























          3












          3








          3







          Pretty sure it cannot be done with a simple REN command. You can however use the power of the FOR /F command to manipulate the file name.



          From the command prompt you can run this.



          for /f "tokens=1* delims=_" %G IN ('dir /a-d /b "testfile_2018-11-14*.pdf"') do ren "%G_%H" "%G_ABCD_%H"


          This finds the file and then splits the file name by the underscore. It then renames it with the extra string in the new file name.



          If you are going to run this in a batch file you must double the percent symbols.






          share|improve this answer













          Pretty sure it cannot be done with a simple REN command. You can however use the power of the FOR /F command to manipulate the file name.



          From the command prompt you can run this.



          for /f "tokens=1* delims=_" %G IN ('dir /a-d /b "testfile_2018-11-14*.pdf"') do ren "%G_%H" "%G_ABCD_%H"


          This finds the file and then splits the file name by the underscore. It then renames it with the extra string in the new file name.



          If you are going to run this in a batch file you must double the percent symbols.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 17:40









          SquashmanSquashman

          8,87931933




          8,87931933

























              2














              If we're offering alternative solutions to REN, here are a few ways in PowerShell:



              String Splitting:



              ## Get a System.IO.FileInfo object to the file
              $f = Get-Item path-to-the-testfile

              ## Split up the name by the underscore so the zeroth entry is 'testfile' and the first entry is the remaining name
              $s = $f.Name.Split("_")

              ## Use String tokenization to recombine the different parts in the desired order during the rename
              Rename-Item $f.FullName ("{0}{1}_{2}_{3}" -f $f.DirectoryName, $s[0], 'ABCD', $s[1])


              String Replace:



              ## Get a System.IO.FileInfo object to the file
              $f = Get-Item path-to-the-testfile

              ## Use string replace to fix the name during the rename operation
              Rename-Item $f.FullName ($f.FullName.Replace('testfile_', 'testfile_ABCD_'))


              Using regex is possible but is probably overly complicated if you're not familiar with the above methods.






              share|improve this answer






























                2














                If we're offering alternative solutions to REN, here are a few ways in PowerShell:



                String Splitting:



                ## Get a System.IO.FileInfo object to the file
                $f = Get-Item path-to-the-testfile

                ## Split up the name by the underscore so the zeroth entry is 'testfile' and the first entry is the remaining name
                $s = $f.Name.Split("_")

                ## Use String tokenization to recombine the different parts in the desired order during the rename
                Rename-Item $f.FullName ("{0}{1}_{2}_{3}" -f $f.DirectoryName, $s[0], 'ABCD', $s[1])


                String Replace:



                ## Get a System.IO.FileInfo object to the file
                $f = Get-Item path-to-the-testfile

                ## Use string replace to fix the name during the rename operation
                Rename-Item $f.FullName ($f.FullName.Replace('testfile_', 'testfile_ABCD_'))


                Using regex is possible but is probably overly complicated if you're not familiar with the above methods.






                share|improve this answer




























                  2












                  2








                  2







                  If we're offering alternative solutions to REN, here are a few ways in PowerShell:



                  String Splitting:



                  ## Get a System.IO.FileInfo object to the file
                  $f = Get-Item path-to-the-testfile

                  ## Split up the name by the underscore so the zeroth entry is 'testfile' and the first entry is the remaining name
                  $s = $f.Name.Split("_")

                  ## Use String tokenization to recombine the different parts in the desired order during the rename
                  Rename-Item $f.FullName ("{0}{1}_{2}_{3}" -f $f.DirectoryName, $s[0], 'ABCD', $s[1])


                  String Replace:



                  ## Get a System.IO.FileInfo object to the file
                  $f = Get-Item path-to-the-testfile

                  ## Use string replace to fix the name during the rename operation
                  Rename-Item $f.FullName ($f.FullName.Replace('testfile_', 'testfile_ABCD_'))


                  Using regex is possible but is probably overly complicated if you're not familiar with the above methods.






                  share|improve this answer















                  If we're offering alternative solutions to REN, here are a few ways in PowerShell:



                  String Splitting:



                  ## Get a System.IO.FileInfo object to the file
                  $f = Get-Item path-to-the-testfile

                  ## Split up the name by the underscore so the zeroth entry is 'testfile' and the first entry is the remaining name
                  $s = $f.Name.Split("_")

                  ## Use String tokenization to recombine the different parts in the desired order during the rename
                  Rename-Item $f.FullName ("{0}{1}_{2}_{3}" -f $f.DirectoryName, $s[0], 'ABCD', $s[1])


                  String Replace:



                  ## Get a System.IO.FileInfo object to the file
                  $f = Get-Item path-to-the-testfile

                  ## Use string replace to fix the name during the rename operation
                  Rename-Item $f.FullName ($f.FullName.Replace('testfile_', 'testfile_ABCD_'))


                  Using regex is possible but is probably overly complicated if you're not familiar with the above methods.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 14 '18 at 18:53

























                  answered Nov 14 '18 at 18:16









                  thepip3rthepip3r

                  1,49042328




                  1,49042328






























                      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%2f53305766%2fusing-windows-ren-to-insert-text-into-a-filename-maintaining-the-rest-of-the-na%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python