Results 1 to 10 of 10

Thread: Dos batch file to sort files based on file names.

  1. #1
    Jon Osborn Guest

    Dos batch file to sort files based on file names.

    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-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\Little Rock






    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


  2. #2
    Tom Lavedas Guest

    Re: Dos batch file to sort files based on file names.

    The approach you posted seems needlessly complex. I would think
    something like this would get the job done ...

    @echo off
    set source=D:\Contents\Cherrypick_test
    set target1=D:\contents\lost_and_found folder
    set target2=D:\Contents\Cherrypick_test
    :: List of file types
    set types=Jonesboro;"Little Rock";Dallas;Houston;^
    :: Move matching files
    for %%a in (%types%) do (
    move "%source%\*%%~a*.m2t" "%target2%\%%~a")
    :: Whatever's left move to lost and found
    move "%source%\*.m2t" "%target1%"

  3. #3
    Pegasus \(MVP\) Guest

    Re: Dos batch file to sort files based on file names.

    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:
    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.

  4. #4
    Jon Osborn Guest

    Re: Dos batch file to sort files based on file names.

    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"

    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.

  5. #5
    Pegasus \(MVP\) Guest
    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.

    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

  6. #6
    Tom Lavedas Guest

    Re: Dos batch file to sort files based on file names.

    The use of 'types' is arbitrary. It is just the name of another
    environment variable, like source and target1. I used the 'types'
    variable to contain a semicolon delimited list of name variations you
    specified in your post.

    The FOR statement parses the list at the semi colons and provides them
    one at a time to the statement after the DO. That's the heart of the
    solution. You can see this (with or without files present) by putting
    ECHO in fornt of the MOVE part as part of the FOR statement ...

    for %%a in (%types%) do (
    ECHO move "%source%\*%%~a*.m2t" "%target2%\%%~a")

    This will show how the statement is repeated with the %%a replaced by
    the items in the list. The tilde (~) acts to remove any parentheses
    around the string in the replacement. BTW, this argument is case
    sensitive, so that %%a is NOT the same as %%A. For more information
    on the FOR's operation, type FOR/? at a command prompt.

  7. #7
    Al Dunbar Guest

    Re: Dos batch file to sort files based on file names.

    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.

  8. #8
    Join Date
    Jun 2009
    I myself have a similiar conundrum, trying to organize thousands of short sound clipswith a name like "20090513_000700.aif" (Date and Time recorded). I tried modifying code posted earlier as so:

    @echo off
    set source=C:\a2w1
    set target1=C:\a2w1\lf
    set target2=C:\a2w1\test
    :: List of file types
    set types=20090513_00;20090513_01;20090513_02;20090513_03;^
    :: Move matching files
    for %%a in (%types%) do (
    move "%source%\*%%~a*.aif" "%target2%\%%~a")
    :: Whatever's left move to lost and found
    move "%source%\*.aif" "%target1%"

    Somewhere along the way I obviously went wrong as all the files are
    moved to the "lf" ie lost and found folder. What I imagined was that
    this would move all the recordings with the name 20090513_00**** to a
    folder, 20090513_01**** etc, basically sorting them by their hours
    during the day.

    This batch processingis far over my head, could you lend some guidance?

    I found a solution via using AutoHotKey and a simple script. Message/Email me if curious. Many thanks to the forum, his is a great resource.

  9. #9
    Pegasus [MVP] Guest
    What is the purpose of the ~a bit in *%%~a*.aif?

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

  10. #10
    Pegasus [MVP] Guest

    Dos batch file to sort files based on file names.

    This was a rhetoric question to the OP.

Similar Threads

  1. Windows Batch file to output directory names and size to txt file
    By m2thearkus in forum Software Development
    Replies: 6
    Last Post: 16-07-2010, 12:04 AM
  2. Deleting files equal to 0 in a batch file.
    By Luis-Fernando in forum Operating Systems
    Replies: 5
    Last Post: 27-03-2010, 08:04 PM
  3. Batch File to Move or Copy Files
    By Pratim in forum Windows Software
    Replies: 6
    Last Post: 21-03-2010, 09:32 PM
  4. Creating a batch file to launch programs based on time
    By rblanda in forum Tips & Tweaks
    Replies: 3
    Last Post: 31-12-2009, 10:19 PM
  5. Can't change file association for DOS batch files
    By pushpendra in forum Windows XP Support
    Replies: 6
    Last Post: 11-03-2009, 12:10 AM

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Page generated in 1,713,164,385.00443 seconds with 17 queries