Replace last _ when renaming files












-1














I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
The file structure is like this.



>upperFolder
>p1
>學生_二反田●雄.jpg
>學生_二反田●雄_.jpg
>學生_半田●男.jpg
>學生_半田●男_.jpg
>學生_潘●.jpg
>學生_潘●_.jpg
>...
>...
>p7
>學生_石井節●.jpg
>學生_石井節●_.jpg
>學生_王裕●.jpg
>學生_王裕●_.jpg
>學生_●垣 勇.jpg
>學生_●垣 勇_.jpg



delete p1/學生_二反田●雄.jpg



rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg




The final result looks as below.
p1_ is to prevent repeat filename and - seperate student name.



>upperFolder
>p1_學生-二反田●雄.jpg
>p1_學生-半田●男.jpg
>p1_學生-潘●.jpg
>...
>p7_學生-石井節●.jpg
>p7_學生-王裕●.jpg
>p7_學生-●垣 勇.jpg


How can I make this in bat or php?



I have tried



mkdir tmp

for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
pushd "%%D"
for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
popd
)

move *_.jpg tmp
del *.jpg
cd tmp
move *.jpg .. 2>NUL


but can't get rid of the last '_'










share|improve this question





























    -1














    I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
    The file structure is like this.



    >upperFolder
    >p1
    >學生_二反田●雄.jpg
    >學生_二反田●雄_.jpg
    >學生_半田●男.jpg
    >學生_半田●男_.jpg
    >學生_潘●.jpg
    >學生_潘●_.jpg
    >...
    >...
    >p7
    >學生_石井節●.jpg
    >學生_石井節●_.jpg
    >學生_王裕●.jpg
    >學生_王裕●_.jpg
    >學生_●垣 勇.jpg
    >學生_●垣 勇_.jpg



    delete p1/學生_二反田●雄.jpg



    rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg




    The final result looks as below.
    p1_ is to prevent repeat filename and - seperate student name.



    >upperFolder
    >p1_學生-二反田●雄.jpg
    >p1_學生-半田●男.jpg
    >p1_學生-潘●.jpg
    >...
    >p7_學生-石井節●.jpg
    >p7_學生-王裕●.jpg
    >p7_學生-●垣 勇.jpg


    How can I make this in bat or php?



    I have tried



    mkdir tmp

    for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
    pushd "%%D"
    for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
    popd
    )

    move *_.jpg tmp
    del *.jpg
    cd tmp
    move *.jpg .. 2>NUL


    but can't get rid of the last '_'










    share|improve this question



























      -1












      -1








      -1







      I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
      The file structure is like this.



      >upperFolder
      >p1
      >學生_二反田●雄.jpg
      >學生_二反田●雄_.jpg
      >學生_半田●男.jpg
      >學生_半田●男_.jpg
      >學生_潘●.jpg
      >學生_潘●_.jpg
      >...
      >...
      >p7
      >學生_石井節●.jpg
      >學生_石井節●_.jpg
      >學生_王裕●.jpg
      >學生_王裕●_.jpg
      >學生_●垣 勇.jpg
      >學生_●垣 勇_.jpg



      delete p1/學生_二反田●雄.jpg



      rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg




      The final result looks as below.
      p1_ is to prevent repeat filename and - seperate student name.



      >upperFolder
      >p1_學生-二反田●雄.jpg
      >p1_學生-半田●男.jpg
      >p1_學生-潘●.jpg
      >...
      >p7_學生-石井節●.jpg
      >p7_學生-王裕●.jpg
      >p7_學生-●垣 勇.jpg


      How can I make this in bat or php?



      I have tried



      mkdir tmp

      for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
      pushd "%%D"
      for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
      popd
      )

      move *_.jpg tmp
      del *.jpg
      cd tmp
      move *.jpg .. 2>NUL


      but can't get rid of the last '_'










      share|improve this question















      I want to delete all 「name.jpg」 and rename 「name_.jpg」 to 「name.jpg」 in windows.
      The file structure is like this.



      >upperFolder
      >p1
      >學生_二反田●雄.jpg
      >學生_二反田●雄_.jpg
      >學生_半田●男.jpg
      >學生_半田●男_.jpg
      >學生_潘●.jpg
      >學生_潘●_.jpg
      >...
      >...
      >p7
      >學生_石井節●.jpg
      >學生_石井節●_.jpg
      >學生_王裕●.jpg
      >學生_王裕●_.jpg
      >學生_●垣 勇.jpg
      >學生_●垣 勇_.jpg



      delete p1/學生_二反田●雄.jpg



      rename p1/學生_二反田●雄_.jpg p1_學生-二反田●雄.jpg




      The final result looks as below.
      p1_ is to prevent repeat filename and - seperate student name.



      >upperFolder
      >p1_學生-二反田●雄.jpg
      >p1_學生-半田●男.jpg
      >p1_學生-潘●.jpg
      >...
      >p7_學生-石井節●.jpg
      >p7_學生-王裕●.jpg
      >p7_學生-●垣 勇.jpg


      How can I make this in bat or php?



      I have tried



      mkdir tmp

      for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
      pushd "%%D"
      for %%F in (*) do move "%%F" "..%%~nxD_%%F" >nul
      popd
      )

      move *_.jpg tmp
      del *.jpg
      cd tmp
      move *.jpg .. 2>NUL


      but can't get rid of the last '_'







      batch-file






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 3:46

























      asked Nov 21 at 3:25









      AllenBooTung

      9212




      9212
























          2 Answers
          2






          active

          oldest

          votes


















          3














          There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:



          Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.



          :: /r stands for recursive and ~n stands for (file)name
          for /r %g in (*_.jpg) do ren %g "%~ng.jphg"


          Then you can delete the files whose names do not end with _:



          del /s *.jpg


          And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:



          :: so that the variable expansion inside for's works as expected
          setlocal EnableDelayedExpansion
          :: loop through all the directories and store the names in g
          for /d %g in (*) do (
          for %h in ("%g*_.jphg") do (
          set "newname=%~nh"
          :: remove trailing _
          set "newname=!newname:~0,-1!"
          :: replace ALL _ with -, assuming the filename only has one inner _
          set "newname=!newname:_=-!"
          :: prepend directory name and restore extension
          set "newname=%g_!newname!.jpg"
          :: rename and move to current directory
          move %h ".!newname!"
          )
          )





          share|improve this answer





















          • The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
            – AllenBooTung
            Nov 21 at 7:05



















          2














          Something like this should work for you:



          for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"


          Here we:




          • search all files of type jpg


            • delete those found

            • and then rename files with same name ending with _ into names of original files




          See for /? command or this for more information about %%~na and similar.



          Please note that unless your system OEM codepage supports kanji this will not work. You can get around this problem by switching to UTF-8 encoding by issuing command CHCP 65001 and saving batch file as UTF-8 (no BOM). See this for more information.






          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%2f53404835%2freplace-last-when-renaming-files%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














            There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:



            Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.



            :: /r stands for recursive and ~n stands for (file)name
            for /r %g in (*_.jpg) do ren %g "%~ng.jphg"


            Then you can delete the files whose names do not end with _:



            del /s *.jpg


            And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:



            :: so that the variable expansion inside for's works as expected
            setlocal EnableDelayedExpansion
            :: loop through all the directories and store the names in g
            for /d %g in (*) do (
            for %h in ("%g*_.jphg") do (
            set "newname=%~nh"
            :: remove trailing _
            set "newname=!newname:~0,-1!"
            :: replace ALL _ with -, assuming the filename only has one inner _
            set "newname=!newname:_=-!"
            :: prepend directory name and restore extension
            set "newname=%g_!newname!.jpg"
            :: rename and move to current directory
            move %h ".!newname!"
            )
            )





            share|improve this answer





















            • The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
              – AllenBooTung
              Nov 21 at 7:05
















            3














            There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:



            Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.



            :: /r stands for recursive and ~n stands for (file)name
            for /r %g in (*_.jpg) do ren %g "%~ng.jphg"


            Then you can delete the files whose names do not end with _:



            del /s *.jpg


            And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:



            :: so that the variable expansion inside for's works as expected
            setlocal EnableDelayedExpansion
            :: loop through all the directories and store the names in g
            for /d %g in (*) do (
            for %h in ("%g*_.jphg") do (
            set "newname=%~nh"
            :: remove trailing _
            set "newname=!newname:~0,-1!"
            :: replace ALL _ with -, assuming the filename only has one inner _
            set "newname=!newname:_=-!"
            :: prepend directory name and restore extension
            set "newname=%g_!newname!.jpg"
            :: rename and move to current directory
            move %h ".!newname!"
            )
            )





            share|improve this answer





















            • The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
              – AllenBooTung
              Nov 21 at 7:05














            3












            3








            3






            There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:



            Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.



            :: /r stands for recursive and ~n stands for (file)name
            for /r %g in (*_.jpg) do ren %g "%~ng.jphg"


            Then you can delete the files whose names do not end with _:



            del /s *.jpg


            And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:



            :: so that the variable expansion inside for's works as expected
            setlocal EnableDelayedExpansion
            :: loop through all the directories and store the names in g
            for /d %g in (*) do (
            for %h in ("%g*_.jphg") do (
            set "newname=%~nh"
            :: remove trailing _
            set "newname=!newname:~0,-1!"
            :: replace ALL _ with -, assuming the filename only has one inner _
            set "newname=!newname:_=-!"
            :: prepend directory name and restore extension
            set "newname=%g_!newname!.jpg"
            :: rename and move to current directory
            move %h ".!newname!"
            )
            )





            share|improve this answer












            There are no wildcards in CMD to not match a character, so instead you could temporally change the extension of the files whose names end with _:



            Note: remember to run the following snippets from upperFolder and to change the %g for %%g if you're running from a batch file.



            :: /r stands for recursive and ~n stands for (file)name
            for /r %g in (*_.jpg) do ren %g "%~ng.jphg"


            Then you can delete the files whose names do not end with _:



            del /s *.jpg


            And finally rename and move the files whose names end with _. But you're asking for something tricky and that is to prepend the filename with the directory name containing it, replace the inner _ of the filename with -, remove the trailing _ of the filename, and then move the file to upperFolder:



            :: so that the variable expansion inside for's works as expected
            setlocal EnableDelayedExpansion
            :: loop through all the directories and store the names in g
            for /d %g in (*) do (
            for %h in ("%g*_.jphg") do (
            set "newname=%~nh"
            :: remove trailing _
            set "newname=!newname:~0,-1!"
            :: replace ALL _ with -, assuming the filename only has one inner _
            set "newname=!newname:_=-!"
            :: prepend directory name and restore extension
            set "newname=%g_!newname!.jpg"
            :: rename and move to current directory
            move %h ".!newname!"
            )
            )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 at 5:37









            Juan López

            794




            794












            • The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
              – AllenBooTung
              Nov 21 at 7:05


















            • The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
              – AllenBooTung
              Nov 21 at 7:05
















            The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
            – AllenBooTung
            Nov 21 at 7:05




            The source's file path contained space. I added double quote to source into first command: ren "%g" "%~ng.jphg" as well as last command move "%h" ".!newname!" .These scripts worked fine. I am grateful for your first register help. Appreciate.
            – AllenBooTung
            Nov 21 at 7:05













            2














            Something like this should work for you:



            for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"


            Here we:




            • search all files of type jpg


              • delete those found

              • and then rename files with same name ending with _ into names of original files




            See for /? command or this for more information about %%~na and similar.



            Please note that unless your system OEM codepage supports kanji this will not work. You can get around this problem by switching to UTF-8 encoding by issuing command CHCP 65001 and saving batch file as UTF-8 (no BOM). See this for more information.






            share|improve this answer


























              2














              Something like this should work for you:



              for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"


              Here we:




              • search all files of type jpg


                • delete those found

                • and then rename files with same name ending with _ into names of original files




              See for /? command or this for more information about %%~na and similar.



              Please note that unless your system OEM codepage supports kanji this will not work. You can get around this problem by switching to UTF-8 encoding by issuing command CHCP 65001 and saving batch file as UTF-8 (no BOM). See this for more information.






              share|improve this answer
























                2












                2








                2






                Something like this should work for you:



                for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"


                Here we:




                • search all files of type jpg


                  • delete those found

                  • and then rename files with same name ending with _ into names of original files




                See for /? command or this for more information about %%~na and similar.



                Please note that unless your system OEM codepage supports kanji this will not work. You can get around this problem by switching to UTF-8 encoding by issuing command CHCP 65001 and saving batch file as UTF-8 (no BOM). See this for more information.






                share|improve this answer












                Something like this should work for you:



                for %%a in (*.jpg) do del /y "%%~a" & ren "%%~na_%%~nx" "%%~nxa"


                Here we:




                • search all files of type jpg


                  • delete those found

                  • and then rename files with same name ending with _ into names of original files




                See for /? command or this for more information about %%~na and similar.



                Please note that unless your system OEM codepage supports kanji this will not work. You can get around this problem by switching to UTF-8 encoding by issuing command CHCP 65001 and saving batch file as UTF-8 (no BOM). See this for more information.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 5:29









                Jack White

                33416




                33416






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53404835%2freplace-last-when-renaming-files%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python