File sorting...

D

Don Y

Guest
This is a little script I threw together to *place* files on
a medium (thumb drive) in a particular order. This was found to
be necessary as many \"dumb\" devices don\'t bother trying to sort
folder contents but, rather, present them in the order encountered
on the medium.

E.g., if you copied Dino, Fred, BammBamm, Wilma, BabyPuss, Betty, Pebbles
and Barney onto a medium USED IN SUCH A SYSTEM, they would appear as:
Dino
Fred
BammBamm
Wilma
BabyPuss
Betty
Pebbles
Barney
instead of the more typically expected:
BabyPuss
BammBamm
Barney
Betty
Dino
Fred
Pebbles
Wilma

In my case, I organize content in a hierarchival structure:
Artist\\Album\\Disc/LP\\Title
But most devices will either expose that or ignore it,
completely!

As there are limits on the number of directory entries at the
\"root\" of a volume, adding a superfluous intermediate folder
may be helpful in some cases.

This also works-around windows policies that may prevent
\"user\" access to media roots!

[ISTR there is a problem in this implementation but it doesn\'t
readily manifest for me so I\'ve not chased it down. <shrug>
Obviously, some assumption I make that is unfounded!]

I\'ve got a road trip scheduled for tomorrow and have updated
my \"media collection\" in preparation -- hence the reason I\'ve
(re)encountered this, now.

Hopefully it will solve someone else\'s problem (?) (watch for
unintended line breaks from cut and paste!)

---8<---8<---8<---
@echo off

REM The purpose of this DOS script is to (recursively) reorder the contents of
a given
REM folder using criteria indicated as (optional) arguments to the script. It
finds
REM use with certain devices (e.g., media players, automobile infotainment
systems) that
REM naively access the contents of a folder in its \"natural order\" --
irrespective of
REM file names, dates, sizes or other criteria that may be of interest to the
user.

REM The invoking syntax is:
REM <script> [<folder>] [/<flags>]
REM where:
REM <script> name of this script (it may have been renamed!)
REM <folder> target folder to be reordered
REM <flags> switches controlling the sort order:
REM N name
REM D date
REM S size
REM [ ] enclose optional components

REM The sort criteria are applied in the order specified; i.e., earlier flags
have more
REM influence than subsequent flags. The current codepage will determine how
names are
REM sorted.

REM !!!FIXME!!! Liar! Order is currently hardwired! :>

setlocal

REM N.B. Messages emitted without %NAME% prefix are purely diagnostic; elide
at release.

set NAME=%~n0.bat

set LOCATION=%~f0
set LOCATION=%~dp0

if not [%2]==[] (
echo %NAME%: Too many parameters.
goto DIE
)

set TARGET=\"%1\"

if [%TARGET%]==[\"\"] set TARGET=%LOCATION%

echo %NAME%: Operating on \'%TARGET%\'

rem :NAMEHOLDING
rem set HOLDING=DeAdBeEf-%RANDOM%.tmp
rem if exist \"%HOLDING%\" GOTO :NAMEHOLDING
rem This last bit is icky! Hope HOLDING is initially sufficiently unique!

set HOLDING=DeAdBeEf.tmp

set ORDER=/Ogneds


pushd %TARGET% >nul 2>&1 || (
echo %NAME%: Folder \'%TARGET%\' not found.
goto DIE
)

for /r . %%d in (.) do (
echo Processing directory \'%%d\'
cd \"%%d\" || (
echo %NAME%: Directory \'%%d\' inaccessible.
goto DIE
)

if exist \"%HOLDING%\" (
echo %NAME%: \'%HOLDING%\' already exists.
goto DIE
)
md \"%HOLDING%\" || (
echo %NAME%: Can\'t create \'%HOLDING%\' subdirectory under \'%%d\'.
goto DIE
)

for /f \"usebackq delims==\" %%c in (`dir /b`) do (
rem if not \"%%c\"==\"%HOLDING%\" move \"%%c\" \"%HOLDING%\" >nul
set DOIT=\"YES\"
if not [%%c]==[%HOLDING%] if not [%%c]==[%NAME%] move \"%%c\" \"%HOLDING%\" >nul
)

cd \"%HOLDING%\"
for /f \"usebackq delims==\" %%c in (`dir /b %ORDER%`) do (
echo Placing \'%%c\'
move \"%%c\" .. >nul
)
cd ..

rd \"%HOLDING%\"
)
popd
echo Fin!


:DIE
---8<---8<---8<---
 

Welcome to EDABoard.com

Sponsor

Back
Top