topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 3:09 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: DONE: Easy Script to move all loose files in a folder into it's alphabetic subfolder  (Read 9012 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
This is needed due to total sloppiness by some users.  In a directory resource for a database, many of the folders contain nothing but the letters A - Z as alphabetized sub-folders.
Files put in folders of this type are SUPPOSED to be put inside the correct Alphabetic subfolder like a filing cabinet.  But... They often are not.
Over a period of a few weeks, I can have as many as 100 loose files that the DB that LOOKS for them under the alphabetic sub-folder they belong in.
"Alexander, John.pdf" should have gone in the "A" subdirectory inside the parent folder but instead he shows up outside the sub-folder in the parent.  This usually occurs in large groups.  I have yet to figure out which of the people loading this cannot read the alphabet but someone can't!

The next person to use the DB software, it brings up a "no record found" because the DB software is looking under A or B or C subfolders

So far, I have been moving these by hand to the correct subdirectories when they start to pile up in each parent folder but there must be some way automatically scan these loose files and move all of them into the correct  sub-folder based on the first letter of their last name. (usual format last name, first name).

Thanks if you have an easy to run script! It should process all loose files in a directory moving them into the correct subdirectory based on the first letter in the filename  Note:  0 - 9 are considered to be under A as there are so few

ALSO note:  I have been trying scripts from another site worded like this>

  • $ mkdir -p output/{A..Z}; for i in tstdir/*; do export FILE=$(basename "$i");  LTR=$(echo" ${FILE:0:1}" | tr [a-z] [A-Z]); mv "$i" "output/$LTR/$FILE" ; done

                                                                                     <or>
    $ mkdir -p output/{A..Z}
    $ for i in tstdir/*; do
        FILE=$(basename "$i") 
        LTR=$(echo "${FILE:0:1}" | tr [a-z] [A-Z])
        mv "$i" "output/$LTR/$FILE"
      done


But not getting there. And as I mention next, the ultimate would be to make the directory "self-adjusting" to prevent anyone from even being able to put a file out loose in it to start with.
« Last Edit: July 03, 2014, 02:53 AM by questorfla, Reason: made clearer »

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
This is more like an addendum.  The reason for al this mess is the people not paying attention in the first place.  Wish there as a rule I could enforce on a folder that would not allow any files (OR additional folders for that matter) to be added at the top level.  This would force the users to put them in the alphabetic sub-directories they belong in so I would not have to clean up after them.
While "wishing" the next thing would be a way to PREVENT certain people with "sticky fingers" from accidentally dragging one or more of the subdirectories down to the next level inside another subdirectory making something like "M through R" seem to disappear.  When they grab the whole group and drag it into the "S" directory or ?? any one of the others.  Next person in doesn't see M or N or O so they make NEW ones.  That covers up the error just enough that it doesn't get noticed until it has become a real problem

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
I'll leave it to you to work out what to do if the first character is a space :)

Code: Text [Select]
  1. @echo off
  2. for /f "usebackq" %%a in (`dir /a-d-s-h-r-l /b d:\*.*`) do (call :extractChar %%a)
  3.  
  4. goto :END
  5.  
  6. :extractChar
  7. set test=%~1
  8. set char=%test:~0,1%
  9. for %%b in (0,1,2,3,4,5,6,7,8,9) do (call :compRep %%b %char%)
  10. rem Throw a mkdir in here if you want to make the dirs on the fly
  11. copy "%~1" "output\%char%\%1"
  12. goto :EOF
  13.  
  14. :compRep
  15. if %1 == %2 set char=a
  16. goto :EOF
  17.  
  18. :END

Call it every x minutes using Task Scheduler or use something like FileNotify to detect file creation in the directory and then call it.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
talk about fast service.!!
I had thought that one through but as you noticed it was kind of late.  I was going to try something like that but was unsure about the commandline to count in from start of name to get the characer needed to file by.
I am sure your solution is more elegant
i can check for space and just have to not go any further if space is present but none of them have a "space" ,  or ".", or other weird character.  If they do, it can STAY lost!
My only issue will be with the ones with a number
Any numbers get filed in the A folders but there are not many

This is a prelude to serious attempts to getting this whole resource cloud-hosted for better off-site access.
Which I THINK is not going to work so  well from my recent trials.  Unless MS removes the 20K limit on number of files per instance, i have to use another provider.
We have more than enough space on the Business OneDrive (which is WHY >>>Thank God <<<  )  No more issues with Outlook not "sending fast enough"
:)
Fast now!  But they are wishing they had not gotten what they WISHED for!

Thanks 4WD (even on vacation you came thru!)



4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Forgot to change the copy to a move for your purposes, (used for testing).

You can also concat the second for and the if statements into one, saves doing the extra call.

ie.

Code: Text [Select]
  1. @echo off
  2. for /f "usebackq" %%a in (`dir /a-d-s-h-r-l /b d:\*.*`) do (call :extractChar %%a)
  3.  
  4. goto :END
  5.  
  6. :extractChar
  7. set test=%~1
  8. set char=%test:~0,1%
  9. for %%b in (0,1,2,3,4,5,6,7,8,9) do (if %%b == %char% set char=a)
  10. move "%~1" "output\%char%\%1"
  11. goto :EOF
  12.  
  13. :END

There's no need to use tr to change lower to uppercase, MS-DOS is case agnostic.  Just create the dirs first as upper or lower case ... or incorporate this to avoid extraneous programs.
« Last Edit: July 03, 2014, 04:50 PM by 4wd »

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Wish there as a rule I could enforce on a folder that would not allow any files (OR additional folders for that matter) to be added at the top level.  This would force the users to put them in the alphabetic sub-directories they belong in so I would not have to clean up after them.
While "wishing" the next thing would be a way to PREVENT certain people with "sticky fingers" from accidentally dragging one or more of the subdirectories down to the next level inside another subdirectory
Can you change read/write permissions on the folders, or get your sysadmin to do it for you?  I'm wondering if it would help to partially invert the logic.  That is, force  all users to write into the top-level or another holding folder, by giving them write permissions to that one only, with read-only permissions to the alphabetized folders inside.  Then, keep running your script as before on a schedule or via File Notify.  That way, everyone only need remember a single target location, the only one they can write to anyway, and the script moves the files into their correct destinations automatically.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Can you change read/write permissions on the folders, or get your sysadmin to do it for you?  I'm wondering if it would help to partially invert the logic.  That is, force  all users to write into the top-level or another holding folder, by giving them write permissions to that one only, with read-only permissions to the alphabetized folders inside.  Then, keep running your script as before on a schedule or via File Notify.  That way, everyone only need remember a single target location, the only one they can write to anyway, and the script moves the files into their correct destinations automatically.

+1  Requires no simulated thought on the part of the users :)

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Can you change read/write permissions on the folders, or get your sysadmin to do it for you?  [...]

+1  Requires no simulated thought on the part of the users :)
Also prevent them from drag-and-dropping subfolders into the wrong places, and send a subtle hint that they should listen when asked to put the files in the right alphabetic slots  :)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
 :Thmbsup: :D
Yep Perfect, 
The sad thing is even i I tell these people
..... in those exact words that I did this to avoid even  simulated thought on their part.....

They would take that as a Compliment!