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

Author Topic: Batch file to create folder following the name of specific file then move files  (Read 3126 times)

0 Members and 1 Guest are viewing this topic.

liechie

    Topic Starter


    Greenhorn

    • Experience: Experienced
    • OS: Windows 7

    Hi i'm an imposition specialist. I need to move files that are all present in one folder to folders that are of the file names. formats are (2).pdf and .psd. 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 9 files named as

    111111_1_abcde - pdf
    111111_001_Cut - psd
    111111_1_Bar - pdf

    222222_2_abcde - pdf
    222222_002_Cut - psd
    222222_2_Bar - pdf

    333333_ 3_2S_100 - pdf
    333333_3_Barcode - psd
    333333_A_-003_Cut - pdf

    I need a batch file that will make folders 111111, 222222 and 333333 and move all the respective files into the respective folders. So, files 111111_1_abcde - pdf, 111111_001_Cut - psd, 111111_1_Bar - pdf will be in folder named after the fiest pdf w/c is files 111111_1_abcde - pdf that doesn't have extension of Bar.

    I'm researching every now and then to figure this out that I just came up making 1 folder for the example mentioned above and not 3. I've been to stock overflow and other sites that helps in coding but ends up always a mistake.

    For this i tried these programs with the basic language that i have about dos programming and using examples from other thread is this website

    http://www.computerhope.com/forum/index.php?topic=152980.0

    I'm hoping that some script geek will have a lil time for this. It will be very appreciated

    thanks in advance!

    Squashman



      Specialist
    • Thanked: 119
    • Experience: Experienced
    • OS: Other
    I am not understanding what you want the folder names to be with your given example because this statement confuses me.
    Quote
    So, files 111111_1_abcde - pdf, 111111_001_Cut - psd, 111111_1_Bar - pdf will be in folder named after the fiest pdf w/c is files 111111_1_abcde - pdf that doesn't have extension of Bar.

    liechie

      Topic Starter


      Greenhorn

      • Experience: Experienced
      • OS: Windows 7
      I'll make a more understandable example, sorry for typography error and confusing

      111111_1_abcde - pdf
      111111_001_Cut - psd
      111111_1_Bar - pdf

      created folder name: 111111_1_abcde

      thanks a lot



      Squashman



        Specialist
      • Thanked: 119
      • Experience: Experienced
      • OS: Other
      For example, if I have 9 files named as

      111111_1_abcde - pdf
      111111_001_Cut - psd
      111111_1_Bar - pdf

      222222_2_abcde - pdf
      222222_002_Cut - psd
      222222_2_Bar - pdf

      333333_ 3_2S_100 - pdf
      333333_3_Barcode - psd
      333333_A_-003_Cut - pdf
      So these files have no file extension?

      I am also confused on which file from the 333333 set is supposed to be used for the folder name?

      liechie

        Topic Starter


        Greenhorn

        • Experience: Experienced
        • OS: Windows 7
        folder name: 111111_1_abcde
        111111_1_abcde - pdf
        111111_001_Cut - psd
        111111_1_Bar - pdf

        folder name: 222222_2_abcde
        222222_2_abcde - pdf
        222222_002_Cut - psd
        222222_2_Bar - pdf

        folder name: 333333_ 3_2S_100
        333333_ 3_2S_100 - pdf
        333333_3_Barcode - psd
        333333_A_-003_Cut - pdf

        they are pdf and a psd. each folder will have 2 pdf and 1 psd

        liechie

          Topic Starter


          Greenhorn

          • Experience: Experienced
          • OS: Windows 7
          here's another example

          folder name will be: 306113_A_3

          306113_A_3
          306113_A_BAR
          306113_003_Cut

          thank a lot!

          Squashman



            Specialist
          • Thanked: 119
          • Experience: Experienced
          • OS: Other
          they are pdf and a psd. each folder will have 2 pdf and 1 psd
          Then why are you providing file name examples with a space dash space for the file extension.

          This seems to make more sense to me.
          333333_ 3_2S_100.pdf
          333333_3_Barcode.psd
          333333_A_-003_Cut.pdf

          liechie

            Topic Starter


            Greenhorn

            • Experience: Experienced
            • OS: Windows 7
            correct, they are just the file format

            I just use dash space to indicate that it's a pdf/psd

            names will be just

            111111_1_abcde
            111111_001_Cut
            111111_1_Bar

            222222_2_abcde
            222222_002_Cut
            222222_2_Bar

            333333_ 3_2S_100
            333333_3_Barcode
            333333_A_-003_Cut

            306113_A_3
            306113_A_BAR
            306113_003_Cut

            hope that this will help

            thanks again

            Squashman



              Specialist
            • Thanked: 119
            • Experience: Experienced
            • OS: Other
            correct, they are just the file format

            I just use dash space to indicate that it's a pdf/psd

            names will be just

            111111_1_abcde
            111111_001_Cut
            111111_1_Bar

            222222_2_abcde
            222222_002_Cut
            222222_2_Bar

            333333_ 3_2S_100
            333333_3_Barcode
            333333_A_-003_Cut

            306113_A_3
            306113_A_BAR
            306113_003_Cut

            hope that this will help

            thanks again
            So now the file names have no extension?

            liechie

              Topic Starter


              Greenhorn

              • Experience: Experienced
              • OS: Windows 7
              yes they do not have, I just used that dash space to indicate its file format

              Squashman



                Specialist
              • Thanked: 119
              • Experience: Experienced
              • OS: Other
              yes they do not have, I just used that dash space to indicate its file format
              Are you using Windows? Windows can only identify the file type and open files based on the file extension. It will not show the correct icon for a file without it.

              liechie

                Topic Starter


                Greenhorn

                • Experience: Experienced
                • OS: Windows 7
                sorry I made a confusion again,

                these are the file formats

                333333_ 3_2S_100.pdf
                333333_3_Barcode.psd
                333333_A_-003_Cut.pdf

                306113_A_3.pdf
                306113_A_BAR.psd
                306113_003_Cut.pdf

                sorry for the late reply




                liechie

                  Topic Starter


                  Greenhorn

                  • Experience: Experienced
                  • OS: Windows 7
                  Folder arrangement using batch file
                  « Reply #12 on: January 24, 2017, 08:54:42 AM »
                  Hi Everyone,

                  Im not that good is scripting and It will be very appreciated if someone will spend a minute with this on

                  is it possible for a batch script to open a folder and arranged like this




                  thanks!

                  Allan

                  • Moderator

                  • Mastermind
                  • Thanked: 1202
                  • Experience: Guru
                  • OS: Windows 10
                  Please don't start multiple threads for the same question. I merged the two threads into this one.

                  patio

                  • Moderator


                  • Genius
                  • Maud' Dib
                  • Thanked: 1682
                    • Yes
                  • Experience: Beginner
                  • OS: Windows 7
                  Also the image is impossible to view...
                     
                   
                  " Anyone who goes to a psychiatrist should have his head examined. "

                  jakel



                    Starter

                    • Yes
                  • Experience: Experienced
                  • OS: Windows 7
                  save below line as myvbs.vbs
                  change line 13 to the target folder also
                  ---------------------------------------------------------
                  Option Explicit
                  On Error Resume Next
                  Dim T
                  dim fs
                  dim folder
                  dim files
                  dim file
                  Dim oFSO
                  Dim X

                  Set fs = CreateObject("Scripting.FileSystemObject")
                  'Directory you want sorted
                  Set folder = fs.GetFolder("c:\myvbs\")

                  Set files = folder.Files
                    For Each file in files
                      If file.name = "myvbs.vbs" Then
                        rem wscript.echo "oops found myself"
                     Else
                        T = Mid(file.name,1,len(file.name) - 4)
                        MD(T)
                        Set oFSO = CreateObject ("Scripting.FileSystemObject")
                        X = folder & "\" & T & "\"
                        oFSO.MoveFile file.name,X & file.name
                     End IF
                    Next

                  Private Function MD(X)
                  Dim objFS, objFol
                  Set objFS = CreateObject("Scripting.FileSystemObject")
                  Set objFol = objFS.CreateFolder(X)
                  End function

                  Squashman



                    Specialist
                  • Thanked: 119
                  • Experience: Experienced
                  • OS: Other
                  Looks like your code is creating a folder for each file name.  Which is not what the user asked.  Of the set of three files, there should only be one folder created and the three files moved to that folder.  Also, why not just use .GetBaseName(strPath) instead of stripping the extension with -4.  Many file extensions these days are no longer dot 3.

                  jakel



                    Starter

                    • Yes
                  • Experience: Experienced
                  • OS: Windows 7
                  it would be more efficient if it didnt try to make a folder each time, but the outcome is the same. good point with getbasename.
                  i have cleaned it up a little bit and added what you said , also filename is irrelevant now as long as it has a .vbs extension , and it will just run in the folder it is in.
                  ___________________________________

                  Dim T , fs, folder, files, file ,X , objFol
                  Set fs = CreateObject("Scripting.FileSystemObject")
                  Set folder = fs.GetFolder(WScript.ScriptFullName & "\..")
                  Set files = folder.Files
                  For Each file in files
                     If Not file.name = Wscript.ScriptName Then
                        T = fs.getbasename(file.name)
                        Set objFol = FS.CreateFolder(T)
                        X = folder & "\" & T & "\"
                        FS.MoveFile file.name,X & file.name
                     End IF
                  Next

                  jakel



                    Starter

                    • Yes
                  • Experience: Experienced
                  • OS: Windows 7
                  not even a thankyou for solving this problem after a year  :'(

                  Salmon Trout



                    Genius

                    Thanked: 923
                    • Yes
                  • Computer: Specs
                  • Experience: Experienced
                  • OS: Other
                  not even a thankyou for solving this problem after a year  :'(
                  If you are here for teh th4nxx you are in the wrong place.