Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to make folders with part of file name and then copy files  (Read 22419 times)

0 Members and 1 Guest are viewing this topic.

doshisahil95

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Windows 7
    I am a designer. I need to move files that are all present in one folder to folders that are of the file names. The files are of two formats, .psd and .jpg. I need a batch file that will read the file name, make a folder of part of that file name and move all files that start with that file name into that folder regardless the extension.

    For example, if I have 12 files named as

    20.psd
    20.jpg
    20_low.jpg
    20_mob.jpg

    220.psd
    220.jpg
    220_low.jpg
    220_mob.jpg

    120.psd
    120.jpg
    120_low.jpg
    120_mob.jpg

    I need a batch file that will make folders 20, 220 and 120 and move all the respective files into the respective folders. So, files 20.psd, 20.jpg, 20_low.jpg and 20_mob.jpg will be in folder named 20 and so on. I tried various links on stackoverflow.com but i couldnt find anything that works. Please help. This is a very monotonous and time consuming job and having a simple program to do my job is useful! :-p

    For this i tried these programs with the basic language that i have about dos programming and using examples from stack overflow.

    http://stackoverflow.com/questions/11441317/need-a-script-to-create-folders-based-on-file-names-and-auto-move-files
    http://stackoverflow.com/questions/19992530/batch-create-folders-based-on-part-of-file-name-and-move-files-into-that-folder

    Now the problem i am facing with this code is that this isnt the kind of file name i am using. Also, there is no division as such in the file name. What can i do to make this work?

    erobby



      Rookie

      • Experience: Experienced
      • OS: Windows 7

      Salmon Trout

      • Guest
      Re: Batch file to make folders with part of file name and then copy files
      « Reply #2 on: November 15, 2015, 03:09:32 AM »
      Assumes: folders do not exist already, and no identically named files will ever exist
      Can remove lines starting with echo for quiet operation
      Put batch script in top folder with extension .bat
      Wil make folders under this

      Code: [Select]
      @echo off
      for %%F in (*.psd *.jpg) do (
      for %%A in (%%F) do (
      echo File found:   %%A
      echo Creating dir  "\%%~nA"
      md "%%~nA"
      move "%%A" "%%~nA"
      )
      )
      echo Finished
      pause


      Salmon Trout

      • Guest
      Re: Batch file to make folders with part of file name and then copy files
      « Reply #3 on: November 15, 2015, 05:13:28 AM »
      Please ignore above. Unfortunately I did not read your requirement carefully enough. Sorry. This script creates a folder for either the full file name, or if it contains an underscore, the part before the underscore. As before, you can remove or comment out all the lines starting with echo, and if you do not want the script to wait for a keypress at the end, you can do likewise with the final "pause" command.

      @echo off
      setlocal enabledelayedexpansion
      for %%A in (*.psd *.jpg) do (
         echo file found  %%A
         for /f "delims=" %%B in ("%%A") do set fname=%%~nB
         for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
         for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
         echo folder name !folname!
         if not exist "!folname!" (
            echo Folder !folname! does not exist, creating
            md "!folname!"
         ) else (
            echo Folder !folname! exists
         )
         echo Moving file %%A to folder !folname!
         move "%%A" "!folname!"
         )
      echo Finished
      pause


      Tested with these files in the same folder as the batch:

      apple.jpg
      apple.psd
      bear.jpg
      bear.psd
      cat.jpg
      cat.psd
      cat_low.jpg
      cat_low.psd
      cat_mob.jpg
      cat_mob.psd


      Batch output:

      file found  apple.psd
      folder name apple
      Folder apple does not exist, creating
      Moving file apple.psd to folder apple
              1 file(s) moved.
      file found  bear.psd
      folder name bear
      Folder bear does not exist, creating
      Moving file bear.psd to folder bear
              1 file(s) moved.
      file found  cat.psd
      folder name cat
      Folder cat does not exist, creating
      Moving file cat.psd to folder cat
              1 file(s) moved.
      file found  cat_low.psd
      folder name cat
      Folder cat exists
      Moving file cat_low.psd to folder cat
              1 file(s) moved.
      file found  cat_mob.psd
      folder name cat
      Folder cat exists
      Moving file cat_mob.psd to folder cat
              1 file(s) moved.
      file found  apple.jpg
      folder name apple
      Folder apple exists
      Moving file apple.jpg to folder apple
              1 file(s) moved.
      file found  bear.jpg
      folder name bear
      Folder bear exists
      Moving file bear.jpg to folder bear
              1 file(s) moved.
      file found  cat.jpg
      folder name cat
      Folder cat exists
      Moving file cat.jpg to folder cat
              1 file(s) moved.
      file found  cat_low.jpg
      folder name cat
      Folder cat exists
      Moving file cat_low.jpg to folder cat
              1 file(s) moved.
      file found  cat_mob.jpg
      folder name cat
      Folder cat exists
      Moving file cat_mob.jpg to folder cat
              1 file(s) moved.
      Finished
      Press any key to continue . . .


      Files after run:

      \apple\apple.jpg
      \apple\apple.psd
      \bear\bear.jpg
      \bear\bear.psd
      \cat\cat.jpg
      \cat\cat.psd
      \cat\cat_low.jpg
      \cat\cat_low.psd
      \cat\cat_mob.jpg
      \cat\cat_mob.psd




      Salmon Trout

      • Guest
      Re: Batch file to make folders with part of file name and then copy files
      « Reply #4 on: November 15, 2015, 06:52:24 AM »
      You don't actually need this line, but it won't do any harm:

      for /f "delims=" %%C in ("%%A") do set fextn=%%~xC

      erobby



        Rookie

        • Experience: Experienced
        • OS: Windows 7
        Re: Batch file to make folders with part of file name and then copy files
        « Reply #5 on: November 15, 2015, 07:36:11 AM »
        Salmon,

        Good job, I wasn't sure that the underscore was standard.  The Link I posted would go through each file name character by character and strip out only the numbers so no matter what the file name is as long as it grab the digits and from there he can create the directory and move the files.  But if the naming convention is as simple as having a underscore after the numbers or not yours script should work perfectly.

        doshisahil95

          Topic Starter


          Starter

          • Experience: Experienced
          • OS: Windows 7
          Re: Batch file to make folders with part of file name and then copy files
          « Reply #6 on: November 15, 2015, 09:36:53 AM »
          Thanks a lot everyone! I will try each of your ideas and let youll know! thanks a ton again! Youll have saved me from a lot of boring monotonous work.

          @ Salmon - Just so that i can learn, can you explain the logic of how you came up with the code? And can you also explain the code to me???

          doshisahil95

            Topic Starter


            Starter

            • Experience: Experienced
            • OS: Windows 7
            Re: Batch file to make folders with part of file name and then copy files
            « Reply #7 on: November 15, 2015, 09:42:58 AM »
            @ Salmon,

            The batch file works like a breeze!! Thank you very much my friend! I owe you real big! If you do get the time, can you please explain how you came up with the program and the syntax bit of dos?

            Thanks

            erobby



              Rookie

              • Experience: Experienced
              • OS: Windows 7
              Re: Batch file to make folders with part of file name and then copy files
              « Reply #8 on: November 15, 2015, 10:14:34 AM »
              @Salmon

              I hope you don't mind

              @echo off

              :: Needed because you are working with variables that are immediately called
              setlocal enabledelayedexpansion

              :: Start of the loop to get all files with a psd or jpg Extension
              for %%A in (*.psd *.jpg) do (
                 echo file found  %%A

              :: Grabs only the file name
                 for /f "delims=" %%B in ("%%A") do set fname=%%~nB

              :: Grabs only the extension
                 for /f "delims=" %%C in ("%%A") do set fextn=%%~xC

              :: Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
                 for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
                 echo folder name !folname!

              :: Checks for the existence of the folder, if the folder does not exist it creates the folder
                 if not exist "!folname!" (
                    echo Folder !folname! does not exist, creating
                    md "!folname!"
                 ) else (
                    echo Folder !folname! exists
                 )

              :: Moves the file to the folder
                 echo Moving file %%A to folder !folname!
                 move "%%A" "!folname!"
                 )
              echo Finished

              It's nice and clean in comparison to what I had in mind Kudos to Salmon

              Salmon Trout

              • Guest
              Re: Batch file to make folders with part of file name and then copy files
              « Reply #9 on: November 15, 2015, 01:04:24 PM »
              :: Needed because you are working with variables that are immediately called
              setlocal enabledelayedexpansion

              erobby, a good effort. I would just add a bit more about delayed expansion. We need to enable delayed expansion because of the behaviour of variables which are both set and expanded inside a loop or other structure with brackets such as IF blocks - consider this code

              setlocal enabledelayedexpansion
              if %foo%==%bar% (
                  set var=YES
                  echo Var is !var!
              )


              If you used %var% then if it did not exist before the IF block or loop then nothing would get echoed (the variable would be empty); if it did exist you would get the previous value befor the script got to the bracket structure. For this reason we enable delayed expansion and use exclamation marks instead of percent symbols. This is because cmd.exe expands all normal percent  variables before running and variables inside loops are created and changed at run time. This is something that changed from MS-DOS.

              This brings me to what some may feel is a minor point (I have had arguments about this!) namely using double colons to start a comment line. This was OK in MS-DOS under command.com but it has the potential to break Windows NT family scripts running under cmd.exe. It is unofficial and unsupported. The comment line starter is REM (case-insensitive). The reason I don't like double colons is that a double colon is really just a broken label and you can't have them inside a loop. A loop or other bracket structure is really just one line to to the commmand interpreter and any label will break that. (The script will crash)
               


              BoboEL



                Newbie

                • Experience: Beginner
                • OS: Windows 7
                Re: Batch file to make folders with part of file name and then copy files
                « Reply #10 on: January 25, 2016, 08:32:26 AM »
                Hello :)

                I have the same problem as the Topic Starter and Salmons solution is nearly perfect for me, except for one thing.
                Some of my file names contain exclamation marks and these file can't be moved by the script.
                I think the problem is "setlocal enabledelayedexpansion" but I'm new to batch scripting so I hope someone can help me solve this problem. Thanks.

                foxidrive



                  Specialist
                • Thanked: 266
                • Experience: Experienced
                • OS: Windows 8
                Re: Batch file to make folders with part of file name and then copy files
                « Reply #11 on: January 26, 2016, 08:27:28 AM »
                I didn't evaluate this at all - and it's untested.  I merely simplified the code as I read it.

                Code: [Select]
                @echo off
                for %%A in (*.psd *.jpg) do (
                    for /f "tokens=1* delims=_" %%D in ("%%~nA") do (
                       md "%%D" 2>nul
                   echo Moving file %%A to folder %%D
                   move "%%A" "%%D" >nul
                   ))
                echo Finished
                pause