Moving folders to alphabetically catalogued folders by using a batch-file

I want to move folders to alphabetically catalogued folders by using a batch-file, eg.:

Move folders starting with an "A" to folder "A", folders starting with a "B" to folder "B" etc. Numerical folders have to go to folder "#".

I've come up with this but it only works for files and I can't get it to work for folders. I have to change the %%~A*.* into something else but I don't know into what...

@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)

FOR %%A IN %Letters% DO MOVE "%%~A*.*" "%%~A\"
FOR %%B IN %Numbers% DO MOVE "%%~B*.*" "#\"

I have created the letter-folders already so creating those folders is not necessary.

What am I doing wrong?

2 answers

  • answered 2020-07-05 01:00 Wasif_Hasan

    Try this:

    for /f %%a in ('dir /b /ad "filepath"') do (
      set fn=%%a
      for %%b 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 if /i "%fn:~0,1%"=="%%b" move "%fn%" " %%b\"
      for /l %%c in (1,1,9) do if "%fn:~0,1%"=="%%c" move "%fn%" "%%c\"
    )
    

  • answered 2020-07-05 01:29 Compo

    This is untested, but may provide a solution based upon to my understanding of the question, with no comment response, as it existed at the time of this answer:

    @SetLocal EnableExtensions DisableDelayedExpansion
    @For /F Delims^=^ EOL^= %%G In ('Dir /B /A ^| "%__AppDir__%findstr.exe" ^
        /I "^[0123456789a-Z]" ^| "%__AppDir__%find.exe" /I /V "%~nx0"') Do @(
        Set "Object=%%G" & SetLocal EnableDelayedExpansion
        If "%%~aG" GEq "d" (If Not "!Object:~1,1!" == "" "%__AppDir__%Robocopy.exe"^
         "%%~G" "!Object:~,1!\%%G" /E /Move >NUL 2>&1
        ) Else "%__AppDir__%Robocopy.exe" . "!Object:~,1!" "%%G" /Mov >NUL 2>&1
        EndLocal)
    

    If you really did want to send the numeric objects to a directory named #, perhaps this untested modification of the above would be sufficient for your needs:

    @SetLocal EnableExtensions DisableDelayedExpansion
    @For /F Delims^=^ EOL^= %%G In ('Dir /B /A ^| "%__AppDir__%findstr.exe" /I "^[0123456789a-Z]" ^| "%__AppDir__%find.exe" /I /V "%~nx0"') Do @(Set "Object=%%G" & SetLocal EnableDelayedExpansion
        If "%%~aG" GEQ "d" (If Not "!Object:~1,1!" == "" (If "!Object:~,1!" GEQ "0" If "!Object:~,1!" LEQ "9" ("%__AppDir__%Robocopy.exe" "%%~G" "#\%%G" /E /Move >NUL 2>&1) Else "%__AppDir__%Robocopy.exe" "%%~G" "!Object:~,1!\%%G" /E /Move >NUL 2>&1)) Else If "!Object:~,1!" GEq "0" If "!Object:~,1!" LEq "9" ("%__AppDir__%Robocopy.exe" . # "%%G" /Mov >NUL 2>&1) Else "%__AppDir__%Robocopy.exe" . "!Object:~,1!" "%%G" /Mov >NUL 2>&1
        EndLocal)