-
Jul 24th, 2024, 01:08 PM
#1
Thread Starter
New Member
How to find a word in a text file and extract the word before ?
Hello Everyone,
I'm currently working on a twinBasic script that reads lines from a text file and extracts specific information. However, I'm encountering an issue where my script always returns the first word of the file, regardless of the content.
Here's the context and the issue:
The Goal:
I want to read a file line by line and extract the first word of the line that contains a specific substring (fileName).
The output_file Content:
Here is an example of what the content of my output_file.txt looks like:
Code:
1PAoTP-3tBSlja3pn6L8SJK8XTpE3ptPV Programmation folder 2024-06-03 21:24:53
1ragISYh9uD9s2mjLA2IwUsmJKWnX5BRC Wow folder 2024-01-26 20:59:49
1huU9Li_zW08bPhuvO0F-wKYBd5MVgoCR Cheat-Engine folder 2024-01-03 21:25:21
1wPkQjWsm7o_XByAMC7vcFl9zV-qiU00R Roms folder 2018-04-02 17:37:59
1stuIK4gHaA4q3nceMhngebPj0IJqzR7a ISS folder 2023-02-03 12:32:58
1Ts32d7S_EC3QoAws6BmiyM-eDs-o0Npr Perso folder 2022-07-03 11:12:36
1FckLxGQu_FVigCm3swezsLm4M3tcKmWp save zenfone folder 2021-11-30 14:20:51
1I3VYvZobFuqlcnLKjxNVmcA-ZOq_p7H5 Estavayer folder 2021-06-27 10:20:11
1giXmE8VcE_gB4hY2fxNwfJbKZIHiCn4H Musique folder 2019-10-03 20:44:27
1B8qkyxMwLRe-ldTx75biUzuYX-rFFS5S KeePass folder 2019-07-31 18:53:46
0BzOrTyY65ohMazJRZVNGSkpiRnM Securité compte folder 2016-09-27 01:54:28
0BzOrTyY65ohMMUNIOVNZNG1oQnc Autres documents folder 2016-09-27 01:52:18
0BzOrTyY65ohMenhLM1RDME9QeVk Sono folder 2016-09-27 01:50:44
1rsMn8QtCxvn5xKpNBpumeKbLoRMy8Pim DBXV2.sav regular 1.2 MB 2024-07-21 17:41:17
1-QWYY_amb2XY1ppYJKzHj5k4MAmBDin1 RPGMaker XP.rar regular 23 MB 2024-05-28 20:26:13
its a list of my google drive files and i only need the hash/ID associated with DBXV2.sav which is "1rsMn8QtCxvn5xKpNBpumeKbLoRMy8Pim"
I came up with this function, with the help of chatgpt(I know i know ...)
Code:
Public Function GetFileID(fileName As String) As String
Dim fileID As String
Dim line As String
Dim fileNumber As Integer
Dim filePath As String
Dim pos As Integer
filePath = "D:\VB6\twinBASIC_IDE_BETA_586\gdrive_output.txt"
fileNumber = FreeFile
On Error GoTo ErrorHandler
Open filePath For Input As #fileNumber
Do While Not EOF(fileNumber)
Line Input #fileNumber, line
If InStr(line, fileName) > 0 Then
pos = InStr(line, " ")
If pos > 0 Then
fileID = Trim(Left(line, pos - 1))
Exit Do
End If
End If
Loop
Close fileNumber
GetFileID = fileID
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Description, vbCritical
If fileNumber > 0 Then Close fileNumber
GetFileID = ""
End Function
And i call it like this :
fileID = GetFileID("DBXV2.sav")
however it always return the first word of the document.
Can someone explain me what is wrong here ? Thanks guys.
-
Jul 24th, 2024, 08:34 PM
#2
Junior Member
Re: How to find a word in a text file and extract the word before ?
seems to be a file encoding related issue.
-
Jul 24th, 2024, 09:39 PM
#3
Re: How to find a word in a text file and extract the word before ?
fan2006 is right I believe; the function works for me with plain ANSI; tB supports other encodings for input but you need to specify, e.g. Open FilePath For Input Encoding utf-16 As #fileNumber
For the full list of supported encodings see here.
-
Jul 25th, 2024, 04:17 AM
#4
Thread Starter
New Member
Re: How to find a word in a text file and extract the word before ?
Thanks guys i managed to figured it out ! It was because the file was using "LF" line ending
-
Jul 25th, 2024, 04:29 AM
#5
Re: How to find a word in a text file and extract the word before ?
Irrespective of the Issue with LF
There is a unnecessary code here
Code:
Do While Not EOF(fileNumber)
Line Input #fileNumber, line
If InStr(line, fileName) > 0 Then
pos = InStr(line, " ")
If pos > 0 Then
fileID = Trim(Left(line, pos - 1))
Exit Do
End If
End If
Loop
shorter
Code:
Do While Not EOF(fileNumber)
Line Input #fileNumber, line
pos = InStr(line, fileName)
If pos > 0 Then
fileID = Trim(Left(line, pos - 1))
Exit Do
End If
Loop
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
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
|