TExcelTAD
Home › Windows CMD & Batch › Mini Project: Folder Organizer

Mini Project: Folder Organizer

Every Downloads folder ends up the same way: invoices next to holiday photos next to zip files next to a README with no extension. In this project you'll write organize.bat, a batch script that sorts the files of a folder into subfolders by type:

text
incoming\                         incoming\
  backup.zip                        Archives\backup.zip
  Budget 2026.xlsx                  Code\script.py
  holiday.png          ------>      Documents\Budget 2026.xlsx, invoice.pdf, notes.txt
  IMG_0412.JPG     organize.bat     Images\holiday.png, IMG_0412.JPG, photo.jpeg
  invoice.pdf                       Music\song.mp3
  ...                               Other\README

Along the way you'll use much of the course: dir /b, the %%~nxf modifiers, for /f, call :label subroutines with arguments, delayed expansion, if /i, mkdir, move, pushd/popd, for /r and sort. The finished script has a dry-run mode, never overwrites a file, and prints a report.

Every step is a complete program that first creates the same ten sample files in a folder called incoming (each playground starts fresh). On your PC you'd point the script at a real folder, for example organize.bat "%USERPROFILE%\Downloads", after trying /dryrun first.

Step 1: Create the sample files and look at them

>"incoming\%%~f" echo sample data writes one line into each file; the quotes around every name keep Budget 2026.xlsx (with its space) in one piece. Then dir /b /o:n /a:-d lists files only (/a:-d = attribute "not a directory"), sorted by name, and the for modifiers take each name apart:

cmd · try it yourself
@echo off
mkdir incoming
for %%f in ("Budget 2026.xlsx" "IMG_0412.JPG" "README" "backup.zip" "holiday.png" "invoice.pdf" "notes.txt" "photo.jpeg" "script.py" "song.mp3") do (
  >"incoming\%%~f" echo sample data
)
cd incoming
dir /b /o:n
echo.
for /f "delims=" %%f in ('dir /b /o:n /a:-d') do echo name=[%%~nf] ext=[%%~xf] size=%%~zf
backup.zip
Budget 2026.xlsx
holiday.png
IMG_0412.JPG
invoice.pdf
notes.txt
photo.jpeg
README
script.py
song.mp3

name=[backup] ext=[.zip] size=13
name=[Budget 2026] ext=[.xlsx] size=13
name=[holiday] ext=[.png] size=13
name=[IMG_0412] ext=[.JPG] size=13
name=[invoice] ext=[.pdf] size=13
name=[notes] ext=[.txt] size=13
name=[photo] ext=[.jpeg] size=13
name=[README] ext=[] size=13
name=[script] ext=[.py] size=13
name=[song] ext=[.mp3] size=13

%%~nf is the name without extension, %%~xf the extension including the dot, and %%~zf the size in bytes (11 characters plus the line break, CR LF). Note two details the organizer must handle: IMG_0412.JPG has an upper-case extension, and README has none at all.

Why for /f with dir, and not for %%f in (*)? The dir command runs once, before the loop body, so the list is fixed even while the loop moves files around. A plain for reads the folder as it goes, and its order is not guaranteed. See Batch for Loops and for /f: Parse Text.

Step 2: Decide the category

Each category is a variable holding its extensions: Images=.jpg .jpeg .png .gif .webp and so on. The subroutine :category receives an extension as %1, starts with Other and checks every list. Because the list's name is in a loop variable, it reads the list with !%%c! (delayed expansion of a variable whose name is computed), and if /i makes .JPG equal to .jpg:

cmd · try it yourself
@echo off
setlocal enabledelayedexpansion
mkdir incoming
for %%f in ("Budget 2026.xlsx" "IMG_0412.JPG" "README" "backup.zip" "holiday.png" "invoice.pdf" "notes.txt" "photo.jpeg" "script.py" "song.mp3") do (
  >"incoming\%%~f" echo sample data
)
set "Images=.jpg .jpeg .png .gif .webp"
set "Documents=.pdf .docx .xlsx .pptx .txt .md"
set "Archives=.zip .7z .rar"
set "Music=.mp3 .wav .flac"
set "Code=.py .js .bat .ps1"

cd incoming
for /f "delims=" %%f in ('dir /b /o:n /a:-d') do (
  call :category "%%~xf"
  set "name=%%f                    "
  echo !name:~0,20! !cat!
)
goto :eof

:category
set "cat=Other"
if "%~1"=="" exit /b
for %%c in (Images Documents Archives Music Code) do (
  for %%e in (!%%c!) do if /i "%~1"=="%%e" set "cat=%%c"
)
exit /b
backup.zip           Archives
Budget 2026.xlsx     Documents
holiday.png          Images
IMG_0412.JPG         Images
invoice.pdf          Documents
notes.txt            Documents
photo.jpeg           Images
README               Other
script.py            Code
song.mp3             Music

