100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

File and Folder Operations

Learn how to navigate, create, and remove directories and inspect file attributes using core Windows Batch commands.

File OperationsBeginner8 min readJul 10, 2026
Analogies

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.

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.

batch
@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
CD

Creating 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

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#FileAndFolderOperations#File#Folder#Operations#Navigating#StudyNotes#SkillVeris