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

Author Topic: File Type Organizer..??  (Read 18050 times)

0 Members and 1 Guest are viewing this topic.

gumbaz

    Topic Starter


    Intermediate

    File Type Organizer..??
    « on: January 25, 2009, 02:38:12 PM »
    I need to make a BAT Script that will search a Folder & all its Sub-Contents for every file type it has in it,
    then create a folder for each type of file Found, then move each file type to its corresponding File-Type-Folder
    So if it finds a .JPG file it creates a Folder named "JPG" then moves that file into that "JPG" folder.. Ect
    So for each file Found a corresponding folder is created & named base on the file type found "JPG" "AVI" "MPG" "PDF" Ect..
    Then each file that is found is moved into the its corresponding File Type Folder..

    This was an old BAT i was using before but i need to update it a lil more to do what im asking here..
    Can anyone please help me out to tweak my existing code to do what I want..
    Thanx..

    Code: [Select]
    CD /D C:\FILES
    set MEDIA=C:\[-MEDIA-]
    if not exist %MEDIA% MD %MEDIA%
    setlocal enabledelayedexpansion
    for %%T in (jpg jpeg gif bmp png tiff swf avi mpg mpeg wmv flv mp4 mov vob) do (
      set file-type=%%T
    echo file type !file-type!
    set /a num=1
    (
    for /f "delims==" %%F in ('dir /s /b *.!file-type!') do (
         set pad=
         if !num! LEQ 999999 set pad=0!pad!
         if !num! LEQ 99999 set pad=0!pad!
         if !num! LEQ 9999 set pad=0!pad!
         if !num! LEQ 999 set pad=0!pad!
         if !num! LEQ 99 set pad=0!pad!
         if !num! LEQ 9 set pad=0!pad!
         ATTRIB -H -S -A -R "%%F"
         IF NOT EXIST "%MEDIA%\%%T" MD "%MEDIA%\%%T"
         COPY "%%F" "%MEDIA%\%%T\!pad!!num!.!file-type!"
         DEL /F /Q "%%F"
         ECHO MOVED "%%F" to "%MEDIA%\%%T\!pad!!num!.!file-type!"
         set /a num=!num!+1
    )
    )
    )

    fireballs



      Apprentice

    • Code:Terminal
    • Thanked: 3
      Re: File Type Organizer..??
      « Reply #1 on: January 25, 2009, 03:24:01 PM »
      how does this look?

      Code: [Select]
      for /f "delims=" %%A in ('dir /b /a-d') do (
      if exist "%CD%\%%~xA" (xcopy "%%A" "%CD%\%%~xA") else (MD "%CD%\%%~xA" & xcopy "%%A" "%CD%\%%~xA")
      del "%%A"
      )

      :EDIT sorry i missed a few things should work now!

      FB
      « Last Edit: January 25, 2009, 03:35:36 PM by fireballs »
      Next time google it.

      Dias de verano

      • Guest
      Re: File Type Organizer..??
      « Reply #2 on: January 25, 2009, 03:40:08 PM »
      Code: [Select]
      IF NOT EXIST "%MEDIA%\%%T" MD "%MEDIA%\%%T"
           COPY "%%F" "%MEDIA%\%%T\!pad!!num!.!file-type!"

      The inner loop does not see the variable %%T, I think.

      gumbaz

        Topic Starter


        Intermediate

        Re: File Type Organizer..??
        « Reply #3 on: January 25, 2009, 03:43:46 PM »
        thanx for the help FB..
        could you comment your code for me please so i know what its doing  
        also can you use my Variables instead of yours .. Im not sure what yours mean..?? (%%A) (%CD%) (%%~xA)
        Thnx again for the help..

        @Dias de verano, I dont get what you mean..??

        fireballs



          Apprentice

        • Code:Terminal
        • Thanked: 3
          Re: File Type Organizer..??
          « Reply #4 on: January 25, 2009, 03:50:13 PM »
          %CD%=the current directory.
          %%A=the file the for loop is currently working on.
          %%~xA=the extension of the file that the for loop is currently working on.

          Code: [Select]
          for /f "delims=" %%A in ('dir /b /a-d') do (Takes all the files in the current directory.
          Code: [Select]
          if exist "%CD%\%%~xA" (xcopy "%%A" "%CD%\%%~xA") If the directory "current directory/extension" exists copy "current file" to "current directory\extension"
          Code: [Select]
          else (MD "%CD%\%%~xA" & xcopy "%%A" "%CD%\%%~xA")If the directory "current directory\extension" doesn't exist "make directory, then copy file to directory"
          Code: [Select]
          del "%%A" delete current file and move onto the next one.

          FB
          Next time google it.

          gumbaz

            Topic Starter


            Intermediate

            Re: File Type Organizer..??
            « Reply #5 on: January 25, 2009, 04:13:34 PM »
            ok thanks.
            So how would I incorporate a same file name collision code into this..?? encase a Src file needs to be moved and a file already exists in the destination folder. how can I Append like (1) (2) (3) Ect into the new file name to be copied over if a file already exists in the dest folder with the same name..??

            So if "C:\[MEDIA]\JPG\PIC-ABC.JPG" already exists and "F:\PIC-ABC.JPG" needs to be moved over to "C:\[MEDIA]\JPG" how would I append (1) to its file name like: "C:\[MEDIA]\JPG\PIC-ABC(1).JPG" and so on for each new same named file the needs to be moved over..??

            gumbaz

              Topic Starter


              Intermediate

              Re: File Type Organizer..??
              « Reply #6 on: January 25, 2009, 05:39:18 PM »
              Anybody got any ideas at all..??

              Dias de verano

              • Guest
              Re: File Type Organizer..??
              « Reply #7 on: January 26, 2009, 12:30:08 AM »
              Bad Gumbaz, bumping! And after only 2 hours!

              fireballs



                Apprentice

              • Code:Terminal
              • Thanked: 3
                Re: File Type Organizer..??
                « Reply #8 on: January 26, 2009, 06:38:10 AM »
                After my beautifully crafted three line answer the first time, i can't think of a nice, efficient way of doing what you want except like this:

                Code: [Select]
                for /f "delims=" %%A in ('dir /b /a-d') do (
                if not exist "%CD%\%%~xA" MD "%CD%\%%~xA" & xcopy "%%A"
                if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA(1)") else (
                if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA(2)") else (
                REM etc.)
                )
                del "%%A")

                FB
                Next time google it.

                gumbaz

                  Topic Starter


                  Intermediate

                  Re: File Type Organizer..??
                  « Reply #9 on: January 26, 2009, 11:07:03 AM »
                  Tanx verry muchos FB..
                  What do you mean by "else (REM etc.)" ..??

                  Will your code only rename the new file copied over up to 2 collisions or will it keep sequentially adding (+1) to each of found duplicate names..??
                  Ex..
                  "C:\[MEDIA]\JPG\PIC-ABC.JPG"  already exist....
                  "F:\FILES\PIC-ABC.JPG"  needs to be copied over, so it gets copied over as "C:\[MEDIA]\JPG\PIC-ABC(1).JPG"
                  then another file is found with the same name "F:\FILES\1\PIC-ABC.JPG"
                  and its copied over as "C:\[MEDIA]\JPG\PIC-ABC(2).JPG"
                  and every new same named file that needs to be copied over gets renamed with a sequential number (1)(2)(3)(4) Ect...
                  Ex..
                  "C:\[MEDIA]\JPG\PIC-ABC(1).JPG"
                  "C:\[MEDIA]\JPG\PIC-ABC(2).JPG"
                  "C:\[MEDIA]\JPG\PIC-ABC(3).JPG"
                  "C:\[MEDIA]\JPG\PIC-ABC(4).JPG"
                  "C:\[MEDIA]\JPG\PIC-ABC(5).JPG"
                  "Ect..."

                  Will your code do that, or does it need to be modified a lil more..??

                  fireballs



                    Apprentice

                  • Code:Terminal
                  • Thanked: 3
                    Re: File Type Organizer..??
                    « Reply #10 on: January 26, 2009, 11:32:45 AM »
                    wow i was not awake this morning, that code would not have worked. my appologies.

                    Code: [Select]
                    setlocal enabledelayedexpansion
                    for /f "delims=" %%A in ('dir /b /a-d') do (
                    set count=1
                    if not exist "%CD%\%%~xA" MD "%CD%\%%~xA"
                    if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA") else (
                    :1
                    if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
                    set /a count=!count!+1
                    goto 1)
                    )
                    del "%%A")

                    Hmm that should work now i'm a little bit more awake!

                    FB
                    « Last Edit: January 26, 2009, 11:50:38 AM by fireballs »
                    Next time google it.

                    Dias de verano

                    • Guest
                    Re: File Type Organizer..??
                    « Reply #11 on: January 26, 2009, 11:37:45 AM »
                    labels inside FOR loops are deprecated, as I suspect you will soon discover...

                    gumbaz

                      Topic Starter


                      Intermediate

                      Re: File Type Organizer..??
                      « Reply #12 on: January 26, 2009, 12:16:38 PM »
                      Eh not too sure about what that means.. im a lil stupid dis mornin...
                      Thnx thow for yer stupendis helpingness FB you da man dawg.!! whoop whoop..!! :)

                      DaveLembke



                        Sage
                      • Thanked: 662
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Expert
                      • OS: Windows 10
                      Re: File Type Organizer..??
                      « Reply #13 on: January 26, 2009, 05:26:19 PM »
                      Was looking this over and I found this to be very useful for a task I have with a external hard drive that is loaded with scattered data.

                      I added the /s  switch to focus also on all files in all sub directories starting at the root trigger point of the batch to xcopy these files as well into the grouped by file extension file folders.

                      I also got rid of the DEL command because I dont want to destroy the file structure, although total chaos, and end up with just .fileextension  for each file extension discovered and file placed within, but instead added this logging to file ability in place of it @echo.%%A>>Copied.txt to have a list of every file copied.

                      Code: [Select]
                      setlocal enabledelayedexpansion
                      for /f "delims=" %%A in ('dir /b /s /a-d') do (
                      set count=1
                      if not exist "%CD%\%%~xA" MD "%CD%\%%~xA"
                      if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA") else (
                      :1
                      if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
                      set /a count=!count!+1
                      goto 1)
                      )
                      @echo."%%A">>Copied.txt)

                      But what is strange and I cant figure out is why when finding an alternate of the same name it is not adding (1) or (2) etc at the end of the name to keep same file names seperate. It prompts do you want to overwrite instead of adding the (1) etc to the end of the file name such as 2007taxes(1).pdf and 2007Taxes(2).pdf  ... Any idea what may be going wrong?   :-\

                      I thought that maybe instead of IF NOT EXIST for

                      if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
                      set /a count=!count!+1
                      goto 1)

                      should be IF EXIST then increment +1, but when trying to fix this I get stuck in endless loop because the condition doesnt change to exit the loop.

                      gumbaz

                        Topic Starter


                        Intermediate

                        Re: File Type Organizer..??
                        « Reply #14 on: February 01, 2009, 05:22:28 AM »
                        This what i got so far into this batch work..
                        The only problem is that after it copies the first duplicate named file over to the corresponding extension folder Ex. "Picture(1).JPG" when it goes to copy the next duplicate named file over as "Picture(2).JPG" it doesn't work correctly because it says it cant find %F or whatever..
                        can you guys please help me figure out whats going wrong in the code..??

                        Code: [Select]
                        CD /D C:\FILES
                        set MEDIA=C:\[-MEDIA-]
                        setlocal enabledelayedexpansion
                        if not exist %MEDIA% MD %MEDIA%
                        for /f "delims=" %%F in ('DIR /AH /AS /AR /AA /B /S') do (
                        set /a NUM=1
                        if not exist "%MEDIA%\%%~xF" MD "%MEDIA%\%%~xF"
                        if not exist "%MEDIA%\%%~xF\%%~nxF" (ATTRIB -H -S -A -R "%%F" & copy "%%F" "%MEDIA%\%%~xF" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nxF") else (
                        :TOP
                        echo %NUM%
                        ECHO %%F
                        if not exist "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" (ATTRIB -H -S -A -R "%%F" & copy "%%F" "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF") else (
                        set /a NUM+=1 & goto TOP)
                        )
                        DEL /F /Q "%%F"
                        )