Batch Directory Auto-Sorter

Gigabyte / M52s-s3p
April 3, 2009 at 00:23:14
Specs: Microsoft Windows XP Professional, 2.31 GHz / 3582 MB
Hey,
I have written a small program (in batch) that
makes a folder containing contact information
about a person.
I also wrote another one that deletes the
information.
Now i want to be able to sort them into folders
suiting their last names first letter.
eg.
Smith starts with S and should be moved to
Folder P-T.
The folders i want the last names to
automaticly move to are;
A-E, F-J, K-O, P-T, U-Z.

I am not even remotely sure were to start
looking at how to do this.
If anyone could please give me some advice or
examples you would make it possible for me
to finish this. Preferably in batch but if there is
a way to do it with VBS (No prompts) i guess
that would be alright too.
Thanks,
LEoH
(Needs to work on XP)


See More: Batch Directory Auto-Sorter

Report •


#1
April 6, 2009 at 05:10:18
I know I probably don't have enough experience with batch to answer questions but here goes for an attempt or stuff for thought

use dir function to build a list of your current directorys then create a for loop to go through and using a few if statments combined with goto's you should be able to make it sort the folders into there correct new sections

Ill keep an eye on this hopefully I can help more =)


Report •

#2
April 8, 2009 at 03:15:00
Sorry for the delay in replying.
I installed windows xp pro 64-bit on another partition, And i lost access to the internet on it and just never got around to using the grub bootloader. All set up now.

I completly agree with your idea... But... How would i implement that into my script?

So far, the first thing my program does is this.

@ECHO OFF
COLOR F2

:1
CD %UserProfile%
CD My Documents
if exist "MyPeople" (
CD MyPeople
GOTO 2
) else (
MKDIR MyPeople
)
CD MyPeople

:2
if exist "A-E" (
GOTO 3
) else (
MKDIR A-E
)

:3
if exist "F-J" (
GOTO 4
) else (
MKDIR F-J
)

:4
if exist "K-O" (
GOTO 5
) else (
MKDIR K-O
)

:5
if exist "P-T" (
GOTO 6
) else (
MKDIR P-T
)

:6
if exist "U-Z" (
GOTO 7
) else (
MKDIR U-Z
)

:6
if exist "Manager" (
GOTO 7
) else (
MKDIR Manager
)

Of course, like i said, it also creates and deletes folders containing information to the MyPeople directory. But it wasn't necessary to include these scripts... i don't think.
I have an idea of how i could do what you means but i think i have got the syntax incorrect.


CD CD %UserProfile%/My Documents/MyPeople
DIR /s /B >>People.LEO
PAUSE
find /v "A-E" "F-J" "K-O" "P-T" "U-Z" < People.LEO > People.LEO

Is what i attempted. This doesn't even work. Is there a way to sort it whilst ignoring the root folders?
And even then, how could i detect first letters? The dir commands lists files including their directory. And this has to be able to be run off a USB. (Hence %UserProfile%)

Thanks for your help,

LEoH


Report •

#3
April 8, 2009 at 03:31:08
I'm a little unclear on a few things.

1. What folder is the information located in(the people folder?)

2. Does each person have a file or directory that you want to move in a sorted fashon?

3. You say that it should be sorted according to the person's last name, how can this information be obtained? Is the name of the file/folder or their contents?

The script you posted above, I just couldn't help myself:

@ECHO OFF
COLOR F2

CD %UserProfile%\My Documents
if not exist "MyPeople" MKDIR MyPeople
CD MyPeople
for %%a in ("A-E" "F-J" "K-O" "P-T" "U-Z" "Manager") do if not exist %%a md %%a


Report •

Related Solutions

#4
April 8, 2009 at 03:46:01
haha,
Hey Judago.
I knew someone would correct that.

Ok, ill explain myself a little better.
Actually... Ill just post my entire script...

@ECHO OFF
COLOR F2

:: Person Delete
:: LEoH
:: Person Manager Include

SET STT=%time%

