4

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?
  • 1
    We are not a script writing service. We expect users to tell us what they have tried so far (including any scripts they are using) and where they're stuck so that we can help with specific problems. Questions that only ask for scripts are too broad and are likely to be put on hold or closed. Please read How do I ask a good question?. – DavidPostill Jan 23 '16 at 15:57
  • With move a*.docx catalog\a you should be able to move all the files starting with a. Copy and past this line in a text file named moveall.bat 26 times and modify by hand for each letter then execute it... or search for some code on internet and try to modify it. – Hastur Jan 23 '16 at 16:36
  • Did it already BEFORE you mentioned it. Check my answer. :-) – James Anderson Jr. Jan 23 '16 at 16:40
3

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

  • 1
    Works Perfectly! – James Anderson Jr. Jan 23 '16 at 22:37
  • 1
    Nice script. A question: It doesn't work if you put a variable OverWrite="/Y " or OverWrite="/-Y " (or empty) and then you write MOVE %OverWrite .... ? (I'm Linux addicted) ;-) – Hastur Jan 25 '16 at 10:42
  • @Hastur Thanks, for the MOVE commands, the FOR lines could also be FOR %A IN %Letters% DO MOVE /Y "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\" or FOR %A IN %Letters% DO MOVE /-Y "%SourceDir%\%~A*.*" "%DestLetterDir%\%~A\". I think I put the ECHO Y rather than /Y out of habit of using the XCOPY command more than MOVE but those two ways should work as well so give those a shot without the ECHO Y |. – Pimp Juice IT Jan 25 '16 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.