PDA

View Full Version : [SOLVED:] Reading text files without .txt extensions



khu
04-24-2014, 07:48 PM
I'm working with output files from an in-house finite element program and the solution files are just text files with custom extensions (.bms, .loadcase, .spc, etc.). I'm trying to read these into dictionaries, but I'm having issues getting into the files.

If I navigate to the files outside of VBA, i get the "program not found, please select a program to open with" or whatever it says. If I do this once with NotePad, it's in the system memory and I can then access the one file through VBA, but this would be inefficient to do with almost hundreds of files.

I'd like this to run without opening NotePad, like running an FSO stream or Open...For method.

Currently I'm looping through an output folder containing all the files until I find a specific extension, then attempting to read like this





file = dir(dataform.file1) 'user selects a file in the root folder with getopenfilename

do while file <> ""
if instr(file, ".bms") > 0 then
open file for Random as #1 ''''''''Kicks me out here''''''''''
'do things with awesome code
end if
file = dir
loop




Any input is appreciated, thanks!

khu

snb
04-25-2014, 12:32 AM
open "G:\OF\Filename.bms" for input as #1
c00=Input(LOF(1),1)
close

p45cal
04-26-2014, 02:33 AM
If there's more than one .bms file, I don't think it'll like using #1 again unless #1 is closed first.
Check out Freefile in excel vba help.

Paul_Hossler
04-27-2014, 02:01 PM
If I do this once with NotePad, it's in the system memory


I'm not sure why you need to open it with Notepad first since VBA doesn't really care




Dim iFileNum as long

file = dir(dataform.file1) 'user selects a file in the root folder with getopenfilename

Do While[/COLOR] file <> ""
If instr(file, ".bms") > 0 Then
iFileNum=FreeFile
Open file ForRandom As #iFileNum
'do things with awesome code
Close #Ifilename


Else instr(file, ".load") > 0 Then
iFileNum=FreeFile
Open file For Random As #iFileNum
'do things with awesome code
Close #Ifilename


etc.


End If
file = dir
Loop