PDA

View Full Version : select based on number of charcters in row



knifelong
03-04-2009, 04:02 AM
Hi.

I'm looking to loop through a number of text files and select only the ones where the length of the first row (could be any row) is greater than 12 characters (for example)

Thanks

CreganTur
03-04-2009, 06:28 AM
If the text files stay constant- the names are the same, in the same folder, then you could use an array to open each file using the OpenTextFile method (shown below). If you need to open different files in different folders, then you can use this kb article (http://www.vbaexpress.com/kb/getarticle.php?kb_id=245) from MD to give you an idea on how you can loop through all of the files in a folder.

This code will let you read the first line of a text file. You will need a reference to Windows Script Host Object Model.

Sub ReadFileLine(srcFileName As String)
Dim fso As New IWshRuntimeLibrary.FileSystemObject
Dim streamIn As IWshRuntimeLibrary.TextStream
Dim srcFile As IWshRuntimeLibrary.File

'open srcFile
Set srcFile = fso.GetFile(srcFileName)
Set streamIn = srcFile.OpenAsTextStream(ForReading, TristateUseDefault)

'check length of first line
If Len(streamIn.ReadLine) > 12 Then
'do stuff
End If

'cleanup
streamIn.Close
Set streamIn = Nothing

Feed the full filepath to the text file you want to read for the srcFileName parameter.

This is written from memory and untested... but it should point you in the right direction.

HTH:thumb