CD..
CLS

CLS
set /p name=First Name :
for %%a in ("&" ">" "<" "|" "^") do call set "name=%%name:%%~a=%%"
set fn=%name:~0,1%
CLS
set /p lname=Last Name :
for %%a in ("&" ">" "<" "|" "^") do call set "lname=%%lname:%%~a=%%"
for /f "tokens=1,2 delims=-" %%a in ("%lname%") do (
set lname=%%a
set pblname=%%b
)
set ln=%lname:~0,1%
if defined pblname set pb=%pblname:~0,1%
CLS
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
call set name=%%name:%%a=%%a%%
call set lname=%%lname:%%a=%%a%%
)

for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
call set fn=%%fn:%%a=%%a%%
call set ln=%%ln:%%a=%%a%%
if defined pb call set pb=%%pb:%%a=%%a%%
)

set name=%fn%%name:~1%
if not defined pb (set lname=%ln%%lname:~1%) else set lname=%ln%%lname:~1%-%pb%%pblname:~1%
CLS


if exist "%lname%" (
CD %lname%
) else (
MKDIR %lname%
CD %lname%
)

if exist "%name%" (
CD %name%
) else (
MKDIR %name%
CD %name%
)

MKDIR Contact
CD Contact

MKDIR Home
CD Home
ECHO.____________________________________________ >> Home.LEO
ECHO. >> Home.LEO
ECHO.Home >> Home.LEO
ECHO.____________________________________________ >> Home.LEO
ECHO. >> Home.LEO
ECHO.Street Adress :>> Home.LEO
ECHO.City :>> Home.LEO
ECHO.State :>> Home.LEO
ECHO.P/C :>> Home.LEO
ECHO.Country :>> Home.LEO
CD..

MKDIR General
CD General
ECHO.____________________________________________ >> Information.LEO
ECHO. >> Information.LEO
ECHO.General >> Information.LEO
ECHO.____________________________________________ >> Information.LEO
ECHO. >> Information.LEO
ECHO.Name : %name% %lname%>> Information.LEO
ECHO.Age :>> Information.LEO
ECHO.B/D :>> Information.LEO
ECHO. >> Information.LEO
ECHO.Home Adress :>> Information.LEO
ECHO.Country :>> Information.LEO
ECHO. >> Information.LEO
ECHO.Phone Numbers; >> Information.LEO
ECHO. Home :>> Information.LEO
ECHO. Personal :>> Information.LEO
ECHO. >> Information.LEO
ECHO.____________________________________________ >> Information.LEO
ECHO. >> Information.LEO
ECHO.Online >> Information.LEO
ECHO.____________________________________________ >> Information.LEO
ECHO. >> Information.LEO
ECHO.Primary Email :>> Information.LEO
ECHO.Secondary Email :>> Information.LEO
ECHO. >> Information.LEO
ECHO.Instant Messanger; :>> Information.LEO
ECHO. MSN :>> Information.LEO
ECHO. AOL :>> Information.LEO
ECHO. AIM :>> Information.LEO
ECHO. Yahoo :>> Information.LEO
ECHO. Gmail :>> Information.LEO
ECHO. >> Information.LEO
ECHO.IP Adress :>> Information.LEO
ECHO. >> Information.LEO
CD..

MKDIR School-Work
CD School-Work
ECHO. >> School-Work.LEO
ECHO.____________________________________________ >> School-Work.LEO
ECHO. >> School-Work.LEO
ECHO.School >> School-Work.LEO
ECHO.____________________________________________ >> School-Work.LEO
ECHO. >> School-Work.LEO
ECHO.School Name :>> School-Work.LEO
ECHO.School Phone :>> School-Work.LEO
ECHO.School Address :>> School-Work.LEO
ECHO.Year :>> School-Work.LEO
ECHO.Role(s) :>> School-Work.LEO
ECHO. >> School-Work.LEO
ECHO.____________________________________________ >> School-Work.LEO
ECHO. >> School-Work.LEO
ECHO.Work >> School-Work.LEO
ECHO.____________________________________________ >> School-Work.LEO
ECHO. >> School-Work.LEO
ECHO.Job Name :>> School-Work.LEO
ECHO.Work Phone :>> School-Work.LEO
ECHO.Work Address :>> School-Work.LEO
ECHO.Title :>> School-Work.LEO
CD..

