Skip to main content

How to Copy Folder Structure Without Files in Batch Script

A common task in scripting is to replicate a directory "scaffold" or template. You might need to set up a new project with standard folders like \src, \assets, and \output, or create a destination directory tree for a backup that mirrors the source, all without copying any of the actual files. This process creates an empty skeleton of the original structure.

This guide will teach you the two primary methods for this task. We'll cover the classic approach using the XCOPY command, which is simple and widely available, and then the modern, recommended method using the more powerful and robust Robocopy command.

The Classic Method: XCOPY /T /E

The XCOPY command is a powerful file and directory copying utility that has been part of Windows for a long time. It includes specific switches designed for exactly this task.

The Syntax

XCOPY "Source" "Destination" /T /E

  • /T: This is the key switch. It tells XCOPY to create the directory sTructure, but not to copy any of the files within it.
  • /E: This switch is also crucial. It instructs XCOPY to copy all subdirectories, Even if they are empty. Without /E, the /T switch would only copy directories that contain files, which defeats the purpose of creating a complete, empty skeleton.

Robocopy (Robust File Copy) is the successor to XCOPY and is the professional standard for any scripted file operations. It offers better reliability, logging, and more granular control. Its method for copying only the structure is less direct but just as effective.

The syntax: ROBOCOPY "Source" "Destination" /E /XF *

  • /E: This switch tells Robocopy to copy all subdirectories, including Empty ones.
  • /XF *: This is the clever part. /XF stands for "eXclude File." The * is a wildcard that means "all files." So, this command tells Robocopy to copy the entire directory tree (/E) but to explicitly exclude every single file it encounters. The result is an exact copy of the folder structure with no files.

Basic Example: Creating a Directory Skeleton

Let's say we have a source folder with the following structure:

C:\ProjectTemplate\
| README.md
|
+---assets\
| | logo.png
| \---styles\
| main.css
|
+---data\
| (empty folder)
|
\---src\
app.js

Method 1: Using XCOPY

@ECHO OFF
XCOPY "C:\ProjectTemplate" "C:\NewProject_XCOPY" /T /E

Method 2: Using Robocopy

@ECHO OFF
ROBOCOPY "C:\ProjectTemplate" "C:\NewProject_Robocopy" /E /XF *

Resulting Structure (for both methods)

Both commands produce the exact same result: a perfect, empty copy of the folder structure.

C:\NewProject_...\
+---assets\
| \---styles\
|
+---data\
|
\---src\

Key Parameters Explained

XCOPY Switches

  • /T: Creates the directory structure without copying files.
  • /E: Ensures that empty directories from the source are also created in the destination.

Robocopy Switches

  • /E: Copies subdirectories, including empty ones. A similar switch, /S, would exclude empty ones.
  • /XF <FileName>: Excludes files matching the specified name or wildcard. Using /XF * excludes all files. You could also use /XF *.log to copy the structure and all files except for log files.

Common Pitfalls and How to Solve Them

Problem: The Path Contains Spaces

If your source or destination path contains spaces, the command will fail unless the paths are properly quoted.

Let's see an example of the error:

REM This will FAIL.
XCOPY C:\My Project Template C:\New Project /T /E

Solution: Always Quote Your Paths

This is a universal best practice in batch scripting. Enclosing the paths in double quotes ensures they are treated as a single argument.

REM This is the correct, safe syntax.
XCOPY "C:\My Project Template" "C:\New Project" /T /E

Problem: The Destination Folder Already Exists

It's important to understand what happens if you run the command and the destination folder already contains files or other folders.

  • Both XCOPY and Robocopy will merge the source structure into the destination. They will create any missing folders from the source but will not delete any extra folders that might already exist in the destination.

Solution: If you need a clean, exact replica of the source structure, you must ensure the destination directory is empty before running the command. A common pattern is to delete and recreate it.

IF EXIST "C:\NewProject\" RMDIR /S /Q "C:\NewProject\"
MKDIR "C:\NewProject\"
XCOPY "C:\ProjectTemplate" "C:\NewProject" /T /E

Practical Example: Creating a New Project from a Template

This script automates the creation of a new, empty project folder based on a master template. It uses the robust Robocopy method.

@ECHO OFF
SETLOCAL
SET "TEMPLATE_DIR=C:\Templates\WebApp_Template"
SET "NEW_PROJECT_NAME=MyNewWebApp"
SET "PROJECTS_PARENT_DIR=E:\Development"

SET "NEW_PROJECT_PATH=%PROJECTS_PARENT_DIR%\%NEW_PROJECT_NAME%"

ECHO --- New Project Scaffolding Script ---
ECHO.
IF NOT EXIST "%TEMPLATE_DIR%\" (
ECHO [ERROR] Template directory not found.
GOTO :End
)
IF EXIST "%NEW_PROJECT_PATH%\" (
ECHO [ERROR] A project with that name already exists.
GOTO :End
)

ECHO Creating new project structure at: "%NEW_PROJECT_PATH%"

REM Use Robocopy to create the empty directory tree.
ROBOCOPY "%TEMPLATE_DIR%" "%NEW_PROJECT_PATH%" /E /XF * > NUL

ECHO.
ECHO [SUCCESS] New project scaffold created.

:End
ENDLOCAL

Conclusion

Replicating a directory structure without its files is a common and useful automation task.

  • The XCOPY /T /E command is the classic, simple method that is available on nearly all Windows systems.
  • The Robocopy /E /XF * command is the modern and recommended best practice. It is more robust, provides better feedback (even when suppressed with > NUL), and is the professional standard for scripted file operations.

By choosing the appropriate command and always remembering to quote your paths, you can easily and reliably create directory skeletons for any purpose.