README has an empty extension, so :category returns Other right away; the "%~1"=="" test with quotes avoids a syntax error on empty arguments. Padding the name with spaces and cutting it with !name:~0,20! lines up the second column (String Manipulation).

Step 3: Move the files

Now the real work: create the category folder if it doesn't exist and move the file into it. if not exist "!cat!\" (with a trailing backslash) checks for a folder. move prints a "1 file(s) moved." line for every file, so it is silenced with >nul. Afterwards the script lists each folder and its contents with two nested for /f loops over dir /b /o:n:

cmd · try it yourself
@echo off
setlocal enabledelayedexpansion
mkdir incoming
for %%f in ("Budget 2026.xlsx" "IMG_0412.JPG" "README" "backup.zip" "holiday.png" "invoice.pdf" "notes.txt" "photo.jpeg" "script.py" "song.mp3") do (
  >"incoming\%%~f" echo sample data
)
set "Images=.jpg .jpeg .png .gif .webp"
set "Documents=.pdf .docx .xlsx .pptx .txt .md"
set "Archives=.zip .7z .rar"
set "Music=.mp3 .wav .flac"
set "Code=.py .js .bat .ps1"

cd incoming
for /f "delims=" %%f in ('dir /b /o:n /a:-d') do (
  call :category "%%~xf"
  if not exist "!cat!\" mkdir "!cat!"
  move "%%f" "!cat!\" >nul
)
for /f "delims=" %%d in ('dir /b /o:n /a:d') do (
  echo %%d\
  for /f "delims=" %%f in ('dir /b /o:n "%%d"') do echo     %%f
)
goto :eof

:category
set "cat=Other"
if "%~1"=="" exit /b
for %%c in (Images Documents Archives Music Code) do (
  for %%e in (!%%c!) do if /i "%~1"=="%%e" set "cat=%%c"
)
exit /b
Archives\
    backup.zip
Code\
    script.py
Documents\
    Budget 2026.xlsx
    invoice.pdf
    notes.txt
Images\
    holiday.png
    IMG_0412.JPG
    photo.jpeg
Music\
    song.mp3
Other\
    README

It works, but it is not yet safe:

  • If Documents\notes.txt already exists, move asks "Overwrite ...? (Yes/No/All)" interactively, or, with /y, silently destroys the old file.
  • There's no way to see what would happen before it happens.
  • The folder name is hard-coded.

Step 4: The finished script

The final version fixes all three and adds a report:

  • :organize folder [/dryrun] takes the folder as an argument, checks that it exists and works inside it with pushd/popd. With /dryrun it only prints what it would do.
  • :unique changes the target name while it is taken: notes.txt becomes notes2.txt, then notes3.txt, … so nothing is ever overwritten. It loops with goto (see goto & Labels).
  • :report counts the files in each subfolder, then walks the whole tree with for /r, strips the folder prefix from each full path (!p:%CD%\=!) and sorts the list with sort. Sorting matters because for /r, like plain for, visits files in the order the file system returns them.
  • setlocal inside :organize keeps its variables private, and exit /b 2 reports a missing folder to the caller (Exit Codes & errorlevel).

The test run puts an older notes.txt into Documents first, then calls the organizer four times: dry run, real run, report, and a second real run that must find nothing left to do.

cmd · try it yourself
@echo off
setlocal enabledelayedexpansion
mkdir incoming
for %%f in ("Budget 2026.xlsx" "IMG_0412.JPG" "README" "backup.zip" "holiday.png" "invoice.pdf" "notes.txt" "photo.jpeg" "script.py" "song.mp3") do (
  >"incoming\%%~f" echo sample data
)
mkdir incoming\Documents
>incoming\Documents\notes.txt echo older notes

call :organize incoming /dryrun
echo.
call :organize incoming
echo.
call :report incoming
echo.
call :organize incoming
goto :eof

:organize
rem usage: call :organize folder [/dryrun]
setlocal
if not exist "%~1\" (
  echo ERROR: folder "%~1" not found
  exit /b 2
)
set "dry="
if /i "%~2"=="/dryrun" set "dry=1"
set "Images=.jpg .jpeg .png .gif .webp"
set "Documents=.pdf .docx .xlsx .pptx .txt .md"
set "Archives=.zip .7z .rar"
set "Music=.mp3 .wav .flac"
set "Code=.py .js .bat .ps1"
pushd "%~1"
set /a moved=0
for /f "delims=" %%f in ('dir /b /o:n /a:-d 2^>nul') do (
  call :category "%%~xf"
  set "target=!cat!\%%~nxf"
  set /a n=2
  call :unique "%%~nf" "%%~xf"
  if defined dry (
    echo [dry run] %%f -^> !target!
  ) else (
    if not exist "!cat!\" mkdir "!cat!"
    move "%%f" "!target!" >nul && set /a moved+=1
  )
)
popd
if defined dry (echo Dry run: nothing was moved.) else (echo Moved %moved% file^(s^) in %~1.)
endlocal
exit /b 0

