Skip to content

Latest commit

 

History

History
executable file
·
195 lines (139 loc) · 4.57 KB

File metadata and controls

executable file
·
195 lines (139 loc) · 4.57 KB

📁 Directory Operations (MD, RD, DIR, CD, PUSHD, POPD)

  1. 📂 MD / MKDIR – Create a Directory

    Both MD and MKDIR do the same thing: create a new folder.

    ✅ Syntax

    MD foldername
    MKDIR foldername

    🔹 Examples

    MD Logs
    MD D:\Backups\2025\July
    • If the full path doesn't exist, it creates all missing folders (like mkdir -p in Unix).
    • No error if the folder already exists.
  2. 🧹 RD / RMDIR – Remove a Directory

    Used to delete folders. If the folder is not empty, you must use the /S flag.

    ✅ Syntax

    RD foldername
    RD /S /Q foldername
    Option Description
    /S Remove all subfolders and files
    /Q Quiet mode (no prompt)

    🔹 Examples

    RD EmptyFolder
    RD /S /Q Logs\OldReports
  3. 📋 DIR – List Directory Contents

    Shows the contents of a directory, like ls in Unix.

    ✅ Syntax

    DIR [path] [/options]
    Option Description
    /B Bare format (just names)
    /S Include subdirectories
    /A Filter by attributes
    /O Sort output
    /T Which time field to use

    🔹 Examples

    DIR
    DIR /B *.txt
    DIR /S /B *.log > loglist.txt
    DIR /O:-D             :: Sort by date descending
  4. 📌 CD / CHDIR – Change Current Directory

    Changes the working directory for the script.

    ✅ Syntax

    CD folder
    CD ..
    CD /D drive:\path
    Option Description
    /D Change drive and directory simultaneously

    🔹 Examples

    CD D:\Projects
    CD /D C:\Scripts\Logs
    CD ..

    📍 Current Directory in a Variable

    SET CURDIR=%CD%
    ECHO You are in: %CURDIR%
  5. 🌀 PUSHD and POPD – Directory Stack Navigation

    These are often underused, but extremely useful when jumping around directories in a script.

    🔁 PUSHD: Go to a directory, save current one

    🔁 POPD: Return to last directory from stack

    🔹 Example

    PUSHD C:\Temp
    :: do something in Temp
    POPD

    This is safer and cleaner than manually changing back using CD.

    ✅ Nested Navigation Example

    PUSHD D:\Backups\July
    :: Run copy or cleanup here
    PUSHD Logs
    :: Nested operation
    POPD
    POPD

🛠️ Real-World Examples

  1. 📁 Create Folder Structure for a New Project

    MD MyApp
    CD MyApp
    MD Logs
    MD Config
    MD Temp
  2. 🧼 Clean Up a Directory

    IF EXIST OldData (
        RD /S /Q OldData
    )
  3. 📊 List All .csv Files in Subfolders

    DIR /S /B *.csv > csv_files.txt
  4. 🔄 Temporarily Navigate and Return

    PUSHD C:\Source
    COPY *.txt D:\Archive\
    POPD

⚠️ Gotchas and Best Practices

Tip / Problem Solution
Quoting paths with spaces Always use double quotes "C:\My Folder\"
CD doesn’t change drive Use CD /D or PUSHD
Folder already exists MD won’t error — safe for repeat use
Want to preserve original location Use PUSHD and POPD
Output clutter from DIR Use /B or redirect to file

🧠 Summary

Command Description
MD Make a new directory
RD Remove directory
DIR List contents of a directory
CD Change working directory
PUSHD Change + remember current folder
POPD Go back to remembered folder