CD..
MKDIR General
CD..
GOTO :EXIT


;INT
ECHO.An Error Has Occured. Please Make Sure You Have
ECHO.Not Renamed The Folder "Manager".
ECHO.And If You Have Return It To Its Original State.
PING 127.0.0.1 >NUL
PING 127.0.0.1 >NUL


:ERROR
SET ENT=%TIME%
RD %al%
DIR Manager >NUL
IF ERRORLEVEL 1 GOTO :INT
CD Manager
ECHO._____________________________>>logs.log
ECHO. >>logs.log
ECHO.Process Failed: >> logs.log
ECHO. Start time %STT% >> logs.log
ECHO. End Time %ENT% >> logs.log
ECHO._____________________________>>logs.log
ECHO.>>logs.log
CLS
ECHO.An Error Has Occured.
ECHO.
PING 127.0.0.1 >nul
CALL PM.bat

:EXIT
@ECHO ON
DIR Manager >NUL
IF ERRORLEVEL 1 GOTO :INT
SET ENT=%TIME%
ECHO._____________________________>>logs.log
ECHO. >>logs.log
ECHO.Process succeeded: >> logs.log
ECHO. Created %af% %al% >>logs.log
ECHO. Start time %STT% >> logs.log
ECHO. End Time %ENT% >> logs.log
ECHO._____________________________>>logs.log
ECHO.>>logs.log
CALL PM.bat

Thats old - A few weeks old. I just replaced the stuff i used to have with the stuff you taught me.
The top is almost identical to fortune teller.
And just so I don't get myself into a heated debate. The reason im not just using Type Source instead of all those echos is because i want it to be in as few files as possible. I make things harder for myself - I know...

The deletion tool is simple. All it does is removes that folder and all is sub folders.

Incase you dont understand what im trying to do...
Say the persons name was John Smith.
I want the directory John to be made in Smith and then Smith moved to the directory P-T. Just so it is a little better organized. I had half my friends in there and i was completely lost.
Thanks

LEoH


Report •

#5
April 8, 2009 at 05:28:40
Here is something that I *think* should do it, I have prefixed the /f and /l switches to xcopy so it will only list exactly what would be copied for testing purposes. It may also be possible to use the move command

It may however be an idea to test the name and switch into the appropriate directory before making subdirectories and doing work, so as to avoid copying the contents over later.

This is a bit quick and dirty, but I think it should work.

@ECHO OFF
pushd "drive:\path\of\dir\containing\last\name\dirs\to\be\sorted"
for /f %%a in ('2^>nul dir /b/ad a* b* c* e*') do xcopy "%%~fa" "drive:\path\a-e\%%~nxa" /e /i /-y /l /f
for /f %%a in ('2^>nul dir /b/ad f* g* h* i* *j') do xcopy "%%~fa" "drive:\path\f-j\%%~nxa" /e /i /-y /l /f
for /f %%a in ('2^>nul dir /b/ad k* l* m* n* o*') do xcopy "%%~fa" "drive:\path\k-o\%%~nxa" /e /i /-y /l /f
for /f %%a in ('2^>nul dir /b/ad p* q* r* s* t*') do xcopy "%%~fa" "drive:\path\p-t\%%~nxa" /e /i /-y /l /f
for /f %%a in ('2^>nul dir /b/ad u* v* w* x* y* z*') do xcopy "%%~fa" "drive:\path\u-z\%%~nxa" /e /i /-y /l /f
popd


Report •

#6
April 8, 2009 at 05:54:35
Wow.
Thanks, That looks complicated.
A few questions though.