:category
rem %1 = extension with dot (may be empty); sets cat
set "cat=Other"
if "%~1"=="" exit /b
for %%c in (Images Documents Archives Music Code) do (
  for %%e in (!%%c!) do if /i "%~1"=="%%e" set "cat=%%c"
)
exit /b

:unique
rem %1 = name, %2 = extension; adds 2, 3, ... until target is free
if not exist "!target!" exit /b
set "target=!cat!\%~1!n!%~2"
set /a n+=1
goto unique

:report
rem %1 = folder; counts per subfolder, then every file
pushd "%~1"
for /f "delims=" %%d in ('dir /b /o:n /a:d') do (
  set /a count=0
  for /f "delims=" %%f in ('dir /b /o:n /a:-d "%%d"') do set /a count+=1
  echo %%d: !count! file^(s^)
)
(for /r %%f in (*) do (
  set "p=%%f"
  echo   !p:%CD%\=!
)) > "%TEMP%\organizer-list.txt"
sort "%TEMP%\organizer-list.txt"
del "%TEMP%\organizer-list.txt"
popd
exit /b
[dry run] backup.zip -> Archives\backup.zip
[dry run] Budget 2026.xlsx -> Documents\Budget 2026.xlsx
[dry run] holiday.png -> Images\holiday.png
[dry run] IMG_0412.JPG -> Images\IMG_0412.JPG
[dry run] invoice.pdf -> Documents\invoice.pdf
[dry run] notes.txt -> Documents\notes2.txt
[dry run] photo.jpeg -> Images\photo.jpeg
[dry run] README -> Other\README
[dry run] script.py -> Code\script.py
[dry run] song.mp3 -> Music\song.mp3
Dry run: nothing was moved.

Moved 10 file(s) in incoming.

Archives: 1 file(s)
Code: 1 file(s)
Documents: 4 file(s)
Images: 3 file(s)
Music: 1 file(s)
Other: 1 file(s)
  Archives\backup.zip
  Code\script.py
  Documents\Budget 2026.xlsx
  Documents\invoice.pdf
  Documents\notes.txt
  Documents\notes2.txt
  Images\holiday.png
  Images\IMG_0412.JPG
  Images\photo.jpeg
  Music\song.mp3
  Other\README

Moved 0 file(s) in incoming.

The dry run already shows the rename (notes.txt -> Documents\notes2.txt) before anything happens, and running the organizer a second time is harmless: there are no loose files left, so it moves nothing. Being safe to run again is exactly what you want from a script that will be scheduled.

Step 5: Use it on your PC

Save the subroutines (everything from :organize on) after this short main part as organize.bat:

cmd
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
  echo usage: organize.bat folder [/dryrun]
  exit /b 1
)
call :organize "%~1" %2
if errorlevel 1 exit /b
if /i not "%~2"=="/dryrun" call :report "%~1"
exit /b 0

Then try it on a copy of a real folder:

cmd
C:\Users\ada>organize.bat "%USERPROFILE%\Downloads" /dryrun
[dry run] Budget 2026.xlsx -> Documents\Budget 2026.xlsx
[dry run] setup-vlc-3.0.21.exe -> Other\setup-vlc-3.0.21.exe
...
Dry run: nothing was moved.

C:\Users\ada>organize.bat "%USERPROFILE%\Downloads"
Moved 37 file(s) in C:\Users\ada\Downloads.
Archives: 4 file(s)
...

Sample session. To keep the folder tidy automatically, schedule it (schtasks: Scheduled Tasks):

cmd
schtasks /create /tn "TAD\OrganizeDownloads" /sc daily /st 18:00 /tr "\"C:\Scripts\organize.bat\" \"C:\Users\ada\Downloads\""

Ideas to extend it

  1. Add an Installers category for .exe and .msi, and a Videos category for .mp4 and .mkv.
  2. Read the categories from a text file such as categories.txt (Images=.jpg .png) with for /f "tokens=1,* delims==", so users can change them without editing the script.
  3. Sort into date folders instead (2026-10\): %%~tf gives each file's modification time.
  4. Write every move to organize.log next to the script (%~dp0organize.log) so a mistake can be undone.
  5. Add /undo, which reads the log backwards and moves the files back.

The same project in other shells: Bash Mini Project, PowerShell Mini Project.

Test yourself

Why does the organizer use if /i "%~1"=="%%e" to compare extensions?

  1. /i makes the comparison case-insensitive, so .JPG matches .jpg
  2. /i ignores the dot
  3. /i is required inside for loops
  4. /i compares numbers instead of text

What does dir /b /o:n /a:-d list?

  1. Only folders, sorted by name
  2. Only files (no folders), bare names sorted by name
  3. Hidden files only
  4. Files in all subfolders

What would happen without the :unique step when Documents\notes.txt already exists?

  1. move would ask whether to overwrite it, or with /y silently replace the old file
  2. move would rename the file automatically
  3. The file would be skipped silently
  4. mkdir would fail