Discussion:
Dos batch file to sort files based on file names.
(too old to reply)
Jon Osborn
2008-05-19 17:46:03 UTC
Permalink
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
Tom Lavedas
2008-05-19 18:26:19 UTC
Permalink
Post by Jon Osborn
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
{snip}
Post by Jon Osborn
Thanks for any help
Jon
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%"

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Jon Osborn
2008-05-19 20:45:54 UTC
Permalink
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
Post by Tom Lavedas
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%"
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Tom Lavedas
2008-05-19 21:29:08 UTC
Permalink
Post by Jon Osborn
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
Post by Tom Lavedas
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%"
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
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.

HTH,

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Pegasus (MVP)
2008-05-19 18:42:53 UTC
Permalink
Post by Jon Osborn
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
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
Post by Jon Osborn
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
Post by Jon Osborn
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 Directories 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.
Thanks for any help
Jon
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.
Jon Osborn
2008-05-19 20:08:43 UTC
Permalink
Post by Pegasus (MVP)
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"?
Yes I did moved. I am not worried about the order once the move in to thier
correct order.
Post by Pegasus (MVP)
Here is one of the examples you gave: The file
Job-01C8A969-546C46C2_CurTMGPCD Little Rock.m2t
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?
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.
Post by Pegasus (MVP)
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!
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.
Post by Pegasus (MVP)
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".
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.
Post by Pegasus (MVP)
What should happen if the file to be moved already exists
in the target directory?
I will use the Move /Y command so they will be overwritten.
Post by Pegasus (MVP)
What is this "Lost and found" bit you're referring to?
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.
Post by Pegasus (MVP)
Can your file names contain any of the following "poison"
characters? % ^ & ( ) = ! ' ". Lots of file names do!
No. They will never have any of those charaters.
Post by Pegasus (MVP)
These are design questions you need to think about very
carefully BEFORE you start any coding effort.
Thanks,
Jon
Pegasus (MVP)
2008-05-19 20:22:24 UTC
Permalink
Post by Jon Osborn
Post by Pegasus (MVP)
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"?
Yes I did moved. I am not worried about the order once the move in to
thier correct order.
Post by Pegasus (MVP)
Here is one of the examples you gave: The file
Job-01C8A969-546C46C2_CurTMGPCD Little Rock.m2t
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?
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.
Post by Pegasus (MVP)
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!
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.
Post by Pegasus (MVP)
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".
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.
Post by Pegasus (MVP)
What should happen if the file to be moved already exists
in the target directory?
I will use the Move /Y command so they will be overwritten.
Post by Pegasus (MVP)
What is this "Lost and found" bit you're referring to?
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.
Post by Pegasus (MVP)
Can your file names contain any of the following "poison"
characters? % ^ & ( ) = ! ' ". Lots of file names do!
No. They will never have any of those charaters.
Post by Pegasus (MVP)
These are design questions you need to think about very
carefully BEFORE you start any coding effort.
Thanks,
Jon
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.
Al Dunbar
2008-05-21 23:30:56 UTC
Permalink
Post by Jon Osborn
Post by Jon Osborn
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
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
Post by Jon Osborn
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
Post by Jon Osborn
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
Directories 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.
Thanks for any help
Jon
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"?
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
Post by Jon Osborn
Here is one of the examples you gave: The file
Job-01C8A969-546C46C2_CurTMGPCD Little Rock.m2t
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!
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.
aborker
2009-06-15 22:01:04 UTC
Permalink
Hi all,

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?
--
aborker
------------------------------------------------------------------------
aborker's Profile: http://forums.techarena.in/members/106111.htm
View this thread: http://forums.techarena.in/server-scripting/971258.htm

http://forums.techarena.in
Pegasus [MVP]
2009-06-16 07:06:55 UTC
Permalink
Post by aborker
Hi all,
I myself have a similiar conundrum, trying to organize thousands of
short sound clipswith a name like "20090513_000700.aif" (Date and Time
@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?
--
aborker
------------------------------------------------------------------------
aborker's Profile: http://forums.techarena.in/members/106111.htm
View this thread: http://forums.techarena.in/server-scripting/971258.htm
http://forums.techarena.in
What is the purpose of the ~a bit in *%%~a*.aif?
aborker
2009-06-16 19:28:29 UTC
Permalink
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.
--
aborker
------------------------------------------------------------------------
aborker's Profile: http://forums.techarena.in/members/106111.htm
View this thread: http://forums.techarena.in/server-scripting/971258.htm

http://forums.techarena.in
Al Dunbar
2009-06-17 04:55:23 UTC
Permalink
Post by Pegasus [MVP]
Post by aborker
Hi all,
I myself have a similiar conundrum, trying to organize thousands of
short sound clipswith a name like "20090513_000700.aif" (Date and Time
@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?
--
aborker
------------------------------------------------------------------------
aborker's Profile: http://forums.techarena.in/members/106111.htm
View this thread: http://forums.techarena.in/server-scripting/971258.htm
http://forums.techarena.in
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".

/Al
Pegasus [MVP]
2009-06-17 05:36:56 UTC
Permalink
Post by Al Dunbar
Post by Pegasus [MVP]
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".
/Al
This was a rhetoric question to the OP.
aborker
2009-06-16 19:28:29 UTC
Permalink
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.
--
aborker
------------------------------------------------------------------------
aborker's Profile: http://forums.techarena.in/members/106111.htm
View this thread: http://forums.techarena.in/server-scripting/971258.htm

http://forums.techarena.in
Loading...