Batch-convert a folder
Leave feedback
On this page
Convert all supported documents in a directory to Markdown files.
using GroupDocs.Markdown;
string inputDir = @"C:\Documents";
string outputDir = @"C:\Markdown";
Directory.CreateDirectory(outputDir);
// Get supported extensions
var supported = new HashSet<string>(
MarkdownConverter.GetSupportedFormats()
.Select(f => "." + f.ToString().ToLower()));
int converted = 0;
int skipped = 0;
foreach (string file in Directory.GetFiles(inputDir))
{
string ext = Path.GetExtension(file).ToLower();
if (!supported.Contains(ext)) continue;
string outputPath = Path.Combine(
outputDir,
Path.GetFileNameWithoutExtension(file) + ".md");
try
{
MarkdownConverter.ToFile(file, outputPath);
converted++;
Console.WriteLine($"OK: {Path.GetFileName(file)}");
}
catch (Exception ex)
{
skipped++;
Console.WriteLine($"SKIP: {Path.GetFileName(file)} — {ex.Message}");
}
}
Console.WriteLine($"Done: {converted} converted, {skipped} skipped");
using GroupDocs.Markdown;
string inputDir = @"C:\Documents";
string outputDir = @"C:\Markdown";
Directory.CreateDirectory(outputDir);
var files = Directory.GetFiles(inputDir, "*.*")
.Where(f => new[] { ".docx", ".pdf", ".xlsx", ".epub" }
.Contains(Path.GetExtension(f).ToLower()))
.ToArray();
var options = new ConvertOptions
{
ImageExportStrategy = new SkipImagesStrategy()
};
var tasks = files.Select(async file =>
{
string outputPath = Path.Combine(
outputDir,
Path.GetFileNameWithoutExtension(file) + ".md");
try
{
await MarkdownConverter.ToFileAsync(file, outputPath, options);
Console.WriteLine($"OK: {Path.GetFileName(file)}");
}
catch (Exception ex)
{
Console.WriteLine($"SKIP: {Path.GetFileName(file)} — {ex.Message}");
}
});
await Task.WhenAll(tasks);
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.