Need .bat that will make folder based on filename












2














I need .bat that will make folders based on filenames and put files inside. I have names formatted like this:



string1 - string2 - string3 - string2 - string3_number.jpg


I find out command that will take 3 first strings as i needed, but cant make it work further. When i try to put this command into bat it makes only folder called "--".



for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (set string1=%%a&set string2=%%b&set string3=%%c
md "%string1%-%string2%-%string3%")


putting @echo off and setlocal before this in .bat dont work
can someone help make it right so it will make folders based on names and put files in.
I read these posts but still can't even make folders:





  • Batch Create a Folder Based on Filename, and Move Multiple Related Files to The Created Folder

  • Need a script to create folders based on file names, and auto move files

  • Batch create folders based on part of file name and move files into that folder










share|improve this question



























    2














    I need .bat that will make folders based on filenames and put files inside. I have names formatted like this:



    string1 - string2 - string3 - string2 - string3_number.jpg


    I find out command that will take 3 first strings as i needed, but cant make it work further. When i try to put this command into bat it makes only folder called "--".



    for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (set string1=%%a&set string2=%%b&set string3=%%c
    md "%string1%-%string2%-%string3%")


    putting @echo off and setlocal before this in .bat dont work
    can someone help make it right so it will make folders based on names and put files in.
    I read these posts but still can't even make folders:





    • Batch Create a Folder Based on Filename, and Move Multiple Related Files to The Created Folder

    • Need a script to create folders based on file names, and auto move files

    • Batch create folders based on part of file name and move files into that folder










    share|improve this question

























      2












      2








      2







      I need .bat that will make folders based on filenames and put files inside. I have names formatted like this:



      string1 - string2 - string3 - string2 - string3_number.jpg


      I find out command that will take 3 first strings as i needed, but cant make it work further. When i try to put this command into bat it makes only folder called "--".



      for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (set string1=%%a&set string2=%%b&set string3=%%c
      md "%string1%-%string2%-%string3%")


      putting @echo off and setlocal before this in .bat dont work
      can someone help make it right so it will make folders based on names and put files in.
      I read these posts but still can't even make folders:





      • Batch Create a Folder Based on Filename, and Move Multiple Related Files to The Created Folder

      • Need a script to create folders based on file names, and auto move files

      • Batch create folders based on part of file name and move files into that folder










      share|improve this question













      I need .bat that will make folders based on filenames and put files inside. I have names formatted like this:



      string1 - string2 - string3 - string2 - string3_number.jpg


      I find out command that will take 3 first strings as i needed, but cant make it work further. When i try to put this command into bat it makes only folder called "--".



      for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (set string1=%%a&set string2=%%b&set string3=%%c
      md "%string1%-%string2%-%string3%")


      putting @echo off and setlocal before this in .bat dont work
      can someone help make it right so it will make folders based on names and put files in.
      I read these posts but still can't even make folders:





      • Batch Create a Folder Based on Filename, and Move Multiple Related Files to The Created Folder

      • Need a script to create folders based on file names, and auto move files

      • Batch create folders based on part of file name and move files into that folder







      batch batch-file






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 6 at 18:21









      Phenixsus

      154




      154






















          1 Answer
          1






          active

          oldest

          votes


















          3














          Because of delayedexpansion is required when setting and using a var in a (code block) it doesn't work.



          But as you don't change the vars that isn't needed at all.



          @Echo off & Setlocal EnableDelayedExpansion

          for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (
          set "Folder=%%a-%%b-%%c"
          Rem to remove the trailing space from the Folder
          set "Folder=!Folder:~0,-1!"
          If not exist "!Folder!" MD "!Folder!"
          )


          To also move files that is the wrong approach, you should then 1st iterate the files and 2nd split them as above.






          share|improve this answer





















          • Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
            – Allen Jackson
            Aug 6 at 21:08











          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%2f1346615%2fneed-bat-that-will-make-folder-based-on-filename%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Because of delayedexpansion is required when setting and using a var in a (code block) it doesn't work.



          But as you don't change the vars that isn't needed at all.



          @Echo off & Setlocal EnableDelayedExpansion

          for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (
          set "Folder=%%a-%%b-%%c"
          Rem to remove the trailing space from the Folder
          set "Folder=!Folder:~0,-1!"
          If not exist "!Folder!" MD "!Folder!"
          )


          To also move files that is the wrong approach, you should then 1st iterate the files and 2nd split them as above.






          share|improve this answer





















          • Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
            – Allen Jackson
            Aug 6 at 21:08
















          3














          Because of delayedexpansion is required when setting and using a var in a (code block) it doesn't work.



          But as you don't change the vars that isn't needed at all.



          @Echo off & Setlocal EnableDelayedExpansion

          for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (
          set "Folder=%%a-%%b-%%c"
          Rem to remove the trailing space from the Folder
          set "Folder=!Folder:~0,-1!"
          If not exist "!Folder!" MD "!Folder!"
          )


          To also move files that is the wrong approach, you should then 1st iterate the files and 2nd split them as above.






          share|improve this answer





















          • Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
            – Allen Jackson
            Aug 6 at 21:08














          3












          3








          3






          Because of delayedexpansion is required when setting and using a var in a (code block) it doesn't work.



          But as you don't change the vars that isn't needed at all.



          @Echo off & Setlocal EnableDelayedExpansion

          for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (
          set "Folder=%%a-%%b-%%c"
          Rem to remove the trailing space from the Folder
          set "Folder=!Folder:~0,-1!"
          If not exist "!Folder!" MD "!Folder!"
          )


          To also move files that is the wrong approach, you should then 1st iterate the files and 2nd split them as above.






          share|improve this answer












          Because of delayedexpansion is required when setting and using a var in a (code block) it doesn't work.



          But as you don't change the vars that isn't needed at all.



          @Echo off & Setlocal EnableDelayedExpansion

          for /F "tokens=1,2,3 delims=-" %%a in ('dir /B /A-D') do (
          set "Folder=%%a-%%b-%%c"
          Rem to remove the trailing space from the Folder
          set "Folder=!Folder:~0,-1!"
          If not exist "!Folder!" MD "!Folder!"
          )


          To also move files that is the wrong approach, you should then 1st iterate the files and 2nd split them as above.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 6 at 18:39









          LotPings

          4,4181720




          4,4181720












          • Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
            – Allen Jackson
            Aug 6 at 21:08


















          • Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
            – Allen Jackson
            Aug 6 at 21:08
















          Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
          – Allen Jackson
          Aug 6 at 21:08




          Well done @LotPings. Introduced in 1999... I never knew about Delayed Expansion.
          – Allen Jackson
          Aug 6 at 21:08


















          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%2f1346615%2fneed-bat-that-will-make-folder-based-on-filename%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

          Plaza Victoria

          Puebla de Zaragoza

          Musa