Discussion:
Batch sort based on variable name
(too old to reply)
StephenTS
2009-07-12 01:32:47 UTC
Permalink
Based on:
a) files named in pattern of "filename_by_name.ext"
b) where "_by_" is common to all files
c) "name" needs to be created as a folder name
d) files matching that "_by_name" to be moved into appropriate folder
e) length of "filename" is variable
f) list of "name" names is variable and unknown

Current setup in batch file:
md a
md b
md ...
md n
md ...
move *_by_a*.* a
move *_by_b*.* b
move *_by_...*.* ...
move *_by_n*.* n
move *_by_...*.* ...

From there then manually:
Create subdirectory within n
Find all files with name in them...
Move files

Need something to:
md name
move *_by_name.* name
[repeat for next file]


I realize this is probably an IF / THEN / FOR type issue as well as
needing LOOP in it but I've been trying to figure this out on my own
from time to time for over a year to no avail. Again, the only thing
common in the filenames is "_by_" being contained within and that
distance in being variable so I can't really use SORT based on n in.
Also as mentioned above, the list of names is not known prior therefore
an answer such as 'this previous thread'
(http://forums.techarena.in/server-scripting/971258.htm) won't suffice,
the list needs to be retrieved within the directory the batch is run.

Thank you in advance.
--
StephenTS
------------------------------------------------------------------------
StephenTS's Profile: http://forums.techarena.in/members/113820.htm
View this thread: http://forums.techarena.in/server-scripting/1212836.htm

http://forums.techarena.in
contrex
2009-07-12 08:09:40 UTC
Permalink
So the pattern is invariably:

some text considered to be the filename_by_some text considered to be
the name.ext

Do I have that right? The underscore, by, underscore being the
unchanging part?

Initially, all the files to be processed are in the same folder, and
the 'name' subfolders are to be under that folder?

so that

Monthly figures Jan-Mar-09_by_Jim Jones.xls

requires that a folder called "Jim Jones" be created (if it doesn't
already exist) and the file moved into that folder?

and

Expenses claim October 2008_by_JASmith.doc

requires that a folder called "JASmith" be created (if it doesn't
already exist) and the file moved into that folder?

and

LaundryList_by_Asghar

requires that a folder called "Asghar" be created (if it doesn't
already exist) and the file moved into that folder?

Query: Is there any possibility that either the 'filename' or the
'name' part can contain the underscore character?
contrex
2009-07-12 08:38:24 UTC
Permalink
Post by contrex
Query: Is there any possibility that either the 'filename' or the
'name' part can contain the underscore character?
Furthermore, can any part of the 'filename' or 'name' contain a dot?
contrex
2009-07-12 09:02:39 UTC
Permalink
@echo off

REM note use of delayed expansion
setlocal enabledelayedexpansion

REM Create some test files
echo hello world>"a test file_by_Roberto Succo.txt"
echo hello world>"Monthly figures_by_RabbitQPie.xls"
echo hello world>"Monthly figures Jan-Mar-09_by_Jim Jones.xls"
echo hello world>"Expenses claim October 2008_by_JASmith.doc"
echo hello world>"Travel Report 328_by_RED Singh.doc"
echo hello world>"LaundryList_by_Asghar.abc"

REM Outer loop
REM Process files containing _by_ in name
REM use /b switch to get just filenames
REM /on is DIR switch: sort by name
REM consult DIR documentation for other options
for /f "delims==" %%A in ( ' dir /b /on *_by_* ' ) do (
REM Put whole file name into variable
set file=%%A
REM Obtain file name minus extension
REM assumes ONLY ONE DOT in filename+extension
set barename=%%~nA
echo file: !file!

REM Inner loop
REM Using underscore as delimiter, split each name minus extension
REM into 3 tokens. Token no. 3 is the 'name'.
REM Assumes underscore occurs only twice, before and after 'by'
for /f "tokens=1-3 delims=_" %%B in ("!barename!") do (

REM Put 'name' into variable
set name=%%D

REM Create folder if it does not exist
REM remove 'echo' when satisfied
echo if not exist "!name!" md "!name!"

REM Move file
REM remove 'echo' when satisfied
echo move "!file!" "!name!"

echo.
)

)

Loading...