Windows CMD – File & Directory Management
1️⃣ Directory Basics (dir)
Show files and folders
dir
Important entries
-
.→ current directory -
..→ parent directory -
<DIR>→ folder - File size shown in bytes
2️⃣ Creating Files with echo
Create a file with text
echo "sajjad" > samina.txt
Read file content
type samina.txt
Output
"sajjad"
⚠ Why quotes appear?
Because quotes are literal characters inside echo.
✔ To avoid quotes:
echo sajjad > samina.txt
3️⃣ Creating an Empty (Blank) File – IMPORTANT CONCEPT
Command used
echo 2>err.txt
❓ Why is 2 used here?
This is error stream redirection.
| Stream | Meaning |
|---|---|
0 |
Standard Input |
1 |
Standard Output |
2 |
Standard Error |
What actually happens?
-
echoproduces no error - Error stream (
2) is empty - Empty stream redirected → file created with 0 bytes
✔ This is a CMD trick to create an empty file.
Better & common methods
type nul > file.txt
or
echo. > file.txt
4️⃣ Deleting Files (del)
del na.txt
Check after deletion
type na.txt
Output
The system cannot find the file specified.
✔ File deleted successfully
5️⃣ Renaming Files (rename)
rename err.txt error.txt
✔ File name changes only, content remains same
6️⃣ Creating & Moving Directories
Create directory
mkdir samsaj
Move file into directory
move samina.txt samsaj
Change directory
cd samsaj
Go back
cd ..
7️⃣ Path Symbols Explained
| Symbol | Meaning |
|---|---|
. |
Current directory |
.. |
Parent directory |
\ |
Windows path separator |
Example:
move samina.txt .\samsaj
8️⃣ Copying Files (copy)
copy samina.txt sajjad.txt
✔ Creates duplicate file with same content
9️⃣ Removing Directories (rmdir) – VERY IMPORTANT
Delete empty directory
rmdir sam
❌ If directory is NOT empty
The directory is not empty.
✔ Correct way to delete directory with files
rmdir /S sam
-
/S→ deletes all files + subdirectories - Confirmation required
❌ Wrong commands (do not work):
rmdir -S sam
rmdir \S sam
🔟 Comparing Files (fc)
fc sajjad.txt samina.txt
Output meaning
- Shows line-by-line differences
- Useful for file integrity checking
1️⃣1️⃣ Symbolic Links (Soft Links) – mklink
⚠ CMD must be Run as Administrator
Create symbolic link
mklink linkfile file3.txt
Verify
dir
Output
<SYMLINK> linkfile [file3.txt]
✔ Both files point to same data
✔ Reading/writing link affects original file
1️⃣2️⃣ Searching Files with dir
Search specific file recursively
dir /s torjan.txt
Search all .txt files
dir /s *.txt
1️⃣3️⃣ Finding Files using forfiles
Command
forfiles /P I:\ /S /M torjan.txt /C "cmd /c echo @PATH"
Flags explained
| Flag | Meaning |
|---|---|
/P |
Starting directory |
/S |
Recursive search |
/M |
Filename or pattern |
/C |
Command to run |
@PATH |
Full file path |
❗ Access denied errors?
Normal behavior — system protected folders ($RECYCLE.BIN, System Volume Information)
✔ File still found successfully.
1️⃣4️⃣ Finding Text Inside Files – find
❌ Wrong usage
find "pass" I:
✔ Correct usage
find "pass" filename.txt
Example:
find "pass" I:\ethical-test\samsaj\s299\s299.txt
Output
pass : s299
pass : s265
⚠ find is case-sensitive and basic
1️⃣5️⃣ Advanced Text Search – findstr (Recommended)
Search word
findstr "pass" s299.txt
Output
pass : s299
pass : s265
sam pass samina password
sajjad password pass
Why findstr is better?
✔ Case-insensitive (by default)
✔ Supports patterns
✔ Searches multiple lines cleanly
✅ Key Takeaways (Revision Summary)
-
echo > file→ creates file with content -
echo 2> file→ creates empty file via error redirection -
type→ read file -
del→ delete file -
rename→ rename file -
mkdir/rmdir→ directory management -
/Sis mandatory to delete non-empty folders -
fc→ compare files -
mklink→ symbolic link (admin required) -
dir /s→ recursive file search -
forfiles→ powerful file finder + executor -
findstr→ best for searching text inside files

Top comments (0)