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

#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 thoughtuse 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 sectionsIll 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 OFFCOLOR F2:1CD %UserProfile%CD My Documentsif exist "MyPeople" (CD MyPeopleGOTO 2) else (MKDIR MyPeople)CD MyPeople:2if exist "A-E" (GOTO 3) else (MKDIR A-E):3if exist "F-J" (GOTO 4) else (MKDIR F-J):4if exist "K-O" (GOTO 5) else (MKDIR K-O):5if exist "P-T" (GOTO 6) else (MKDIR P-T):6if exist "U-Z" (GOTO 7) else (MKDIR U-Z):6if 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/MyPeopleDIR /s /B >>People.LEOPAUSEfind /v "A-E" "F-J" "K-O" "P-T" "U-Z" < People.LEO > People.LEOIs 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 OFFCOLOR F2:: Person Delete:: LEoH:: Person Manager IncludeSET STT=%time%CD..CLSCLSset /p name=First Name : for %%a in ("&" ">" "<" "|" "^") do call set "name=%%name:%%~a=%%"set fn=%name:~0,1%CLSset /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=%%aset pblname=%%b)set ln=%lname:~0,1%if defined pblname set pb=%pblname:~0,1%CLSfor %%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%CLSif exist "%lname%" (CD %lname%) else (MKDIR %lname%CD %lname%)if exist "%name%" (CD %name%) else (MKDIR %name%CD %name%)MKDIR ContactCD ContactMKDIR HomeCD HomeECHO.____________________________________________ >> Home.LEOECHO. >> Home.LEOECHO.Home >> Home.LEOECHO.____________________________________________ >> Home.LEOECHO. >> Home.LEOECHO.Street Adress :>> Home.LEOECHO.City :>> Home.LEOECHO.State :>> Home.LEOECHO.P/C :>> Home.LEOECHO.Country :>> Home.LEOCD..MKDIR GeneralCD GeneralECHO.____________________________________________ >> Information.LEOECHO. >> Information.LEOECHO.General >> Information.LEOECHO.____________________________________________ >> Information.LEOECHO. >> Information.LEOECHO.Name : %name% %lname%>> Information.LEOECHO.Age :>> Information.LEOECHO.B/D :>> Information.LEOECHO. >> Information.LEOECHO.Home Adress :>> Information.LEOECHO.Country :>> Information.LEOECHO. >> Information.LEOECHO.Phone Numbers; >> Information.LEOECHO. Home :>> Information.LEOECHO. Personal :>> Information.LEOECHO. >> Information.LEOECHO.____________________________________________ >> Information.LEOECHO. >> Information.LEOECHO.Online >> Information.LEOECHO.____________________________________________ >> Information.LEOECHO. >> Information.LEOECHO.Primary Email :>> Information.LEOECHO.Secondary Email :>> Information.LEOECHO. >> Information.LEOECHO.Instant Messanger; :>> Information.LEOECHO. MSN :>> Information.LEOECHO. AOL :>> Information.LEOECHO. AIM :>> Information.LEOECHO. Yahoo :>> Information.LEOECHO. Gmail :>> Information.LEOECHO. >> Information.LEOECHO.IP Adress :>> Information.LEOECHO. >> Information.LEOCD..MKDIR School-WorkCD School-WorkECHO. >> School-Work.LEOECHO.____________________________________________ >> School-Work.LEOECHO. >> School-Work.LEOECHO.School >> School-Work.LEOECHO.____________________________________________ >> School-Work.LEOECHO. >> School-Work.LEOECHO.School Name :>> School-Work.LEOECHO.School Phone :>> School-Work.LEOECHO.School Address :>> School-Work.LEOECHO.Year :>> School-Work.LEOECHO.Role(s) :>> School-Work.LEOECHO. >> School-Work.LEOECHO.____________________________________________ >> School-Work.LEOECHO. >> School-Work.LEOECHO.Work >> School-Work.LEOECHO.____________________________________________ >> School-Work.LEOECHO. >> School-Work.LEOECHO.Job Name :>> School-Work.LEOECHO.Work Phone :>> School-Work.LEOECHO.Work Address :>> School-Work.LEOECHO.Title :>> School-Work.LEOCD..CD..MKDIR GeneralCD..GOTO :EXIT;INTECHO.An Error Has Occured. Please Make Sure You HaveECHO.Not Renamed The Folder "Manager".ECHO.And If You Have Return It To Its Original State.PING 127.0.0.1 >NULPING 127.0.0.1 >NUL:ERRORSET ENT=%TIME%RD %al%DIR Manager >NULIF ERRORLEVEL 1 GOTO :INTCD ManagerECHO._____________________________>>logs.logECHO. >>logs.logECHO.Process Failed: >> logs.logECHO. Start time %STT% >> logs.logECHO. End Time %ENT% >> logs.logECHO._____________________________>>logs.logECHO.>>logs.logCLSECHO.An Error Has Occured.ECHO.PING 127.0.0.1 >nulCALL PM.bat:EXIT@ECHO ONDIR Manager >NULIF ERRORLEVEL 1 GOTO :INTSET ENT=%TIME%ECHO._____________________________>>logs.logECHO. >>logs.logECHO.Process succeeded: >> logs.logECHO. Created %af% %al% >>logs.logECHO. Start time %STT% >> logs.logECHO. End Time %ENT% >> logs.logECHO._____________________________>>logs.logECHO.>>logs.logCALL PM.batThats 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.ThanksLEoH

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 commandIt 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...LEoHEDIT: 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 OFFCOLOR F2CD..:startCLSset /p name=First Name? : for %%a in ("&" ">" "<" "|" "^" " ") do call set "name=%%name:%%~a=%%"set fn=%name:~0,1%CLSset /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=%%aset pblname=%%b)set ln=%lname:~0,1%if defined pblname set pb=%pblname:~0,1%CLSfor %%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:1000set et="Last Name"goto error:2000if exist "%name%" (rd /s /q "%name%") else (GOTO 4000):ENDEXIT:4000set et="First Name"goto error:errorECHO.An Error Has Occured.ECHO.%et% May Be Incorrect...PING 127.0.0.1 >nulGOTO startBut 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 •