Dos batch file to sort files based on file names.

Discussion in 'Scripting' started by Jon Osborn, May 19, 2008.

  1. Jon Osborn

    Jon Osborn Guest

    We have a program creates reports for each of our offices. The files are
    always named *_location.m2t where the * is a always random characters. We
    would like to be able to sort the files in to different Directorys.


    Here are some excate Eamples

    The Folder D:\Contents\Cherrypick_test has these files

    Job-01C8A968-8CB4110A_CurTMGPCD Jonesboro.m2t

    Job-01C8A969-546C46C2_CurTMGPCD Little Rock.m2t

    Job-01C8A96F-4A222142_CurTMGPCP Dallas.m2t

    Job-01C8A96F-D0AE056A_CurTMGPCP Houston.m2t

    Job-01C8A970-290AE2EA_CurTMGPCP Jonesboro.m2t

    Job-01C8A970-E8E940DA_CurTMGPCPLittleRock.m2t

    Job-01C8A9FC-AEE0937E_CurTMGPCP Memphis.m2t

    Job-01C8A9FF-4F976F6A_CurTMGPCP Texarkana.m2t

    Job-01C8AA02-FC4EF59A_CurTQGPCP Authorized Resellers.m2t

    Job-01C8AA07-BA24389A_CurTQGPCP General Resellers.m2t



    And we want the files sorted into these folders

    D:\contents\Jonesboro

    D:\contents\Little Rock

    D:\contents\Dallas

    D:\contents\Houston

    D:\contents\memphis

    D:\contents\Authorized

    D:\contents\General

    And the if location is not there move it into the D:\contents\lost_and_found
    folder ( I now the lost and found will have to be the last command run)



    _______________________________________________________
    I found a script (in this newsgroup) that will sort file if a specific text
    is in the file (below) and I like how it works but if could be modified to
    work on files names it would be perfect. If figure I could change the Set
    String line and do a different loop for each location. I am sure the boss
    is going to add more to it once it starts to work. I know enough about
    batching to create a simple ones or modify some batches. I figure there is
    a different command other than find for file names but I am not sure what.
    Again if this could be modifled to work with filenames it would be perfect
    for us. Of course these are just test Directorys so I will change them to
    production once we get the script working. I know the lost and found part
    will not work, but I can cut that out and run it as the last command.



    @echo off

    setlocal enabledelayedexpansion

    set string=Dallas

    set source=D:\Contents\Cherrypick_test

    set target1= D:\contents\lost_and_found folder

    set target2=D:\Contents\Cherrypick_test\Dalls

    REM if not exist "%target1%" md "%target1%"

    REM if not exist "%target2%" md "%target2%"



    for %%a in ("%source%\*.m2t") do (

    set target=%target2%

    find /i "%string%" "%%a" nul && set target=%target1%

    echo move "%%a" "!target!"

    )



    set string=Jonesboro

    set source=D:\Contents\Cherrypick_test

    set target1= D:\contents\lost_and_found folder

    set target2=D:\Contents\Cherrypick_test\Jonesboro



    for %%a in ("%source%\*.m2t") do (

    set target=%target2%

    find /i "%string%" "%%a" nul && set target=%target1%

    echo move "%%a" "!target!"

    )





    Thanks for any help

    Jon
     
    Jon Osborn, May 19, 2008
    #1
    1. Advertisements

  2. Job-01C8A968-8CB4110A_CurTMGPCD Jonesboro.m2t
    Job-01C8A969-546C46C2_CurTMGPCD Little Rock.m2t
    Job-01C8A96F-4A222142_CurTMGPCP Dallas.m2t
    Job-01C8A96F-D0AE056A_CurTMGPCP Houston.m2t
    Job-01C8A970-290AE2EA_CurTMGPCP Jonesboro.m2t
    Job-01C8A970-E8E940DA_CurTMGPCPLittleRock.m2t
    Job-01C8A9FC-AEE0937E_CurTMGPCP Memphis.m2t
    Job-01C8A9FF-4F976F6A_CurTMGPCP Texarkana.m2t
    Job-01C8AA02-FC4EF59A_CurTQGPCP Authorized Resellers.m2t
    Job-01C8AA07-BA24389A_CurTQGPCP General Resellers.m2t
    D:\contents\Jonesboro
    D:\contents\Little Rock
    D:\contents\Dallas
    D:\contents\Houston
    D:\contents\memphis
    D:\contents\Authorized
    D:\contents\General
    The batch file you quote came out of my own kitchen.
    What you want to do can be done but we first need to
    remove some ambiguities.

    You state "And we want the files sorted into these folders".
    I doubt very much that you mean "sorted". To sort means
    "to arrange in a specific order". Do you perhaps mean
    "And we want the files MOVED into these folders"?

    Here is one of the examples you gave: The file
    Job-01C8A969-546C46C2_CurTMGPCD Little Rock.m2t
    is supposed to be sorted/moved to this folder:
    D:\contents\Little Rock
    Are you trying to say that the batch file should remove
    the first 32 characters of the file name (i.e. the
    "Job-01C8A969-546C46C2_CurTMGPCD " bit, then
    grab the remaining characters of the file name up to the
    full stop, then use this string for the target folder?

    If this is correct, why do you move the file
    Job-01C8AA07-BA24389A_CurTQGPCP General Resellers.m2t
    to the "General" folder? This example does not follow
    your rule!

    And what about this example:
    Job-01C8A970-E8E940DA_CurTMGPCPLittleRock.m2t
    It lacks the space to the left of the word "Little". It also lacks
    the space between "Little" and "Rock".

    What should happen if the file to be moved already exists
    in the target directory?

    What is this "Lost and found" bit you're referring to?

    Can your file names contain any of the following "poison"
    characters? % ^ & ( ) = ! ' ". Lots of file names do!

    These are design questions you need to think about very
    carefully BEFORE you start any coding effort.
     
    Pegasus \(MVP\), May 19, 2008
    #2
    1. Advertisements

  3. Jon Osborn

    Jon Osborn Guest

    Yes I did moved. I am not worried about the order once the move in to thier
    correct order.
    This batch file doesn't need to worry removing characters. I have already
    created a bat that should strip the fist 22 Chars (ex
    Job-01C8A969-546C46C2_) leaving what is left of the file name. This batch
    file just need to move any file with "Little Rock" in to the "Little Rock"
    Folder.
    This one is Policital in the company, they are part of one of the locations
    but they want thiers files sorted out "special" for them.
    I know, I am try to get the report write to remove all spaces from her file
    names. I figure if I can't we don't have any ither offices with little
    their name so I would use that as my name to sort from.
    I will use the Move /Y command so they will be overwritten.
    Incase a new report is created and it is not labeled for a specific
    location. I want to know where to look when Management asks about. I Also
    wnat to keep the soruce folder clean of files.
    No. They will never have any of those charaters.
    Thanks,
    Jon
     
    Jon Osborn, May 19, 2008
    #3
  4. If you already have the code to extract the destination
    folder then you should post it so that it can be properly
    integrated into the overall scheme of things. Alternatively
    you could ask some specific questions about things that
    are unclear at the moment.
     
    Pegasus \(MVP\), May 19, 2008
    #4
  5. Jon Osborn

    Jon Osborn Guest

    I didn't know about the set types command.

    This works fine.

    Quick question what is the %%A do in the for statement. I know it is an
    increament counter but how does it work? How does it know how many time to
    run?

    Thanks,
    Jon
     
    Jon Osborn, May 19, 2008
    #5
  6. Jon Osborn

    Al Dunbar Guest

    The OP undoubtedly worked in a post office, where letters are sorted, not
    into some sort of alphanumerical order that those of us with programming
    backgrounds might think, but into separate slots, each corresponding to a
    postal worker's "walk" or route.

    /Al
     
    Al Dunbar, May 22, 2008
    #6
  7. What is the purpose of the ~a bit in *%%~a*.aif?
     
    Pegasus [MVP], Jun 16, 2009
    #7
  8. Jon Osborn

    Al Dunbar Guest

    The tilde causes any enclosing double quotes to be stripped if they would
    otherwise exist in "%%a".

    /Al
     
    Al Dunbar, Jun 17, 2009
    #8
  9. This was a rhetoric question to the OP.
     
    Pegasus [MVP], Jun 17, 2009
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.