Skip to content

Latest commit

 

History

History
executable file
·
194 lines (131 loc) · 5.56 KB

File metadata and controls

executable file
·
194 lines (131 loc) · 5.56 KB

🧾 File Extensions and Wildcards

  • When working with batch scripts in Windows, two often-overlooked but fundamental tools are:

    • 📄 File Extensions — which determine how files are interpreted and handled

    • Wildcards — which let you select multiple files dynamically

    Together, they let you create powerful scripts that are flexible, dynamic, and scalable — perfect for file processing, cleanup routines, and automated workflows.

    📂 Understanding File Extensions in Batch

    In Windows, file extensions (e.g., .txt, .log, .exe) define how the OS treats the file. In batch scripting:

    • You often filter files by extension using FOR, IF EXIST, DEL, COPY, etc.
    • Extensions are not case-sensitive
    • To reference just the extension or filename, use variable modifiers

    ✅ Common File Extensions in Batch Contexts

    Extension Use Case
    .bat Batch script
    .cmd Modern batch script (like .bat)
    .txt Logs, data, or configuration
    .log Logs from apps or services
    .exe Executables
    .zip Archives or backups

✳️ Wildcards in Batch Scripts

  • Wildcards let you match multiple files using patterns.

    ✅ Supported Wildcards

    Wildcard Meaning
    * Zero or more characters
    ? Exactly one character

    🔹 Examples

    DIR *.txt          :: All files ending in .txt
    DIR data?.csv      :: data1.csv, dataA.csv, but NOT data10.csv
    DEL report*.log    :: report1.log, report_final.log

    🛠️ Using Wildcards with Commands

    🧹 Deleting Multiple Files

    DEL /Q *.tmp

    Deletes all .tmp files in the directory without confirmation.


    📝 Copying Files by Extension

    COPY *.txt D:\Backup\

    Copies all .txt files to the backup directory.


    🌀 Processing Files in a Loop

    FOR %%F IN (*.log) DO (
        ECHO Processing: %%F
    )

    This loop processes all .log files in the current folder.

    You can also apply filters:

    FOR %%F IN (report_202?.log) DO (
        ECHO Match: %%F
    )

    🔍 File Matching: FOR /R for Subdirectories

    FOR /R %%F IN (*.txt) DO (
        ECHO Found: %%F
    )
    • Recursively finds all .txt files in current and subfolders.

🧠 Variable Modifiers for File Extensions

  • Inside a FOR loop, you can extract file parts:

    Modifier Meaning
    %%~nF Name only (no extension)
    %%~xF Extension only (with dot)
    %%~dpF Drive + path
    %%~nxF Name + extension
    FOR %%F IN (*.log) DO (
        ECHO Name: %%~nF, Extension: %%~xF
    )

🧪 Real-World Use Cases

  1. 📦 Batch Rename .txt.bak

    FOR %%F IN (*.txt) DO (
        REN "%%F" "%%~nF.bak"
    )
  2. 📁 Move Only .zip Files to Archive

    MOVE *.zip D:\Archives\
  3. 📄 Count .log Files in a Folder

    SET COUNT=0
    FOR %%F IN (*.log) DO SET /A COUNT+=1
    ECHO Log file count: %COUNT%
  4. 🧹 Clean Up Old Backups

    DEL /S /Q backup_*.zip

    Deletes all ZIP files starting with backup_ in current and subfolders.

  5. 💡 Bonus: Conditional File Extension Checks

    You can conditionally act on file extensions:

    FOR %%F IN (*) DO (
        IF /I "%%~xF"==".log" (
            ECHO Found log file: %%F
        )
    )

    Use /I for case-insensitive comparison.


⚠️ Limitations and Gotchas

Issue Workaround
Cannot use wildcards with REN destination Use FOR loop with modifiers
Wildcards in IF EXIST require care Avoid quotes: IF EXIST *.txt (not IF EXIST "*.txt")
No regex support Use PowerShell or hybrid script for complex filters

🧠 Summary

Task Batch Pattern
Match all .txt files *.txt
Match any one-letter name ?.txt
Process by extension FOR %%F IN (*.ext)
Get name only %%~nF
Get extension only %%~xF