(1) is the j* in the line for /f %%a in ('2^>nul dir /b/ad f* g* h* i* *j') do xcopy "%%~fa" "drive:\path\f-j\%%~nxa" /e /i /-y /l /f
Meant to be in reverse to the others?

(2) Is it cAsE sEnSiTiVe? (Answered this myself)

(3) What exactly am i meant to replace? I couldn't figure out what parts. I tried a few and i got an error message asking if it was a file or a directory...

(It may be obvious i have no idea about the pushd command...)

Thankyou very much for this. I spent hours a few days ago trying to do this and failed...

LEoH

EDIT: I just remembered. The "People" contain sub folders and files. Could that be interfering?

EDIT2: Aah, So is pushd like CD?

(Sorry about all the questions.)


Report •

#7
April 8, 2009 at 06:35:29
1. The *j is a typo, it should be j*.

2. "drive:\path\of\dir\containing\last\name\dirs\to\be\sorted" should be the directory that contains the information (i.e the smith folder). In "drive:\path\f-j\%%~nxa" drive:\path should be the folder that contains the a-e, f-j, ect. folders.

"EDIT: I just remembered. The "People" contain sub folders and files. Could that be interfering?

The only problem would be if the people folder contained folders other than what you want sorted. The commands I posted won't actually do anything except show you what would be copied so you can review the changes before commiting to them. To actually copy the folders the /l switch must be removed from each xcopy command, /l tells it to list what would be done if /l wasn't present.

The /i switch tells xcopy to assume that the destination is a directory without asking, so I'm not sure why you are being prompted.

Pushd is just a fancy cd command with a memory; typing popd will take you back to the directory you were in before pushd.


Report •

#8
April 8, 2009 at 06:50:56
Haha!
Awesome!
It works!
ありがとうございます!
I can't believe it!
"It may also be possible to use the move command"
I don't think it is... I tried replacing xcopy with move and it didn't work.
Thanks again,

LEoH


Report •

#9
April 16, 2009 at 04:40:22
Hey,
Sorry for posting in a slightly old topic, but seems my question refers to were your auto sorter moves the file, i didn't think it would matter too much.

It all works perfectly well but now i need to tell my other program, the deleter, were to look for the last name.

so far i have this;

@ECHO OFF
COLOR F2

CD..

:start
CLS
set /p name=First Name? :
for %%a in ("&" ">" "<" "|" "^" " ") do call set "name=%%name:%%~a=%%"
set fn=%name:~0,1%
CLS
set /p lname=Last Name? :
for %%a in ("&" ">" "<" "|" "^") do call set "lname=%%lname:%%~a=%%"
for /f "tokens=1,2 delims=-" %%a in ("%lname%") do (
set lname=%%a
set pblname=%%b
)
set ln=%lname:~0,1%
if defined pblname set pb=%pblname:~0,1%

CLS

for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
call set name=%%name:%%a=%%a%%
call set lname=%%lname:%%a=%%a%%
)

for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
call set fn=%%fn:%%a=%%a%%
call set ln=%%ln:%%a=%%a%%
if defined pb call set pb=%%pb:%%a=%%a%%
)

set name=%fn%%name:~1%
if not defined pb (set lname=%ln%%lname:~1%) else set lname=%ln%%lname:~1%-%pb%%pblname:~1%

cd..
cd iPeople


::This is were it needs the 'Locate-r' to be...
if exist "%lname%" (
CD %lname%
) else (
GOTO 1000
)
GOTO 2000

:1000
set et="Last Name"
goto error

:2000
if exist "%name%" (
rd /s /q "%name%"
) else (
GOTO 4000
)

:END
EXIT

:4000
set et="First Name"
goto error

:error
ECHO.An Error Has Occured.
ECHO.%et% May Be Incorrect...
PING 127.0.0.1 >nul
GOTO start

But of course, that automatically generates an error report.

It needs to be able to locate the appropriate directory automatically and after about 2 days of trying, i have not prevailed.

Thanks,

LEoH


Report •


Ask Question