Move Files to an Alphabetically Cataloged Sub-Directory
Arranging Files into an Alphabetically Cataloged Sub-Directory
For example: [
D:/documents/janet_henderson.docx
] would move to [D:/documents/catalog/J/janet_henderson.docx
]Also, Note: I'm hoping it's obvious that files that have file names starting with a number go into the
"D:/documents/catalog/#/"
folder.
Based on how you explain this and your examples, below are some example command prompt copy and paste items, and batch scripts items that'll complete these actions as you describe.
Assumptions
- All the catalogue lettered and
#
directories will already be created. - For the first two below examples you will NEVER
MOVE
the same file with the same name to a directory where a file with that SAME name already exists. - For the last two below examples you will ALWAYS overwrite an
existing file with the
MOVE
commmand from source to destintion if the same file with the SAME name already exists.
Command Prompt Copy (prompt if file already exist)
@ECHO ON
SET Letters=(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)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#
FOR %A IN %Letters% DO MOVE "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\"
FOR %B IN %Numbers% DO MOVE "%SourceDir%\%~B*.*" "%DestNumDir%\"
GOTO EOF
Batch Script (prompt if file already exist)
@ECHO ON
SET Letters=(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)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#
FOR %%A IN %Letters% DO MOVE "%SourceDir%\%%~A*.*" "%DestLetterDir%\%%~A\"
FOR %%B IN %Numbers% DO MOVE "%SourceDir%\%%~B*.*" "%DestNumDir%\"
GOTO EOF
Command Prompt Copy (force overwrite)
@ECHO ON
SET Letters=(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)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#
FOR %A IN %Letters% DO ECHO Y | MOVE "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\"
FOR %B IN %Numbers% DO ECHO Y | MOVE "%SourceDir%\%~B*.*" "%DestNumDir%\"
GOTO EOF
Batch Script (force overwrite)
@ECHO ON
SET Letters=(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)
SET Numbers=(0,1,2,3,4,5,6,7,8,9)
SET SourceDir=D:\documents
SET DestLetterDir=D:\documents\catalog
SET DestNumDir=D:\documents\catalog\#
FOR %%A IN %Letters% DO ECHO Y | MOVE "%SourceDir%\%%~A*.*" "%DestLetterDir%\%%~A\"
FOR %%B IN %Numbers% DO ECHO Y | MOVE "%SourceDir%\%%~B*.*" "%DestNumDir%\"
GOTO EOF
Further Reading and Resources
Related videos on Youtube
James Anderson Jr.
Professional Full-Stack Web Developer, Designer, Artist, Computer Engineer, IT Consultant, Web Entrepreneur, Collector, Archivist, Completist, Purist, Uncle, Son, Brother, Friend & Beloved by All.
Updated on September 18, 2022Comments
-
James Anderson Jr. over 1 year
I have a variety of "files" (not folders) of the same type (e.g. "
.docx
"), in the same directory.Also, in that SAME directory, I have a sub-directory named "
catalog
", and within it, alphabetical folders (i.e, starting with#
,A
,B
,C
,D
,E
,F
, and so on...).Assuming these files are located at "
D:/documents/
", how [Using Windows CMD, or Windows Powershell] would I sort through the list of files, and move them into the "catalog" folder, into their proper [case-sensitive] "Alphabetical" folder, sorting by the [case-insensitive] first letter of their file-name?For example: [
D:/documents/janet_henderson.docx
] would move to [D:/documents/catalog/J/janet_henderson.docx
]Please note I need to have files with names starting with any number go into the "
D:/documents/catalog/#/
" folder.
What I have so far
I have the following in a
organize_files.bat
file:move documents\A*.docx documents\catalog\A\ move documents\B*.docx documents\catalog\B\ move documents\C*.docx documents\catalog\C\ move documents\D*.docx documents\catalog\D\ move documents\E*.docx documents\catalog\E\ move documents\F*.docx documents\catalog\F\ move documents\G*.docx documents\catalog\G\ move documents\H*.docx documents\catalog\H\ move documents\I*.docx documents\catalog\I\ move documents\J*.docx documents\catalog\J\ move documents\K*.docx documents\catalog\K\ move documents\L*.docx documents\catalog\L\ move documents\M*.docx documents\catalog\M\ move documents\N*.docx documents\catalog\N\ move documents\O*.docx documents\catalog\O\ move documents\P*.docx documents\catalog\P\ move documents\Q*.docx documents\catalog\Q\ move documents\R*.docx documents\catalog\R\ move documents\S*.docx documents\catalog\S\ move documents\T*.docx documents\catalog\T\ move documents\U*.docx documents\catalog\U\ move documents\V*.docx documents\catalog\V\ move documents\W*.docx documents\catalog\W\ move documents\X*.docx documents\catalog\X\ move documents\Y*.docx documents\catalog\Y\ move documents\Z*.docx documents\catalog\Z\
- Is there a better way to complete this operation such as putting it in a loop maybe?
- How do I take care of the files starting with a number, though?
- Do I need to tell Windows CMD to be case-insensitive, when checking for the file names?
-
James Anderson Jr. over 8 yearsWorks Perfectly!
-
Hastur over 8 yearsNice script. A question: It doesn't work if you put a variable
OverWrite="/Y "
orOverWrite="/-Y "
(or empty) and then you writeMOVE %OverWrite ....
? (I'm Linux addicted) ;-) -
Vomit IT - Chunky Mess Style over 8 years@Hastur Thanks, for the
MOVE
commands, theFOR
lines could also beFOR %A IN %Letters% DO MOVE /Y "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\"
orFOR %A IN %Letters% DO MOVE /-Y "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\"
. I think I put theECHO Y
rather than/Y
out of habit of using theXCOPY
command more thanMOVE
but those two ways should work as well so give those a shot without theECHO Y |
. -
PeterCo almost 5 years@PimpJuiceIT Why do you need the
GOTO EOF
as last command? -
Vomit IT - Chunky Mess Style almost 5 years@PeterCo That's just a habit I suppose but in this case I believe the
GOTO :EOF
will not make any difference. I put that there out of habit I think when I wrote that answer. Having it there doesn't do any harm in this case, but in some cases when you make aCALL
to another label, you can useGOTO :EOF
and that'll ensure once that routine you called it done, it'll pass control back to the logic where ever the last call left off if that's how the flow of the batch script logic is supposed to work.