ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Finished Programs

DONE: Easy Script to move all loose files in a folder into it's alphabetic subfolder

(1/2) > >>

questorfla:
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.

questorfla:
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:
I'll leave it to you to work out what to do if the first character is a space :)


--- Code: Text ---@echo offfor /f "usebackq" %%a in (`dir /a-d-s-h-r-l /b d:\*.*`) do (call :extractChar %%a) goto :END :extractCharset test=%~1set char=%test:~0,1%for %%b in (0,1,2,3,4,5,6,7,8,9) do (call :compRep %%b %char%)rem Throw a mkdir in here if you want to make the dirs on the flycopy "%~1" "output\%char%\%1"goto :EOF :compRepif %1 == %2 set char=agoto :EOF :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:
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:
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 ---@echo offfor /f "usebackq" %%a in (`dir /a-d-s-h-r-l /b d:\*.*`) do (call :extractChar %%a) goto :END :extractCharset test=%~1set char=%test:~0,1%for %%b in (0,1,2,3,4,5,6,7,8,9) do (if %%b == %char% set char=a)move "%~1" "output\%char%\%1"goto :EOF :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.

Navigation

[0] Message Index

[#] Next page

Go to full version