Skip to content

Latest commit

 

History

History
executable file
·
204 lines (135 loc) · 5.18 KB

File metadata and controls

executable file
·
204 lines (135 loc) · 5.18 KB

📂Creating, Deleting, and Renaming Files

  • File operations are at the heart of scripting and automation. Whether you’re creating temp files, deleting old logs, or renaming backups,

    Windows Batch scripting provides a set of straightforward yet powerful commands to manage files directly from the terminal or script.

  1. 📝 Creating Files

    ✅ Using ECHO

    The simplest way to create a file:

    ECHO Hello world > file.txt
    • > creates or overwrites the file
    • >> appends to the file

    Example: Creating an empty file

    ECHO. > empty.txt

    or:

    COPY NUL empty.txt >NUL

    COPY NUL is the traditional DOS way to create an empty file.


    ⚠️ Common Pitfall: Writing Special Characters

    ECHO This is a > symbol > file.txt   :: WRONG

    This breaks because > is a redirection operator. Escape it using ^:

    ECHO This is a ^> symbol > file.txt

  1. 🗑️ Deleting Files

    ✅ Using DEL

    DEL file.txt
    • Deletes the specified file
    • Wildcards are allowed
    • No confirmation prompt

    📌 Syntax Variations

    DEL /Q /F myfile.txt
    Switch Meaning
    /Q Quiet mode (no prompt)
    /F Force delete (even read-only)

    Example: Delete all .log files in a folder

    DEL /Q *.log

    🔐 Safety: Confirm Before Deleting

    Batch doesn’t prompt by default, so always double-check wildcards.

    You can simulate confirmation:

    IF EXIST "dangerous.txt" (
        ECHO Are you sure? (Y/N)
        SET /P CHOICE=
        IF /I "%CHOICE%"=="Y" DEL dangerous.txt
    )

    🔄 Delete Files Recursively

    To delete all .tmp files in a folder and subfolders:

    DEL /S /Q *.tmp
    Switch Description
    /S Include subdirectories
    /Q Quiet, no confirmation

  1. 🔄 Renaming Files

    ✅ Using REN

    REN oldname.txt newname.txt
    • Does not allow changing directories — just the name

      Example: Rename all .txt to .bak

      FOR %%F IN (*.txt) DO REN "%%F" "%%~nF.bak"
      • %%~nF extracts just the filename (no extension)

      🚚 Rename and Move with MOVE

      MOVE can rename and change directory.

      MOVE file.txt C:\Archive\

      You can rename in-place too:

      MOVE old.txt new.txt

🧪 Real-World Examples

  1. 🧼 Clean Up Old Log Files

    DEL /Q /F /S *.log

    Delete all logs recursively and quietly.

  2. 🗂️ Archive Yesterday’s Files

    FOR %%F IN (*.txt) DO (
        MOVE "%%F" "C:\backup\%%~nF_%DATE:/=-%.txt"
    )

    Appends today’s date to each file and moves it to C:\backup.

  3. 📑 Create Config Template

    ECHO server=localhost > config.ini
    ECHO port=8080 >> config.ini

    Creates a basic config file line by line.


🧱 Best Practices

Practice Why it Matters
Use quotes for filenames Prevent errors with spaces or special characters
Double-check wildcards Avoid accidental data loss
Use /Q for automation Prevents hanging scripts
Use IF EXIST before DEL Adds a safety layer
Prefer MOVE for renaming REN doesn’t support changing directories

🧠 Summary Cheat Sheet

Operation Command
Create file ECHO Text > file.txt
Create empty file COPY NUL file.txt
Delete file DEL /Q file.txt
Delete all .log DEL /Q *.log
Delete recursively DEL /S /Q *.tmp
Rename file REN old.txt new.txt
Move and rename MOVE file.txt archive\file_2025.txt
Rename in loop FOR %%F IN (*.txt) DO REN "%%F" "%%~nF.bak"