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

Author Topic: Help with a batch file to move based upon part of file name  (Read 6087 times)

0 Members and 1 Guest are viewing this topic.

TriDawg

    Topic Starter


    Starter

    • Experience: Familiar
    • OS: Windows 7
    Help with a batch file to move based upon part of file name
    « on: January 23, 2017, 07:36:29 AM »
    Hi all, I'm a hack sql programmer / web designer of sorts.  More of an Excel moderate/exp than a programmer.  I'm familiar with batch files and how to use them, but very unfamiliar with how to program them. 

    I need a batch file to perform the following move:

    From location:
    C:\Materials\Transfer File

    Filenames:
    V00128594 11-001 MINERALS.pdf
    V00128594 11-001 MICROS.pdf
    V00128598 11-1000 MICROS.pdf
    V00129476 11-001 MICROS.pdf
    V00131451 33-4872 MICROS.pdf
    V00131451 33-4872 MINERALS.pdf
    V00131451 33-4872 SPECTROPHOTOMETRY.pdf

    Results would look like this with Move To Filename Locations:
    C:\Materials\11's\11-001
    • V00128594 11-001 MINERALS.pdf
    • V00128594 11-001 MICROS.pdf
    • V00129476 11-001 MICROS.pdf

    C:\Materials\11's\11-1000
    • V00128598 11-1000 MICROS.pdf

    C:\Materials\33's\33-4872
    • V00131451 33-4872 MICROS.pdf
    • V00131451 33-4872 MINERALS.pdf
    • V00131451 33-4872 SPECTROPHOTOMETRY.pdf


    Our naming conventions are:
    Filename
    {LOT}" "{ITEM CLASS}"-"{ITEM NUMBER}" "{TEST RESULTS}.pdf

    Locations
    C:\Material\Transfer File  - files are stored til ready to transfer
    C:\Material\{ITEM CLASS}"'s"\{ITEM CLASS}"-"{ITEM NUMBER}\ - location where the files need to land, based upon filename

    I've located several examples in this forum that are very close, but I can't seem to figure out how to tell it the new location based upon the "33" before the dash, and adding an "'s" for the folder directory.  Thanks in advance for any help.

    Edit:
    My files always end in .pdf and all of the folders will always be pre-existing.

    « Last Edit: January 23, 2017, 07:49:16 AM by TriDawg »

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Help with a batch file to move based upon part of file name
    « Reply #1 on: January 23, 2017, 09:44:32 AM »
    With item numbers always the same you can add wildcard to ends of the filename such as *11-001*.* to your move instruction and this way anything conforming to any file name that contains 11-001 in it will go to the move destination. **Only tricky part would be if you had a part number of say 11-0018. It would grab this and move this to that location based on matching 11-001

    If you only have a few part numbers it wouldnt take much time to make this happen, if you have many part numbers and need to have the xx-xxxx read in and dynamically make folders for the item numbers and then move those files to those specific folders then a batch guru would need to help here beyond that of myself because it goes above my skills in batch.

    Example of a move with the wildecards at the ends would be:

    Quote
    MOVE "C:\Materials\Transfer File\*11-001*.*" C:\Materials\11's\11-001

    This is only for the 11-001 for the others you would need a line for say 11-1000 and 33-4872 such as a batch with:
    Quote
    MOVE "C:\Materials\Transfer File\*11-001*.*" C:\Materials\11's\11-001
    MOVE "C:\Materials\Transfer File\*11-1000*.*" C:\Materials\11's\11-1000
    MOVE "C:\Materials\Transfer File\*33-4872*.*" C:\Materials\33's\33-4872

    *Note: Untested but last time I did a similar process of placing a target index within 2 wildcards at either end it worked for me. The .* will grab all file types with the match. If you only want pdf's you can change it to

    Code: [Select]
    MOVE "C:\Materials\Transfer File\*11-001*.pdf" C:\Materials\11's\11-001
    MOVE "C:\Materials\Transfer File\*11-1000*.pdf" C:\Materials\11's\11-1000
    MOVE "C:\Materials\Transfer File\*33-4872*.pdf" C:\Materials\33's\33-4872

    If your dealing with a few hundred or thousand item numbers then maybe a batch guru can assist with making this a smaller batch that might be able to work from a text file with a list of part numbers and step down through the listing to handle all files that match the xx-xxxx item number range. If the item numbers are ever different than xx-xxxx for maximum digits then the largest part number should be shared so that they know to target - as the part number identifier and read in 2 characters prior to - and 4 characters after - or more than that to set the scope I am thinking. If I was going to make a complicated batch file I am thinking that is the direction i would go would be to read in part numbers from a text file list and then those that match xx-xxxx for *xx-xxxx*.pdf will be moved to appropriate folders.

    Quote
    I've located several examples in this forum that are very close, but I can't seem to figure out how to tell it the new location based upon the "33" before the dash, and adding an "'s" for the folder directory.  Thanks in advance for any help.

    If you already have a batch file that you got partially working you can share it here and we can assist with that too.


    TriDawg

      Topic Starter


      Starter

      • Experience: Familiar
      • OS: Windows 7
      Re: Help with a batch file to move based upon part of file name
      « Reply #2 on: January 23, 2017, 10:04:44 AM »
      Thanks for the idea, but yes we'll probably have several hundred files to move per day, and we have over 4000 different items with inventory constantly growing.  Trying to figure out an automated way to do this, where the lady that renames all the files can just double click something, like a batch file, and it will do all the tedious file transfers for her...we may just end up buying RoboBasket if we can't figure any other way.  We just don't need a full feature software package to only perform one function.  :(

      DaveLembke



        Sage
      • Thanked: 662
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Help with a batch file to move based upon part of file name
      « Reply #3 on: January 23, 2017, 10:28:33 AM »
      What is the range for the part numbers do all conform to xx-xxxx or would it need to look in a scope of xxx-xxxx or some other scope? The good thing is that in your files the ( - ) is the unique identifier so you can tell it to read in the first 2 characters before ( - ) and 4 characters after ( - ) I am thinking and go that route. If you had a text file with 1 item number per line and the batch can step through that list, it can then process I am hypothetically thinking all part numbers from the target location. The xx- (leading) part number can be used for pathing to group all 33- into 33's

      This unfortunately exceeds my skills in batch though. So were gonna need another member here to assist with this one other then myself, but at least we are information gathering at this point and throwing out there some ideas on how this could be completed at this point so they have a head start on what you need by knowing the range that your part numbers will conform to such as they never exceed xx-xxxx or will some go beyond that for instead of 2 by 4 its 3 by 5 digits which are handled not as digits but characters so alpha would even work if you had a 11-103Y

      TriDawg

        Topic Starter


        Starter

        • Experience: Familiar
        • OS: Windows 7
        Re: Help with a batch file to move based upon part of file name
        « Reply #4 on: January 23, 2017, 10:58:33 AM »
        Attaching a small .zip of the Materials folder, with sample files that anyone can feel free to work with, if it will help.  This is our exact directory bones, and a sample of files in the Transfer folder that were just named today.

        In response to Dave, all our items are format code xx-xxx or xx-xxxx.  The first two numbers distinguish our item class (vitamin, mineral, protein, etc.), while the last 3 or 4 digits, after the dash represent that particular material.

        Example
        11-001  =  Vitamin A
        11-003  =  Vitamin B12
        33-149  =  Liver Powder (33's being Proteins)
        33-001  =  Cranberry Juice Powder


        [attachment deleted by admin to conserve space]

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Help with a batch file to move based upon part of file name
        « Reply #5 on: February 11, 2017, 11:39:01 PM »
        Tested with your provided file.

        Code: [Select]
        @echo off

        REM Assign the working directory to a variable
        set "from=C:\Materials\Transfer File"

        REM Change the current directory and store the previous folder for use by the POPD command.
        PUSHD "%from%"

        REM Parse the output of the DIR command
        REM File name is broken apart into tokens by the hyphen and space.
        REM %%G will equal the first part of the file name up to the first space
        REM %%H will equal the second part of the file name: space to hyphen
        REM %%I will equal the third part of the file name: hyphen to space.
        REM %%J will be the remainder of the file name.
        FOR /F "tokens=1-3* delims=- " %%G IN ('dir /B /a-d *.pdf') DO (
        IF NOT EXIST "..\%%H's\%%H-%%I" MD "..\%%H's\%%H-%%I"
        MOVE "%%G %%H-%%I %%J" "..\%%H's\%%H-%%I\"
        )
        REM Change back to the previous directory.
        popd
        pause