Move all files from multiple subfolders into the parent folder












5














I would normally open the parent folder and search for * in order to select all of the files in subfolders, but in this instance, I have over 1,000,000 files that I need to sort through, so my explorer often crashes when trying to copy that many files through the GUI. I am not sure how much more effective this will be through the command prompt or a batch file, but it is worth a try, I suppose.



What I need to do is make it so that



|parent
| |123
| | 123abc.png
| |456
| | 456def.png
| |789
| | 789ghi.png


becomes



|parent
| 123abc.png
| 456def.png
| 789ghi.png


Yes, my actual file structure has the first 3 characters of the file name given to the folder name, if that can help at all in sorting these.










share|improve this question





























    5














    I would normally open the parent folder and search for * in order to select all of the files in subfolders, but in this instance, I have over 1,000,000 files that I need to sort through, so my explorer often crashes when trying to copy that many files through the GUI. I am not sure how much more effective this will be through the command prompt or a batch file, but it is worth a try, I suppose.



    What I need to do is make it so that



    |parent
    | |123
    | | 123abc.png
    | |456
    | | 456def.png
    | |789
    | | 789ghi.png


    becomes



    |parent
    | 123abc.png
    | 456def.png
    | 789ghi.png


    Yes, my actual file structure has the first 3 characters of the file name given to the folder name, if that can help at all in sorting these.










    share|improve this question



























      5












      5








      5


      3





      I would normally open the parent folder and search for * in order to select all of the files in subfolders, but in this instance, I have over 1,000,000 files that I need to sort through, so my explorer often crashes when trying to copy that many files through the GUI. I am not sure how much more effective this will be through the command prompt or a batch file, but it is worth a try, I suppose.



      What I need to do is make it so that



      |parent
      | |123
      | | 123abc.png
      | |456
      | | 456def.png
      | |789
      | | 789ghi.png


      becomes



      |parent
      | 123abc.png
      | 456def.png
      | 789ghi.png


      Yes, my actual file structure has the first 3 characters of the file name given to the folder name, if that can help at all in sorting these.










      share|improve this question















      I would normally open the parent folder and search for * in order to select all of the files in subfolders, but in this instance, I have over 1,000,000 files that I need to sort through, so my explorer often crashes when trying to copy that many files through the GUI. I am not sure how much more effective this will be through the command prompt or a batch file, but it is worth a try, I suppose.



      What I need to do is make it so that



      |parent
      | |123
      | | 123abc.png
      | |456
      | | 456def.png
      | |789
      | | 789ghi.png


      becomes



      |parent
      | 123abc.png
      | 456def.png
      | 789ghi.png


      Yes, my actual file structure has the first 3 characters of the file name given to the folder name, if that can help at all in sorting these.







      windows batch-file cmd.exe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 13 '17 at 17:51









      Hennes

      58.8k792141




      58.8k792141










      asked Nov 13 '15 at 3:32









      Wulfre

      33113




      33113






















          3 Answers
          3






          active

          oldest

          votes


















          12














          Use FOR /R at the command prompt:




          [FOR /R] walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.




          First create a staging folder outside of the parent folder you're moving files from. This will avoid possible circular references.



          In your case the command would look something like this:



          FOR /R "C:Source Folder" %i IN (*.png) DO MOVE "%i" "C:Staging Folder"


          If you want to put this into a batch file, change %i to %%i.



          Note the double-quotes are important, don't miss any of them out. They ensure any filenames containing spaces are dealt with correctly.



          Once the move is complete, you can rename/move the staging folder as required.



          TIP: If you have hard drive space to burn and time on hand, you may want to play it safe and copy the files rather than moving them, just in case something goes wrong. Just change MOVE to COPY in the above command.






          share|improve this answer





















          • +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
            – bigp
            Mar 24 '17 at 15:09



















          1














          Also use the environment variable %%~dpi which refers to the folder the files are in. You can then strip the trailing backslash which would then get the parent folder of the files. Below does just that.



          SetLocal EnableDelayedExpansion   
          FOR /R "C:Source Folder" %%i IN (*.png) DO (
          Set "CurrFile=%%~i"
          Set "TMPdp=%%~dpi"
          Set "ParFldr=!TMPdp:~0,-1!"
          @Echo MOVE "%%~i" "!ParFldr!" ) & REM Delete echo to move. Run to test.


          The ! enables Dynamic Expansion and can be used for the %%~i as well as the ParFldr. So the %%~i can be !CurrFldr! This will actually be required while your testing because some files will have strings Batch will not like. By not like, I mean they will cause the script to fail and exit. I just changed all the %%A to %%i for clarity. It really makes no difference if an A was used or a lowercase i What does matter is consistently using the same letter throughout, so i changed every single %%A to an %%i.






          share|improve this answer























          • While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
            – DavidPostill
            Nov 13 '15 at 13:03



















          1














          This is some sample code:



          :loop
          for /d %%D in (%1*) do (move "%%D*" %1 && rmdir "%%D")
          SHIFT
          set PARAMS=%1
          if not %PARAMS%!==! goto loop


          With this version you drag the folder from which you wish to remove the subfolder unto the batch and it will move all files from the subfolders into the parent folder. I use it for downloaded archives files which randomly have or haven't subfolder. Mind you, It was made with a single subfolder in mind, as specific for my case.



          'Shift' is to move to the next argument, when you drag many subfolder at once on the script.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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%2fsuperuser.com%2fquestions%2f999922%2fmove-all-files-from-multiple-subfolders-into-the-parent-folder%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









            12














            Use FOR /R at the command prompt:




            [FOR /R] walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.




            First create a staging folder outside of the parent folder you're moving files from. This will avoid possible circular references.



            In your case the command would look something like this:



            FOR /R "C:Source Folder" %i IN (*.png) DO MOVE "%i" "C:Staging Folder"


            If you want to put this into a batch file, change %i to %%i.



            Note the double-quotes are important, don't miss any of them out. They ensure any filenames containing spaces are dealt with correctly.



            Once the move is complete, you can rename/move the staging folder as required.



            TIP: If you have hard drive space to burn and time on hand, you may want to play it safe and copy the files rather than moving them, just in case something goes wrong. Just change MOVE to COPY in the above command.






            share|improve this answer





















            • +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
              – bigp
              Mar 24 '17 at 15:09
















            12














            Use FOR /R at the command prompt:




            [FOR /R] walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.




            First create a staging folder outside of the parent folder you're moving files from. This will avoid possible circular references.



            In your case the command would look something like this:



            FOR /R "C:Source Folder" %i IN (*.png) DO MOVE "%i" "C:Staging Folder"


            If you want to put this into a batch file, change %i to %%i.



            Note the double-quotes are important, don't miss any of them out. They ensure any filenames containing spaces are dealt with correctly.



            Once the move is complete, you can rename/move the staging folder as required.



            TIP: If you have hard drive space to burn and time on hand, you may want to play it safe and copy the files rather than moving them, just in case something goes wrong. Just change MOVE to COPY in the above command.






            share|improve this answer





















            • +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
              – bigp
              Mar 24 '17 at 15:09














            12












            12








            12






            Use FOR /R at the command prompt:




            [FOR /R] walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.




            First create a staging folder outside of the parent folder you're moving files from. This will avoid possible circular references.



            In your case the command would look something like this:



            FOR /R "C:Source Folder" %i IN (*.png) DO MOVE "%i" "C:Staging Folder"


            If you want to put this into a batch file, change %i to %%i.



            Note the double-quotes are important, don't miss any of them out. They ensure any filenames containing spaces are dealt with correctly.



            Once the move is complete, you can rename/move the staging folder as required.



            TIP: If you have hard drive space to burn and time on hand, you may want to play it safe and copy the files rather than moving them, just in case something goes wrong. Just change MOVE to COPY in the above command.






            share|improve this answer












            Use FOR /R at the command prompt:




            [FOR /R] walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.




            First create a staging folder outside of the parent folder you're moving files from. This will avoid possible circular references.



            In your case the command would look something like this:



            FOR /R "C:Source Folder" %i IN (*.png) DO MOVE "%i" "C:Staging Folder"


            If you want to put this into a batch file, change %i to %%i.



            Note the double-quotes are important, don't miss any of them out. They ensure any filenames containing spaces are dealt with correctly.



            Once the move is complete, you can rename/move the staging folder as required.



            TIP: If you have hard drive space to burn and time on hand, you may want to play it safe and copy the files rather than moving them, just in case something goes wrong. Just change MOVE to COPY in the above command.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '15 at 6:23









            misha256

            8,53664061




            8,53664061












            • +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
              – bigp
              Mar 24 '17 at 15:09


















            • +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
              – bigp
              Mar 24 '17 at 15:09
















            +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
            – bigp
            Mar 24 '17 at 15:09




            +1 that really helped! Note to self however - Use whole paths, relative paths didn't seem to pickup the location of the files in sub-folders.
            – bigp
            Mar 24 '17 at 15:09













            1














            Also use the environment variable %%~dpi which refers to the folder the files are in. You can then strip the trailing backslash which would then get the parent folder of the files. Below does just that.



            SetLocal EnableDelayedExpansion   
            FOR /R "C:Source Folder" %%i IN (*.png) DO (
            Set "CurrFile=%%~i"
            Set "TMPdp=%%~dpi"
            Set "ParFldr=!TMPdp:~0,-1!"
            @Echo MOVE "%%~i" "!ParFldr!" ) & REM Delete echo to move. Run to test.


            The ! enables Dynamic Expansion and can be used for the %%~i as well as the ParFldr. So the %%~i can be !CurrFldr! This will actually be required while your testing because some files will have strings Batch will not like. By not like, I mean they will cause the script to fail and exit. I just changed all the %%A to %%i for clarity. It really makes no difference if an A was used or a lowercase i What does matter is consistently using the same letter throughout, so i changed every single %%A to an %%i.






            share|improve this answer























            • While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
              – DavidPostill
              Nov 13 '15 at 13:03
















            1














            Also use the environment variable %%~dpi which refers to the folder the files are in. You can then strip the trailing backslash which would then get the parent folder of the files. Below does just that.



            SetLocal EnableDelayedExpansion   
            FOR /R "C:Source Folder" %%i IN (*.png) DO (
            Set "CurrFile=%%~i"
            Set "TMPdp=%%~dpi"
            Set "ParFldr=!TMPdp:~0,-1!"
            @Echo MOVE "%%~i" "!ParFldr!" ) & REM Delete echo to move. Run to test.


            The ! enables Dynamic Expansion and can be used for the %%~i as well as the ParFldr. So the %%~i can be !CurrFldr! This will actually be required while your testing because some files will have strings Batch will not like. By not like, I mean they will cause the script to fail and exit. I just changed all the %%A to %%i for clarity. It really makes no difference if an A was used or a lowercase i What does matter is consistently using the same letter throughout, so i changed every single %%A to an %%i.






            share|improve this answer























            • While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
              – DavidPostill
              Nov 13 '15 at 13:03














            1












            1








            1






            Also use the environment variable %%~dpi which refers to the folder the files are in. You can then strip the trailing backslash which would then get the parent folder of the files. Below does just that.



            SetLocal EnableDelayedExpansion   
            FOR /R "C:Source Folder" %%i IN (*.png) DO (
            Set "CurrFile=%%~i"
            Set "TMPdp=%%~dpi"
            Set "ParFldr=!TMPdp:~0,-1!"
            @Echo MOVE "%%~i" "!ParFldr!" ) & REM Delete echo to move. Run to test.


            The ! enables Dynamic Expansion and can be used for the %%~i as well as the ParFldr. So the %%~i can be !CurrFldr! This will actually be required while your testing because some files will have strings Batch will not like. By not like, I mean they will cause the script to fail and exit. I just changed all the %%A to %%i for clarity. It really makes no difference if an A was used or a lowercase i What does matter is consistently using the same letter throughout, so i changed every single %%A to an %%i.






            share|improve this answer














            Also use the environment variable %%~dpi which refers to the folder the files are in. You can then strip the trailing backslash which would then get the parent folder of the files. Below does just that.



            SetLocal EnableDelayedExpansion   
            FOR /R "C:Source Folder" %%i IN (*.png) DO (
            Set "CurrFile=%%~i"
            Set "TMPdp=%%~dpi"
            Set "ParFldr=!TMPdp:~0,-1!"
            @Echo MOVE "%%~i" "!ParFldr!" ) & REM Delete echo to move. Run to test.


            The ! enables Dynamic Expansion and can be used for the %%~i as well as the ParFldr. So the %%~i can be !CurrFldr! This will actually be required while your testing because some files will have strings Batch will not like. By not like, I mean they will cause the script to fail and exit. I just changed all the %%A to %%i for clarity. It really makes no difference if an A was used or a lowercase i What does matter is consistently using the same letter throughout, so i changed every single %%A to an %%i.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 13 '17 at 14:48

























            answered Nov 13 '15 at 11:21









            Noshad Chaudhry

            1284




            1284












            • While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
              – DavidPostill
              Nov 13 '15 at 13:03


















            • While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
              – DavidPostill
              Nov 13 '15 at 13:03
















            While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
            – DavidPostill
            Nov 13 '15 at 13:03




            While this may answer the question it is almost impossible to tell because of the poor formatting. Take some time to edit your answer and make it readable. Please read Markdown help.
            – DavidPostill
            Nov 13 '15 at 13:03











            1














            This is some sample code:



            :loop
            for /d %%D in (%1*) do (move "%%D*" %1 && rmdir "%%D")
            SHIFT
            set PARAMS=%1
            if not %PARAMS%!==! goto loop


            With this version you drag the folder from which you wish to remove the subfolder unto the batch and it will move all files from the subfolders into the parent folder. I use it for downloaded archives files which randomly have or haven't subfolder. Mind you, It was made with a single subfolder in mind, as specific for my case.



            'Shift' is to move to the next argument, when you drag many subfolder at once on the script.






            share|improve this answer




























              1














              This is some sample code:



              :loop
              for /d %%D in (%1*) do (move "%%D*" %1 && rmdir "%%D")
              SHIFT
              set PARAMS=%1
              if not %PARAMS%!==! goto loop


              With this version you drag the folder from which you wish to remove the subfolder unto the batch and it will move all files from the subfolders into the parent folder. I use it for downloaded archives files which randomly have or haven't subfolder. Mind you, It was made with a single subfolder in mind, as specific for my case.



              'Shift' is to move to the next argument, when you drag many subfolder at once on the script.






              share|improve this answer


























                1












                1








                1






                This is some sample code:



                :loop
                for /d %%D in (%1*) do (move "%%D*" %1 && rmdir "%%D")
                SHIFT
                set PARAMS=%1
                if not %PARAMS%!==! goto loop


                With this version you drag the folder from which you wish to remove the subfolder unto the batch and it will move all files from the subfolders into the parent folder. I use it for downloaded archives files which randomly have or haven't subfolder. Mind you, It was made with a single subfolder in mind, as specific for my case.



                'Shift' is to move to the next argument, when you drag many subfolder at once on the script.






                share|improve this answer














                This is some sample code:



                :loop
                for /d %%D in (%1*) do (move "%%D*" %1 && rmdir "%%D")
                SHIFT
                set PARAMS=%1
                if not %PARAMS%!==! goto loop


                With this version you drag the folder from which you wish to remove the subfolder unto the batch and it will move all files from the subfolders into the parent folder. I use it for downloaded archives files which randomly have or haven't subfolder. Mind you, It was made with a single subfolder in mind, as specific for my case.



                'Shift' is to move to the next argument, when you drag many subfolder at once on the script.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 at 21:35









                zx485

                724613




                724613










                answered Nov 22 at 20:45









                e.g

                112




                112






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f999922%2fmove-all-files-from-multiple-subfolders-into-the-parent-folder%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

                    AnyDesk - Fatal Program Failure

                    How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

                    QoS: MAC-Priority for clients behind a repeater