File Handling Using Command Prompt (CMD) – A Comprehensive Guide
Table of Contents
1 Introduction
2 Getting Started with CMD
3 File Navigation
4 Listing Files and Directories
5 Creating a Directory
6 Deleting Files and Directories
7 Copying and Moving Files
8 File Renaming
9 Displaying File Contents
10 Creating an Empty File
1 Introduction
What is CMD?
CMD, short for Command Prompt, is a command-line interpreter available in Windows It allows users to
interact with the operating system by typing commands It's a powerful tool for various tasks, including
file handling
Why Learn File Handling with CMD?
File handling is a fundamental aspect of computing Learning to manage files with CMD provides a
deeper understanding of the Windows file system, enhances productivity, and can be valuable for
scripting and automation
Continue reading to delve into the world of CMD and master the art of efficient file handling
2 Getting Started with CMD
Opening CMD
- To open CMD, press `Win + R`, type `cmd`, and press Enter
- Alternatively, search for “Command Prompt” in the Start menu
Understanding the CMD Interface
CMD opens in a text-based interface You’ll see a prompt, typically starting with
`C:\Users\YourUsername>` This is where you’ll enter commands You can navigate through directories,
execute commands, and perform file handling tasks here
3 File Navigation
Using the `cd` Command
The `cd` (change directory) command allows you to navigate to different directories
Examples: Navigating to Different Directories
- `cd C:\` - Change to the root of the C drive
- `cd Documents` - Navigate into the “Documents” folder
- `cd ` - Move up one directory
4 Listing Files and Directories
The `dir` Command
The `dir` command lists the files and directories in the current directory
Output Formatting
- File and directory names
- Date and time of last modification
- File sizes
Recursive Listing
Use `dir /s` to list files and directories in the current directory and its subdirectories
Certainly, let's continue the guide by adding sections on creating, deleting files, and folders in a similar
detailed format as listing files and directories
5 Creating a Directory
The `mkdir` Command
The `mkdir` command allows you to create new directories
Example: Creating a New Directory
To create a directory named "NewFolder" in the current directory, use the following command:
mkdir NewFolder
6 Deleting Files and Directories
The `del` Command for File Deletion
The `del` command is used to delete files
Example: Deleting a File
To delete a file named "File txt" in the current directory, use the following command:
del File txt
The `rmdir` Command for Directory Removal
The `rmdir` command is used to remove directories However, it can only delete directories that are
empty
Example: Removing an Empty Directory
To remove an empty directory named "FolderName" in the current directory, use the following
command:
rmdir FolderName
7 Copying and Moving Files
The `copy` Command
The `copy` command is used to duplicate files from one location to another
Example: Copying a File
To copy a file named "File txt" to a different location, such as "C:\NewLocation," use the following
command:
copy File txt C:\NewLocation
The `move` Command
The `move` command is used to move files from one location to another It effectively cuts and pastes
the file
Example: Moving a File
To move a file named "File txt" to a different location, such as "C:\NewLocation," use the following
command
move File txt C:\NewLocation
These new sections provide detailed information and examples for creating, deleting files, and managing
directories using CMD Continue to the subsequent sections for more commands and in-depth
explanations
8 File Renaming
The `ren` Command
The `ren` command is used to rename files and directories
Example: Renaming a File
To rename a file named “OldName txt” to “NewName txt” in the current directory, use the following
command:
Ren OldName txt NewName txt
9 Displaying File Contents
The `type` Command
The `type` command allows you to view the content of text files directly in the CMD window
Example: Viewing a Text File
To view the content of a text file named “File txt,” use the following command:
Type File txt
10 Creating an Empty File
Using `echo` to Create Empty Files
You can create empty files using the `echo` command
Example: Creating an Empty Text File
To create an empty text file named “NewFile txt,” use the following command:
Echo > NewFile txt