ZetCode

Windows Command Prompt File Operations

last modified July 14, 2025

File operations are fundamental to working with the Windows Command Prompt. They allow you to create, modify, organize, and delete files and directories. Mastering these commands is essential for efficient system administration. This tutorial covers all major file operations with practical examples.

The Command Prompt provides powerful file management capabilities. Unlike graphical interfaces, cmd allows batch processing of files. This makes it ideal for automating repetitive file tasks. Many operations can be performed faster through commands than GUI.

Basic file operations include creating, copying, moving, and deleting files. Advanced operations involve searching, comparing, and modifying file attributes. All these can be performed through simple command-line instructions. We'll explore each with clear examples and explanations.

Understanding file paths is crucial for command-line file operations. Windows supports both absolute (full) and relative paths. Paths can reference the current directory (.) or parent directory (..). Proper path specification ensures commands target the correct files.

Basic Definitions

Before diving into examples, let's define key terms and concepts. These form the foundation for understanding file operations.

File System: The method Windows uses to organize files on storage. It consists of directories (folders) and files arranged hierarchically. The NTFS file system is standard on modern Windows installations.

Current Directory: The working directory where commands execute. Cmd displays this in the prompt (C:\Users\Name>). Use cd command to change the current directory.

Path: The address of a file or directory in the file system. Absolute paths start from the root (C:\Folder\file.txt). Relative paths are based on current directory (..\file.txt).

Wildcards: Special characters that match multiple files. The asterisk (*) matches any sequence of characters. The question mark (?) matches any single character.

Redirection: Changing where command input/output goes. The > symbol redirects output to a file (overwriting). The >> symbol appends output to a file.

Creating and Viewing Files

Creating and viewing files are among the most basic operations. Cmd provides several ways to create files with content. The type command displays file contents directly in the console.

create_view.bat
@echo off
echo Creating sample files...
echo First line > file1.txt
echo Second line >> file1.txt
echo Another file > file2.txt
type file1.txt

This script demonstrates file creation and content viewing. It creates two text files with different approaches. The type command shows the contents of file1.txt.

echo First line > file1.txt

Creates file1.txt with "First line" as content. The > operator creates a new file or overwrites existing. This is the simplest way to create small text files.

echo Second line >> file1.txt

Appends "Second line" to file1.txt. The >> operator adds to existing content instead of overwriting. This is useful for logging or accumulating output.

type file1.txt

Displays the contents of file1.txt in the console. The type command is the primary way to view file contents. For large files, use | more to paginate output.

C:\>create_view.bat
Creating sample files...
First line
Second line

The output shows the creation process and file contents. Both lines appear because we used both > and >> operators.

Copying and Moving Files

Copying and moving files are essential for file management. The copy command duplicates files while move transfers them. Both support wildcards for batch operations.

copy_move.bat
@echo off
mkdir source
mkdir destination
echo Test file > source\original.txt
copy source\original.txt destination\copy.txt
move source\original.txt destination\moved.txt
dir destination

This script demonstrates file copying and moving between directories. It creates source and destination folders for the operations. Finally, it lists the destination directory contents.

copy source\original.txt destination\copy.txt

Copies original.txt from source to destination as copy.txt. The copy command requires source and destination parameters. Wildcards like *.txt can copy multiple files at once.

move source\original.txt destination\moved.txt

Moves original.txt from source to destination as moved.txt. Move deletes the original file after successful transfer. Like copy, it supports wildcards for batch operations.

dir destination

Lists contents of the destination directory. This verifies both copy and move operations succeeded. The output should show both copy.txt and moved.txt.

C:\>copy_move.bat
 Volume in drive C is OS
 Volume Serial Number is ABCD-EFGH

 Directory of C:\destination

02/15/2025  01:30 PM                10 copy.txt
02/15/2025  01:30 PM                10 moved.txt
               2 File(s)             20 bytes

The output confirms both files exist in destination. File sizes and dates will match the original creation time.

Deleting Files and Directories

Deleting files and directories requires careful command use. The del command removes files while rd removes directories. These operations are permanent - no Recycle Bin recovery.

delete_remove.bat
@echo off
mkdir to_delete
echo File1 > to_delete\file1.txt
echo File2 > to_delete\file2.txt
del to_delete\*.txt
rd to_delete

This script creates a directory with files, then cleans up. It demonstrates safe deletion of files followed by directory removal. The operations are sequential and dependent.

del to_delete\*.txt

Deletes all .txt files in the to_delete directory. The * wildcard matches all filenames with .txt extension. Use /q for quiet mode (no confirmation prompts).

rd to_delete

Removes the empty to_delete directory. The rd command only works on empty directories by default. Add /s /q to force delete non-empty directories.

C:\>delete_remove.bat
C:\to_delete\*.txt
Are you sure (Y/N)? y

The output shows the deletion confirmation prompt. The directory removal happens silently if successful. Errors appear if operations fail (e.g., files still exist).

File Attributes and Permissions

File attributes control visibility and access permissions. The attrib command displays and modifies file attributes. Attributes include Read-only, Hidden, System, and Archive.

attributes.bat
@echo off
echo Test file > test.txt
attrib test.txt
attrib +h +r test.txt
attrib test.txt
attrib -h -r test.txt

This script demonstrates attribute manipulation. It creates a test file, shows initial attributes, modifies them, then restores original settings. Each step is verified.

attrib test.txt

Displays current attributes of test.txt. The output shows letter codes for set attributes. No letters means no special attributes are set.

attrib +h +r test.txt

Adds Hidden (h) and Read-only (r) attributes to test.txt. The + operator adds attributes while - removes them. Multiple attributes can be modified in one command.

attrib -h -r test.txt

Removes Hidden and Read-only attributes from test.txt. This restores the file to its original state. System (s) and Archive (a) attributes work similarly.

C:\>attributes.bat
A        C:\test.txt
A  R  H  C:\test.txt
A        C:\test.txt

The output shows attribute changes. 'A' indicates Archive bit is set. R and H appear when those attributes are active, then disappear.

Searching and Comparing Files

Finding and comparing files are powerful cmd capabilities. The find command searches files for text strings. The fc command compares files to identify differences.

search_compare.bat
@echo off
echo Apples > fruits.txt
echo Oranges >> fruits.txt
echo Bananas >> fruits.txt
find "range" fruits.txt
echo A different line > compare1.txt
echo Another different line > compare2.txt
fc compare1.txt compare2.txt

This script demonstrates text search and file comparison. It creates a sample file for searching and two files for comparison. Both operations provide detailed output about matches/differences.

find "range" fruits.txt

Searches fruits.txt for lines containing "range". The command is case-sensitive by default (/i ignores case). It outputs matching lines with line numbers.

fc compare1.txt compare2.txt

Compares compare1.txt and compare2.txt byte-by-byte. The fc (File Compare) command highlights all differences. Use /b for binary comparison or /n to show line numbers.

C:\>search_compare.bat

---------- FRUITS.TXT
Oranges

Comparing files compare1.txt and compare2.txt
***** compare1.txt
A different line
***** compare2.txt
Another different line
*****

The output shows the search found "Oranges" containing "range". The comparison clearly displays the differing lines between files.

Source

Windows Command Reference

This tutorial covered essential Command Prompt file operations. Practice these commands to become proficient in managing files via cmd. Combine them in scripts to automate complex file management tasks.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.