TExcelTAD
Home › Windows CMD & Batch › mkdir & rmdir

mkdir & rmdir

mkdir (make directory) creates folders and rmdir (remove directory) deletes them. Both have short aliases that you will see in many scripts: md and rd.

Creating folders

Give mkdir one or more folder names. Names containing spaces need quotes, and a path creates every missing folder along the way:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir reports
md archive "old photos"
mkdir src\app\components
dir /b /o:n
echo ---
dir /b /o:n /s src
archive
old photos
reports
src
---
C:\Users\learner\demo\src\app
C:\Users\learner\demo\src\app\components

Creating intermediate folders automatically (like mkdir -p on Linux) is part of CMD's command extensions, which are on by default on every modern Windows.

When the folder already exists

mkdir refuses to create a folder that is already there, prints an error and sets a non-zero exit code:

cmd
C:\Users\ada>mkdir reports

C:\Users\ada>mkdir reports
A subdirectory or file reports already exists.

In scripts you usually want "make sure it exists". There are two common patterns: test first with if not exist, or try and hide the error with 2>nul:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir logs
if not exist logs\ mkdir logs
if not exist cache\ mkdir cache
mkdir logs 2>nul
mkdir output 2>nul
dir /b /o:n
cache
logs
output

The trailing backslash in if not exist logs\ makes the test true only for a folder called logs, not for a file with that name.

Removing empty folders

rmdir (or rd) deletes a folder only if it is empty:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir empty full
echo keep me> full\note.txt
rd empty
rd full 2>nul || echo full was not removed
dir /b /o:n
full was not removed
full

On Windows, the hidden error message for full is:

cmd
C:\Users\ada\demo>rd full
The directory is not empty.

Removing a folder tree: rd /s

rd /s deletes the folder and everything inside it: all files and subfolders. Interactively it asks first:

cmd
C:\Users\ada\demo>rd /s full
full, Are you sure (Y/N)? y

C:\Users\ada\demo>

/q (quiet) skips the question, which is what scripts use:

cmd · try it yourself
@echo off
mkdir demo & cd demo
mkdir build\obj\debug
echo binary> build\app.exe
echo temp> build\obj\debug\a.obj
echo before:
dir /b /o:n
rd /s /q build
echo after:
dir /b /o:n
if not exist build\ echo build is gone
before:
build
after:
build is gone

Warning: rd /s /q deletes immediately and permanently. Files do not go to the Recycle Bin. Double-check the path, and be extra careful with variables: if %target% is empty, rd /s /q "%target%\" would delete from the root of the current drive. Guard dangerous commands with if defined target.

cmd · try it yourself
@echo off
mkdir demo & cd demo
set "target="
if defined target (rd /s /q "%target%") else echo Refusing: target is not set
set "target=tmp_build"
mkdir %target%
if defined target (rd /s /q "%target%" && echo Removed %target%)
Refusing: target is not set
Removed tmp_build

Things that block rd

Windows will not delete a folder that something is still using:

  • Your own Command Prompt's current folder is inside it: cd .. first.
  • A program has a file inside it open (an editor, Excel, an antivirus scan).
  • A file inside is read-only or the folder needs administrator rights (the attrib lesson and Running as Administrator explain both).
cmd
C:\Users\ada\demo\build>rd /s /q C:\Users\ada\demo\build
The process cannot access the file because it is being used by another process.

Creating empty files

There is no touch command in CMD, but redirection does the job:

cmd · try it yourself
@echo off
mkdir demo & cd demo
type nul > empty.txt
echo first line> one.txt
for %%f in (empty.txt one.txt) do echo %%f is %%~zf bytes
empty.txt is 0 bytes
one.txt is 12 bytes

type nul prints nothing, so > empty.txt creates a 0-byte file. one.txt holds 10 characters plus the Windows line ending CR LF (2 bytes). %%~zf is the size of the file in the for variable.

Command summary

Task CMD PowerShell bash
create a folder mkdir name New-Item -ItemType Directory name (mkdir) mkdir name
create nested folders mkdir a\b\c mkdir a\b\c mkdir -p a/b/c
remove an empty folder rd name Remove-Item name rmdir name
remove a folder tree rd /s /q name Remove-Item -Recurse -Force name rm -rf name
create an empty file type nul > f.txt New-Item f.txt touch f.txt

Learn more: Creating Files & Folders · Deleting Safely · Bash mkdir & rmdir

Test yourself

Which command removes the folder build and everything in it without asking?

  1. rd build
  2. del build
  3. rd /s /q build
  4. rmdir -rf build

What does mkdir a\b\c do when none of the folders exist?

  1. Fails because a does not exist
  2. Creates a, a\b and a\b\c
  3. Creates only c
  4. Creates a file called a\b\c

How do you create a folder only if it doesn't exist yet, without an error message?

  1. mkdir /f logs
  2. if not exist logs\ mkdir logs
  3. mkdir logs --force
  4. md? logs