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

Author Topic: Elegant Batch File to Make Folder and Copy 2 Specific file into  (Read 4086 times)

0 Members and 1 Guest are viewing this topic.

Biscuit

    Topic Starter


    Greenhorn

    • Experience: Familiar
    • OS: Unknown
    Hi Folks

    It's been some time since I last resorted to a batch file and I'm a little rusty so wondered if anyone could help. I have a large number of docs with files names in the in the format XXX-YYY-ZZZ1.DOC

    The batch file I'm trying to write would repeatedly generate a folder and then move 2 specific files into it, then move on till all files have been moved. I have a table that associates each file with a specific folder.

    So the batch file could be cintructed in the form:

    MD d:\AAA BBB1    [The unique folder name]
    MOVE XXX-YYY-ZZZ001.DOC d:\AAA BBB1\XXX-YYY-ZZZ001.DOC
    MOVE XXX-YYY-ZZZ002.DOC d:\AAA BBB1\XXX-YYY-ZZZ002.DOC
    MD d:\AAA BBB2    [The unique folder name]
    MOVE XXX-YYY-ZZZ003.DOC d:\AAA BBB1\XXX-YYY-ZZZ003.DOC
    MOVE XXX-YYY-ZZZ004.DOC d:\AAA BBB1\XXX-YYY-ZZZ004.DOC
    ........ and so on till all files have been moved.

    This works but is long winded as I'm are talking of 1000 plus files and the excercise will need to be repeated several times.

    Was wondering if there is a more elegant way of doing this, preferably with a batch file that will make the folder and then copy the 2 files into it using a single command line.

    Any Guidance Much Apreciated

    Biscuit



    nil

    • Global Moderator


    • Intermediate
    • Thanked: 15
      • Experience: Experienced
      • OS: Linux variant
      Do not communicate by sharing memory; instead, share memory by communicating.

      --Effective Go

      Salmon Trout

      • Guest
      Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
      « Reply #2 on: March 26, 2019, 11:37:45 AM »
      What is the rule that decides which files get moved to which folder?

      Biscuit

        Topic Starter


        Greenhorn

        • Experience: Familiar
        • OS: Unknown
        Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
        « Reply #3 on: March 26, 2019, 01:17:05 PM »
        What is the rule that decides which files get moved to which folder?

        Hi and thanks for getting back to me.

        Currently I've a very large Excel file (1200 lines) which I'd like to edit and then save a a txt file, so I can use this as the basis of my batch.

        Each line of the xls file currently has, 2 columns  Column1 is name Name of the Folder and Column 2 is the name of the of file, all files are unique and the folders are usually referenced twice, as most folders should end up with 2 files moved in.

        Hope I'm making myseld clear.

        Thanks

        Biscuit

        Salmon Trout

        • Guest
        Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
        « Reply #4 on: March 26, 2019, 01:43:58 PM »
        Please show a sample of the Excel file.

        Biscuit

          Topic Starter


          Greenhorn

          • Experience: Familiar
          • OS: Unknown
          Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
          « Reply #5 on: March 29, 2019, 04:56:29 AM »
          Hi Thanks for getting back to me

          Here is a sample of the spreadsheet (which could end up being over 2000 lines long). Currently all the files are in a single folder, so all file names will be unique. Folder names are defined and will usually be referenced 2-3 times in the spreadsheet as each is associated with 2-3 drawings.

          Looking for a nice script that will both generate the folder and move each file into it's respective folder.

          Thanks

          Biscuit

          Salmon Trout

          • Guest
          Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
          « Reply #6 on: March 29, 2019, 12:29:47 PM »
          You can make the lines of a batch script in Excel itself; I often do this.

          Have Notepad open.

          Step 1. Create commands to make the folders if they don't already exist (you can skip this if each folder does already exist, but it won't do any harm if you run it)

          I am using the letters for the columns, and numbers for the rows, as they are in your example spreadsheet.

          I hope you can see the formula in cell D3. Copy this down so it is in column D all the way down. Duplicated folder names won't matter.



          Highlight the cells in Column D that have the formula and paste into Notepad first.

          Step 2. make the commands to move the files

          Again, I hope you can see the formulas



          Highlight and copy the formulas in Column E into Notepad, below the commands you did in step (1) above.



          Finally, in Notepad, save the text as a .bat file in the folder where the .dwg files are located, and run it.

          If you don't want to see the commands scroll past, put @echo off as the first line of the batch.

          If you want the batch window to stay open until you press a key, put pause as the last line of the batch.

          The other alternative is to save the columns with the .dwg filenames and the folder names, from Excel, as a .csv file and make a batch to process that. Let me know if that is preferable.






          Salmon Trout

          • Guest
          Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
          « Reply #7 on: March 29, 2019, 02:37:21 PM »
          The other alternative is to save the columns with the .dwg filenames and the folder names, from Excel, as a .csv file and make a batch to process that.



          CSV file looks like this (I called it MyFile.csv for this exercise):

          Code: [Select]
          FOLDER,DRAWING
          Product 0021,765-245.dwg
          Product 0107,963-541.dwg
          Product 0956,524-890.dwg
          Product 0584,769-527.dwg
          Product 0022,835-245.dwg
          Product 0096,963-523.dwg
          Product 0021,192-879.dwg
          Product 1584,769-524.dwg

          Batch file

          Code: [Select]
          @echo off
          for /f "skip=1 tokens=1-2 delims=," %%A in ('type MyFile.csv') do (
          if not exist "%%A" md "%%A"
          echo Moving file "%%B" to folder "%%A"
          move "%%B" "%%A"
          )
          echo Finished
          pause

          Salmon Trout

          • Guest
          Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
          « Reply #8 on: March 31, 2019, 02:44:10 AM »
          A third possibility is to use Visual Basic Script (which is on all Windows) to directly extract the data from the Excel spreadsheet, and I have been working on that, but as this thread has gone silent, I am not inclined to spend any more time on it. A fourth would be a VBA macro, (but likewise).

          Biscuit

            Topic Starter


            Greenhorn

            • Experience: Familiar
            • OS: Unknown
            Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
            « Reply #9 on: April 02, 2019, 02:17:10 AM »
            Many Many Thanks for your response looks very interesting Sorry I didn't reply earlier I took "She Who Must be Obeyed" away for a long weekend with strict instructions to leave the laptop begind  :(

            Anyway will try tonight

            Thanks Alot

            Biscuit

            Biscuit

              Topic Starter


              Greenhorn

              • Experience: Familiar
              • OS: Unknown
              Re: Elegant Batch File to Make Folder and Copy 2 Specific file into
              « Reply #10 on: April 03, 2019, 01:25:55 AM »
              Many thanks Salmon Trout

              Yes that works fine, thanks alot, just think it's a shame that the MOVE command doesn't have an option to generate a folder if it doesn't exist.
              Your efforts much appreciatedd

              Biscuit