|
-
May 21st, 2013, 02:39 PM
#1
Thread Starter
Junior Member
reading through multiple text files and putting content into new files
Hi Everyone,
I have a folder with multiple text files in it. I want to write a code that can read through these and organize the data into new files based on the values in the columns. So for example, to loop through and from 1,18 until it reaches 11,5 and take this data and put into new file. Then do that again continuously through all the txt files (the data is in separate folders so the first txt file may end at 7,10 so i would want to continue to the next file where it will pick up until it reaches 11,5).
I hope this makes sense.
Is this even possible?
-
May 21st, 2013, 04:39 PM
#2
Re: reading through multiple text files and putting content into new files
yes i am sure it is
use DIR to loop through all files in folder
use OPEN to open text files
use SPLIT to put the text file into an array of the lines in file
as text files do not have columns reading the columns depends how the file is set up
more information would be required to give any more help
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 22nd, 2013, 03:37 PM
#3
Thread Starter
Junior Member
Re: reading through multiple text files and putting content into new files
more information would be required to give any more help
I have the following code which converts all the .Htxt files i have in the folder to xlsx files. The files are output from a program that runs over night, which is the problem because the results for a run span over two days (the files automatically cutoff at midnight and then starts new file). So essentially the first part of every file needs to be at the end of the previous one (or the end of each needs to be at the beginning of the next. I am not sure how to reassign based on the beginning and end conditions. For example each run starts when column F has the value 1 and Column G has the value 18 and each ends when the values are 11 and 5.
Code:
Sub ConvertFiles()
Dim strFile As String
Dim strPath As String
With Application
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
End With
'Turn off events, alerts & screen updating
strPath = "C:\Users\kdunnigan\Documents\Lyo\Freeze Dryer log by day\"
strFile = Dir(strPath & "*.txt")
'Change the path as required
Do While strFile <> ""
Workbooks.Open Filename:=strPath & strFile, Format:=2
strFile = Mid(strFile, 1, Len(strFile) - 4) & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close True
strFile = Dir
Loop
'Opens the Workbook, set the file name, save in new format and close workbook
With Application
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
End With
'Turn on events, alerts & screen updating
End Sub
-
May 22nd, 2013, 04:33 PM
#4
Re: reading through multiple text files and putting content into new files
The files are output from a program that runs over night,
are these multiple files? or 2 files each night?
are the date times on the text file of help?
the text files could possibly be appended based on date time
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 22nd, 2013, 08:29 PM
#5
Thread Starter
Junior Member
Re: reading through multiple text files and putting content into new files
are these multiple files? or 2 files each night?
are the date times on the text file of help?
the text files could possibly be appended based on date time
multiple files. Each file goes from 12:00am till 11:59 pm. then a new one starts. That's the issue. But the times the cycles start and end are different depending on ramp rate and stuff. But the values in the columns i mentioned always contain those values when it starts/ends. But the beginning end are often in two separate files if it runs overnight.
So I think the best would be to create a loop and assign values to new files using multiple conditions? I am just not sure exactly how to do this...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|