Skip to main content
The batch-convert command processes entire directories of files, converting all files with a specified input extension to a target output format.

Syntax

python solution.py batch-convert <input_dir> <output_dir> <input_ext> <output_ext>

Parameters

input_dir
string
required
Path to the input directory containing files to convert
output_dir
string
required
Path to the output directory where converted files will be saved (created if it doesn’t exist)
input_ext
string
required
Input file extension to match (e.g., .txt or txt)
output_ext
string
required
Output file extension for converted files (e.g., .pdf or pdf)

Usage Examples

Convert Documents Directory

python solution.py batch-convert ./docs ./output .docx .pdf
Expected Output:
Info: Found 15 files with extension .docx
Info: Converting from /path/to/docs to /path/to/output, .docx → .pdf
Converting: document1.docx → document1.pdf
Converting: /path/to/docs/document1.docx to /path/to/output/document1.pdf
Success: Document conversion successful: /path/to/output/document1.pdf
Info: Conversion completed in 2.15 seconds.
Converting: document2.docx → document2.pdf
Converting: /path/to/docs/document2.docx to /path/to/output/document2.pdf
Success: Document conversion successful: /path/to/output/document2.pdf
Info: Conversion completed in 1.98 seconds.
...
--------------------------------------------------
Info: Batch conversion completed - Successful: 15
Info: Failed: 0
Info: Output directory: /path/to/output

Convert Images to PDF

python solution.py batch-convert ./photos ./pdfs .png .pdf
Converts all PNG images in the ./photos directory to PDF files in the ./pdfs directory.

Convert Audio Files

python solution.py batch-convert ./music ./converted .mp3 .flac
Converts all MP3 files to lossless FLAC format for archival purposes.

Extension Formats

Both formats work for extensions:
# With leading dot
python solution.py batch-convert ./input ./output .txt .pdf

# Without leading dot
python solution.py batch-convert ./input ./output txt pdf

Use Cases

1. Document Migration

Convert an entire documentation folder from Markdown to PDF:
python solution.py batch-convert ./markdown-docs ./pdf-docs .md .pdf

2. Image Optimization

Convert large PNG files to compressed JPG format:
python solution.py batch-convert ./images ./optimized .png .jpg

3. Media Library Conversion

Convert video files to web-optimized format:
python solution.py batch-convert ./videos ./web-videos .avi .mp4

4. Archive Format Standardization

Convert all ZIP archives to TAR.GZ:
python solution.py batch-convert ./archives ./converted .zip .tar.gz

Best Practices

  1. Test First: Run a test conversion on a single file before processing entire directories
  2. Separate Output: Always use a different output directory to avoid mixing source and converted files
  3. Check Dependencies: Ensure required dependencies (LaTeX, FFmpeg, etc.) are installed for your conversion type
  4. Monitor Progress: Watch the terminal output to identify any failed conversions
  5. Verify Results: Spot-check converted files to ensure quality meets expectations

Error Handling

If no files match the input extension:
Info: No files with extension .txt found in /path/to/input
If individual conversions fail, the batch process continues:
Error: Failed to convert document3.docx: Unsupported conversion
--------------------------------------------------
Info: Batch conversion completed - Successful: 14
Info: Failed: 1

Notes

  • The output directory is created automatically if it doesn’t exist
  • Original files are always preserved (equivalent to --preserve-original flag)
  • Failed conversions don’t stop the batch process
  • Output filenames match input filenames with the new extension
  • Only files in the root of the input directory are processed (not subdirectories)

Build docs developers (and LLMs) love