Introduction to File and Folder Operations
Windows Batch scripting gives you direct control over the file system through a small set of built-in commands: DIR to list contents, CD to change directories, MD/RD to create and remove folders, and ATTRIB to inspect or change file attributes. These commands run inside cmd.exe itself rather than being separate executables, which is why they respond instantly and why their exact behavior can differ subtly between Windows versions.
Cricket analogy: Just as a captain checks the pitch report and ground dimensions before setting a field, a batch script checks the directory structure with DIR before deciding how to process files, adapting its plan to what it finds.
Navigating the Directory Tree
CD (or CHDIR) changes the current working directory, but by default it will not cross drives unless you add the /D switch, for example CD /D E:\Projects. PUSHD and POPD extend this by remembering a stack of directories: PUSHD saves the current location and moves to a new one, while POPD returns to the previously saved location, which is invaluable when a script needs to temporarily work elsewhere and reliably come back.
Cricket analogy: A fielder sprints from the boundary to take a catch and then jogs straight back to their original fielding position, mirroring how PUSHD moves to a new folder and POPD returns to where the script started.
@echo off
REM Show current directory, then navigate
CD
CD /D D:\Reports
REM Save current location, work elsewhere, then return
PUSHD C:\Temp\Backup
DIR /B
POPD
REM Confirm we are back where we started
CDCreating and Removing Directories
MD (or MKDIR) creates one or more folders, and unlike some other shells it can create an entire chain of missing parent folders in a single call, so MD C:\Data\2026\July works even if Data and 2026 do not yet exist. RD (or RMDIR) removes a directory, but by default it only succeeds on an empty folder; adding /S removes the folder and everything inside it recursively, and /Q suppresses the "Are you sure" confirmation prompt, so RD /S /Q is the combination scripts most often use for automated cleanup.
Cricket analogy: Building a new stadium complete with practice nets and a pavilion in one project mirrors MD creating an entire folder chain in a single command, while demolishing the whole ground at once resembles RD /S /Q.
Checking File Attributes
The ATTRIB command reads and sets four core attributes: R (read-only), A (archive), S (system), and H (hidden). Running ATTRIB alone on a file shows its current flags, while ATTRIB +H +S secret.txt marks a file hidden and system, and ATTRIB -R report.txt clears the read-only flag so a script can overwrite it. Scripts frequently check for the read-only flag before writing to a file, because attempting to overwrite a read-only file without clearing the attribute first will cause the write to fail.
Cricket analogy: Marking a pitch as 'used' before a match so groundstaff know not to prepare it again is similar to ATTRIB flagging a file read-only so scripts know not to overwrite it without clearing the flag first.
RD /S /Q permanently deletes a folder and every file and subfolder inside it, bypassing the Recycle Bin entirely. Always double-check the target path (and consider testing with an ECHO of the command first) before running RD /S /Q in any script that runs unattended, since a typo in the path can silently wipe the wrong directory.
- DIR, CD, MD, RD, and ATTRIB are built into cmd.exe, not separate executables.
- CD /D switches both the current directory and the current drive in one step.
- PUSHD saves the current directory on a stack; POPD restores it, enabling safe round trips.
- MD can create an entire chain of nested folders in a single command.
- RD requires /S to delete non-empty folders and /Q to suppress confirmation prompts.
- ATTRIB reads or sets the Read-only, Archive, System, and Hidden flags with +/- syntax.
- RD /S /Q deletes permanently, bypassing the Recycle Bin, so scripts must validate paths carefully.
Practice what you learned
1. Which switch allows CD to change to a directory on a different drive in one step?
2. What does PUSHD do before changing to a new directory?
3. Which command removes a non-empty folder and all its contents without a confirmation prompt?
4. What must a script do before it can overwrite a read-only file?
5. What happens when you run RD /S /Q on the wrong folder path?
Was this page helpful?
You May Also Like
Copy, Move, and Delete Commands
Master COPY, XCOPY, MOVE, REN, and DEL to duplicate, relocate, and remove files and folders safely in Batch scripts.
Robocopy Basics
Learn Robocopy's retry logic, mirroring behavior, and key switches for reliable file synchronization and backup scripts.
Reading and Writing Text Files
Learn how Batch scripts write to files with redirection and read them back using FOR /F, SET /P, and FINDSTR.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics