Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Copy Loop 2

Status
Not open for further replies.

Fendal

Technical User
Sep 13, 2005
178
GB
Hi, All

I'll just take a minute to set the scene, I have 250 folders each folder contains a text file,
eg: C:\folder1\file1.txt, C:\folder2\file2.txt, C:\folder3\file3.txt ...and so on, within each of
those *.txt files there are 5 lines of text, what i'm hoping to be able to do is loop through
the files with FSO then read and copy the last 5 characters on the second line of every *.txt
file into a textbox in my project form.

any advice, code or links would be great, thanks
 
I think you already got the code to loop through the files in your renamer post. All you have to do is adjust it so that it does not rename but open the files. Just grab the name of the file from the file object and open it using

'declarations
Dim intFileNumber as Integer
dim strLine as string
'get free file
intFileNumber = FreeFile
'open file
Open "C:\folder1\file1.txt" for Input as #intFileNumber
'skip first line
Line Input #intFileNumber, strLine
'get second line
Line Input #intFileNumber, strLine
'get right 5 chars
msgbox right(strline,5)

There are other ways of doing it, I'm sure, but I think this might be the easiest. Just adjust the code a bit.

-Max
 
Thanks Max, I'll give that a try.
 
Do you know how to loop through all the text files only programmatically?
-or-
If you add a folder browser control, use a for loop with variable "i", so to get folder<i> and file<i>.txt ...

As for the 2nd line, you need after opening the text file for reading to read two times. The 1st for the 1st line and the 2nd for the 2nd line. At this point you ll use a string var to store the 2nd line and:TextBox1.Text = CStr(Right(SrtingVar,5))
 
That Works Perfect, Thanks Again Max.

and Thanks TipGiver.
 
And here's the evil one-liner way. Assuming that strFilepath contains e.g "C:\folder1\file1.txt":

MsgBox Right$(Split(CreateObject("scripting.filesystemobject").OpenTextFile(strFilepath).ReadAll, vbCrLf)(6), 5)
 
Thanks for that, StrongM, I've seen some of these one liners
on this forum before, I don't tend to use them even though
they do look good, mainly because I'm a little dim and don't
understand them.

My additional question however is, why do people call these
one line codes the "evil" way ?, also is it a common term,
I haven't noticed it on other sites.
 
'Evil' has been tossed around a bit lately. In my opinion, his method is 'evil' because it is a one liner. His algorithm could easily be broken in to several lines. With the one line approach, it is more difficult to understand, and therefore, not good programming practice. In my opinion, it is probably more important for code to be readable then how it performs. Undoubtedly, you will need to tweak this function later. Trying to understand what is happening (six months from now) will be difficult with the 'evil' approach.

Code:
Dim FSO as scripting.FileSystemObject
dim arData() as string
dim strAllData as string

Set FSO = CreateObject("Scripting.FileSystemObject")
strAllData = FSO.OpenTextFile(strFilePath).ReadAll
arData = split(strAllData, vbcrlf)

Debug.Print Right$(arData(6), 5)
Erase arData
Set FSO = Nothing

Seeing the algorithm broken out like I have done, makes it much more understandable.

Personally, I think StrongM is just looking for Job Security. [bigsmile]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
>Personally, I think StrongM is just looking for Job Security.

[tongue]

Fortunately, I'm not a programmer.
 
Oh - and one additional point. Getting down to the single line solution often means that you've managed to eliminate any extraneous mucking about from your solution.

For maintenance reasons in production code you might want to break the single line solution out a little (as per George's illustration), but what you'll have (or should have) when you do that is the essence of the problem
 
<Fortunately, I'm not a programmer.

strongm is actually third violinist in the London Symphony Orchestra, and just does this for fun in his spare time.

Sorry to blow your cover, strongm. I'm afraid I'm not very good at keeping secrets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top