eddyman1us
Junior Member
- Jan 31, 2008
- 121
- 8
Hi , I have directory full of .txt files all articles , is there any way to batch import all the files to a .csv database .
Last edited:
Hi , I have directory full of .txt files all articles , is there any way to batch import all the files to a .csv database .
http://hiderefer.org/?http://www.exceluser.com/explore/questions/vba_textcols.htm
''Require that all variables be declared
Option Explicit
''======================================================
'' Program: ParseText
'' Desc: Reads a text file into a variable then
'' writes it into a row, n chars at a time
'' Called by: user
'' Call:
'' Arguments:
'' Comments: Written quickly. No error-checking.
'' Changes----------------------------------------------
'' Date Programmer Change
'' 6/14/06 Charley Kyd Written
''======================================================
Sub ParseText()
''###C0NAN### - New
Dim MyFileSystemObject as Object
Dim MyFolderObject As Object
Dim MyFileObject As Object
Dim MyFileCollection As Object
Dim FolderContainingRawFiles as String
''###C0NAN### - New
Dim sText As String, sFile As String
''Get the full path to the source file
'' With ThisWorkbook
'' sFile = .Names("SourcePath").RefersToRange
'' If Left(sFile, 1) <> "\" Then sFile = sFile & "\"
'' sFile = sFile & .Names("SourceFile").RefersToRange
'' End With
''###C0NAN###
FolderContainingRawFiles = "C:\FolderWithFiles"
Set MyFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set MyFolderObject = MyFileSystemObject.GetFolder(FolderContainingRawFiles)
Set MyFileCollection = MyFolderObject.Files
For Each MyFileObject In MyFileCollection
sFile = MyFileObject.Name
sText = GetText(sFile)
next
''###C0NAN###
''###C0NAN### - Moved into above For Loop
''Get the full text string from the text file
''sText = GetText(sFile)
''###C0NAN### - Moved into above For Loop
''Remove all nonprintable characters from the text
''Comment out if the characters are wanted
sText = Excel.WorksheetFunction.Clean(sText)
''Write to the workbook
WriteToSheet sText,sFile
End Sub
''======================================================
'' Program: GetText
'' Desc: Read a text file into a string and then
'' return the string
'' Called by: ParseText
'' Call: GetText(sFile)
'' Arguments: sFile--The full path to the text file
'' Comments:
'' Changes----------------------------------------------
'' Date Programmer Change
'' 6/14/06 Charley Kyd Written
''======================================================
Function GetText(sFile As String) As String
Dim nSourceFile As Integer, sText As String
''Close any open text files
Close
''Get the number of the next free text file
nSourceFile = FreeFile
''Write the entire file to sText
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
GetText = sText
End Function
''======================================================
'' Program: WriteToSheet
'' Desc: Writes a text string to one row of a
'' worksheet, n characters per column
'' Called by: ParseText
'' Call: WriteToSheet sText
'' Arguments: sText--String with text from text file
'' Comments: This routine will throw an error if the
'' text is too long to fit within 256 columns
'' Changes----------------------------------------------
'' Date Programmer Change
'' 6/14/06 Charley Kyd Written
''======================================================
''###C0NAN### - I have Changed This To Send In the File Name as well
''Sub WriteToSheet(sText As String)
''###C0NAN### - I have Changed This To Send In the File Name as well
Sub WriteToSheet(sText As String,sFile as String)
Dim sTgtSheet As String, nTgtRow As Integer
Dim nColCount As Integer, sChunk As String
Dim nIncrement As Integer, rngRef As Range
''Get the controlling variables
With ThisWorkbook
sTgtSheet = .Names("TargetSheet").RefersToRange
''###C0NAN### - If you use this, It will overrite the rame row for each file
''nTgtRow = .Names("TargetRow").RefersToRange
''###C0NAN### - If you use this, It will overrite the rame row for each file
''###C0NAN### - Now this Will Automaticaly increment to the Next Unused Row.
nTgtRow = Worksheets(sTgtSheet).UsedRange.Rows.Count + 1
''###C0NAN### - Now this Will Automaticaly increment to the Next Unused Row.
''###C0NAN### - This is here to limit the amount of characters per cell...You can Skip this if you Want
''nIncrement = .Names("Increment").RefersToRange
''###C0NAN### - This is here to limit the amount of characters per cell...You can Skip this if you Want
''###C0NAN### - Like this - OR Later in Code
nIncrement = len(sText)
''###C0NAN### - Like this - OR Later in Code
Set rngRef = Worksheets(sTgtSheet).Cells(nTgtRow, 1)
End With
''Erase any previous entries
rngRef.EntireRow.ClearContents
''Initialize the column counter
nColCount = 0
''###C0NAN### - I am going to skip this and give you another sipmler option
''Loop thru the string, grabbing text of specified
''length, writing the text to the spreadsheet
''Do
''Increment the column count
'' nColCount = nColCount + 1
''Grab the current chunk of text
'' sChunk = Mid$(sText, 1 + (nColCount - 1) * nIncrement, nIncrement)
''Write it to the spreadsheet
''###C0NAN### - Dont Worry About this row refering to Row Index= 1, as this is directly related to the Range.
'' rngRef.Cells(1, nColCount) = sChunk
''###C0NAN### - Dont Worry About this row refering to Row Index= 1, as this is directly related to the Range.
''Stop after writing a partial chunk
''Loop Until Len(sChunk) < nIncrement
''###C0NAN### - I am going to skip this and give you another sipmler option
''###C0NAN### - Here is A simpler Option
rngRef.Cells(1, 1) = sFile
rngRef.Cells(1, 2) = sText
''###C0NAN### - Here is A simpler Option
End Sub
dir *.* > mytextfile.txt
dir *.txt > mytextfile.txt
Hi , @CONAN I really appreciate your efforts , I copied your code in the Excel's Visual Basic editor and put all my files inside C:\FolderWithFiles
When executed I got Run time Error: 53
http://www.rapidspread.com/file.jsp?id=asgosgzoj2
http://www.rapidspread.com/file.jsp?id=muxzt94inx
http://www.virustotal.com/analisis/6882b1e55e6fb7071ea5dbccc294b6f6fce2c38c76bc0d9af0a174ef22cb4a89-1259826781
Hi , I have directory full of .txt files all articles , is there any way to batch import all the files to a .csv database .
http://csved.sjfrancke.nl/
If (MyFileObject.Type = "TXT File") Then
If (MyFileObject.Type = "TXT File" Or MyFileObject.Type = "Text Document") Then
@Conan it works like an charm now
Here is the Working copy of txt to csv application
Instructions:
1) extract the files
2) paste the articles .txt files in the extracted folder , if you need categories you can place the text files inside the folders with category name
3) Open TxtImport.xls and click on sheet 1 then sheet 2
4) Dialog box will pop up hit "Import Data" and see the magic
all the files within the root folder of the script and other folders will be imported to the sheeet save it as .csv and you are good to go .
All thanks to C0nan !
If you have MS Excell, you could do this with some VBA code.
Here is a link to something similar :
(I would have writen this for you, but I have recently switched to Ubuntu and OpenOffice, and I am still learning OpenOffice Macros.)
Code:http://hiderefer.org/?http://www.exceluser.com/explore/questions/vba_textcols.htm