如何将多个RTF文件转换为TXT文件

时间:2016-11-18 09:37:59

标签: powershell rtf

看这里: Is it possible to change an .rtf file to .txt file using some sort of batch script on Windows? 我已经看到了可能使用POWERSHELL做的事情。是一个完整的例子,但链接不起作用。 谁可以告诉我,我可以解决它?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以通过实现System.Windows.Forms.RichTextBox控件,将richtext文件加载到其中,然后将文本版本拉出来,使用.NET在powershell中轻松完成此操作。这是迄今为止我发现的最简单,最快捷的方式。

我完成此操作的功能在于:https://github.com/Asnivor/PowerShell-Misc-Functions/blob/master/translate-rtf-to-txt.ps1

更基本地解释一下:

$rtfFile = [System.Io.FileInfo]"path/to/some/rtf/file"
$txtFile = "path/to/the/destination/txt/file"

# Load *.rtf file into a hidden .NET RichTextBox
$rtBox = New-Object System.Windows.Forms.RichTextBox
$rtfText = [System.IO.File]::ReadAllText($rtfFile);
$rtBox.Rtf = $rtfText

# Get plain text version
$plainText = $rtBox.Text;

# Write the plain text out to the destination file
[System.IO.File]::WriteAllText($txtFile, $plainText)