Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , ,

Sponsored Links


Dos batch file to sort files based on file names.

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-05-2008
Jon Osborn
 
Posts: n/a
Dos batch file to sort files based on file names.

Sponsored Links
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




Reply With Quote
  #2  
Old 19-05-2008
Tom Lavedas
 
Posts: n/a
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;^
memphis;Authorized;General
:: 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%"
Reply With Quote
  #3  
Old 20-05-2008
Pegasus \(MVP\)
 
Posts: n/a
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:
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.
Reply With Quote
  #4  
Old 20-05-2008
Jon Osborn
 
Posts: n/a
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"
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.
Reply With Quote
  #5  
Old 20-05-2008
Pegasus \(MVP\)
 
Posts: n/a
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
run?
Reply With Quote
  #6  
Old 20-05-2008
Tom Lavedas
 
Posts: n/a
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.
Reply With Quote
  #7  
Old 22-05-2008
Al Dunbar
 
Posts: n/a
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.
Reply With Quote
  #8  
Old 16-06-2009
Member
 
Join Date: Jun 2009
Posts: 1
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;^
20090513_04;20090513_05;20090513_06
:: 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.
Reply With Quote
  #9  
Old 16-06-2009
Pegasus [MVP]
 
Posts: n/a
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".
Reply With Quote
  #10  
Old 17-06-2009
Pegasus [MVP]
 
Posts: n/a
Dos batch file to sort files based on file names.

This was a rhetoric question to the OP.
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Dos batch file to sort files based on file names."
Thread Thread Starter Forum Replies Last Post
Windows Batch file to output directory names and size to txt file m2thearkus Software Development 6 16-07-2010 12:04 AM
Deleting files equal to 0 in a batch file. Luis-Fernando Operating Systems 5 27-03-2010 08:04 PM
Creating a batch file to launch programs based on time rblanda Tips & Tweaks 3 31-12-2009 09:19 PM
Can't change file association for DOS batch files pushpendra Windows XP Support 6 11-03-2009 12:10 AM
Batch file to delete files dynamically freelancer24 Windows Server Help 3 08-02-2009 01:33 AM


All times are GMT +5.5. The time now is 07:08 AM.