I have 19,000 txt files that I need to convert to either doc or docx. Is there a way to do this in one batch or do I need to do them individually?
I have 19,000 txt files that I need to convert to either doc or docx. Is there a way to do this in one batch or do I need to do them individually?
@SteveNicol-2539
Tag "office-exchange-online-itpro" focuses on general issues about the configuration and administration of Exchange Online, as your issue is out of the scope of the tag, I will remove it. Thanks for your understanding.
Welcome to Q&A forum.
—————————————Update————————————
Sorry for the previous inappropriate response.
Please note: as PaulEdstein-5060 said, "Word cannot open .txt files that are simply renamed as .docx.
Although Word can open .txt files that are simply renamed as .doc, that does not mean they conform to the .doc binary format - they remain plain text files."
Please check if PaulEdstein-5060's reply is helpful to you.
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
erinding's 'solution' does not work!
Word cannot open .txt files that are simply renamed as .docx.
Although Word can open .txt files that are simply renamed as .doc, that does not mean they conform to the .doc binary format - they remain plain text files.
To do it properly, you need a macro like:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String
Sub Main()
Application.ScreenUpdating = False
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
Next
Application.ScreenUpdating = True
End Sub
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
RecurseWriteFolderName (SubFolder)
Next
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Sub UpdateDocuments(oFolder As String)
Dim strFldr As String, strFile As String, wdDoc As Document
strFldr = oFolder: If strFldr = "" Then Exit Sub
strFile = Dir(strFldr & "\*.txt", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFldr & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)
With wdDoc
'Save as docx
.SaveAs2 FileName:=Split(.FullName, ".txt")(0) & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
'close the document
.Close False
End With
'Delete the old file
'Kill strFldr & "\" & strFile
strFile = Dir()
Wend
Set wdDoc = Nothing
End Sub
The above code processes all .txt files in the selected folder and its sub-folders. Note the commented-out code to delete the .txt file once it has been converted.
For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
If you use C#, you can batch convert txt to doc or docx by using Spire.Doc for .NET, which is class library for processing Word documents on .NET platform.
Install Spire.Doc in to your Visual Studio via NuGet, and then you'll be able to do the batch conversion by running the following code snippet.
using System;
using System.IO;
using Spire.Doc;
namespace CreateWordFromTxt
{
class Program
{
static void Main(string[] args)
{
string inputFolder = @"C:\Users\Administrator\Desktop\TxtFileFolder";
string outputFolder = @"C:\Users\Administrator\Desktop\GeneratedWord\";
Document doc;
string fileNameWithoutExtension;
string outputFilePath;
DirectoryInfo folder = new DirectoryInfo(inputFolder);
foreach (FileInfo file in folder.GetFiles())
{
doc = new Document();
doc.LoadText(file.FullName);
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FullName);
outputFilePath = String.Format(outputFolder + fileNameWithoutExtension + ".docx");
doc.SaveToFile(outputFilePath, FileFormat.Docx2013);
}
}
}
}

9 people are following this question.