Delete known line in .txt file VBA

Oukapaka Posted messages 120 Status Member -  
lermite222 Posted messages 9042 Status Contributor -
Hello, I know that there are messages and topics concerning this question, but for now, I haven't received the answers I wanted.

So I have a .txt file of at least 20MB that comes out with about 120,000 lines. This is an extraction, so there’s not much I can do to modify the file. I absolutely need to delete the first line in order to process the rest of the document (the first line is useless and has no interest for the processing). Deleting this line would allow me to import my file with a specialized template, so everything is planned. But I can't manage to delete my first line.

Here is my code (it works on small files, but on mine, which contains black squares of line breaks, it deletes the entire document. Annoying...)

Sub suppr_1e_ligne() Dim colLignes As New Collection Dim ff As Integer ff = FreeFile Dim sLigne As String 'Reading the file, sending each line to the collection Open "C:\Documents and Settings\sghn4628\Desktop\Sujet PFC\FUQ_MOBISTORE_15314CEN_19122011.txt" For Input As #ff While Not EOF(ff) Line Input #ff, sLigne colLignes.Add sLigne Wend Close #ff 'Deleting lines 5 and 11 for example colLignes.Remove 1 'Rewriting the file Dim i As Integer Open "C:\Documents and Settings\sghn4628\Desktop\Sujet PFC\FUQ_MOBISTORE_15314CEN_19122011.txt" For Output As #ff For i = 2 To colLignes.Count Print #ff, colLignes(i) Next i Close #ff End Sub


Configuration: Windows XP / Firefox 9.0.1

20 answers

  1. Oukapaka Posted messages 120 Status Member 5
     
    Either

    Or
    Either

    The first code is designed to enter the file and output, and I keep encountering a recurring error: runtime error 55: The file is already open, even though it's not at all.
    3
  2. Oukapaka Posted messages 120 Status Member 5
     
    And could you add a clarification for me? How could we go from a final Access table to X text files (with semicolons and everything as separators, I'm totally stuck on this) corresponding to a parameter from another Access table?
    Basically, the algorithm would give something like
    X referring to the Reference table
    X = X+1
    For X = 1, create X text files (or csv, I’m not picky about the extension and format).

    Knowing that I won’t have X tables in my database, I prefer to separate them in code otherwise it might be too long with the Access engine.

    Thanks a lot for the first part already =)
    1
    1. lermite222 Posted messages 9042 Status Contributor 1 199
       
      I didn't get it at all :-)
      Give a concrete example
      0
    2. Oukapaka Posted messages 120 Status Member 5
       
      And well, my text file is filled, the goal is to divide it according to a choice I already know and keep the same format.
      The format being:
      field1;field2;field3;fieldX
      value1;;value3,valueX
      This is just an example of course. I have 37 fields like this and they are not always filled, so leave nothing in the empty spaces and put the values in quotes when there is a corresponding value. I hope I have been clearer =)
      0
    3. lermite222 Posted messages 9042 Status Contributor 1 199
       
      Not really,
      Basically, you want to create Csv files that include 1 or more rows from the table while keeping the columns so that you can open them in Excel with the correct columns, right?
      0
    4. Oukapaka Posted messages 120 Status Member 5
       
      I have an indicator in my table that differentiates between PFCs and PDVs. A PFC contains PDVs (basically, it's the parent link). I want to create a CSV for each PFC, so I run a query in VB by entering the PFC as parameters, using a counter or a loop for each one, and then I end up with a small file for each PFC instead of one large one where everything is grouped together. I'm not sure if it's clearer, but let me know if you need more details. And unfortunately, I can't attach the file due to economic reasons and confidentiality policies...
      0
  3. lermite222 Posted messages 9042 Status Contributor 1 199
     
    It's starting to come... :-)
    So you have 37 columns in your PFC, but how many rows?

    After executing a PFC query, you want to create a file, but for what destination?
    --
    If you bang into a pot and it sounds hollow, it doesn't necessarily mean the pot is empty. ;-)(Confucius)
    NOTE: I do not reply to private messages for technical questions.
    1
  4. lermite222 Posted messages 9042 Status Contributor 1 199
     
    Read my question
    You don't answer it explicitly
    --
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that is empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    1
  5. Oukapaka Posted messages 120 Status Member 5
     
    And yet, this is indeed the exact code I have in my file ^^' but the error keeps occurring. And always on the line with the Append.
    1
  6. lermite222 Posted messages 9042 Status Contributor 1 199
     
    A little rigor please.
    You declare: sLignes and you use sLigne (without S)
    You declare: ff1 and you use ff
    You forget to declare the collection
    ...
    To avoid all this, put Option Explicit at the top of the module
    But I still found your problem (I should have thought of it sooner :-))
    Sub suppr_1e_ligne() Dim sLignes As String Dim i As Long Dim ff1 As Integer Dim ff2 As Integer ff1 = FreeFile Open "C:\Documents and Settings\sghn4628\Desktop\Sujet PFC\FUM_MOBISTORE_15314CEN_31122011.txt" For Input As #ff1 ff2 = FreeFile 'HERE THE ERROR, Freefile gives the number of OPEN files +1 Open "C:\Documents and Settings\sghn4628\Desktop\Sujet PFC\FUM_MOBISTORE_15314CEN_31122011(test).txt" For Output As #ff2 i = 1 While Not EOF(ff1) Line Input #ff1, sLignes If i <> 1 Then Print #ff2, sLignes End If i = i + 1 Wend Close End Sub

    See you
    --
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to PMs for technical questions.
    1
  7. lermite222 Posted messages 9042 Status Contributor 1 199
     
    It would be simpler not to add the concerned lines.
     Dim i As Integer = 1 While Not EOF(ff) Line Input #ff, sLigne If i <> 5 And i <> 11 Then colLignes.Add(sLigne) End If i += 1 End While

    To avoid lines 5 and 11
    See you
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  8. lermite222 Posted messages 9042 Status Contributor 1 199
     
    A little simpler?
    Sub delete_1st_line() Dim colLines As New Collection Dim F1 As Integer, F2 As Integer F1 = FreeFile F2 = FreeFile Dim sLine As String, i As Long 'Reading file 1, writing to file 2 Open "C:\Documents and Settings\sghn4628\Desktop\Subject PFC\FUQ_MOBISTORE_15314CEN_19122011.txt" For Input As #F1 Open "C:\Documents and Settings\sghn4628\Desktop\Subject PFC\FUQ_MOBISTORE_15314CEN_19122011.txt" For Output As #F2 While Not EOF(F1) Line Input #F1, sLine i = i + 1 If i <> 5 And i <> 11 Then Print #F2, sLine End If Wend Close End Sub

    'Deleting lines 5 and 11 for example
    EDIT: I hadn't noticed it's the same file name.
    Rather use my first example.
    For this example, need to go through a temporary file; if time is a criterion, this example would be faster
    If you bump into a pot and it sounds hollow, it’s not necessarily the pot that is empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  9. Oukapaka Posted messages 120 Status Member 5
     
    Uh, the destination is just on my PC in .csv, and after that, the end users will manage it, there are no constraints on that level. The rows are variable, so I think it's acTransfertText with acExportDelim that needs to be used. But I can't seem to get it to work; I don't know why. Right now, my example has 122,000 rows with 60 PFC. So the rows will be distributed among these PFCs (not necessarily evenly).
    Basically, let's say that PFC is a general client that oversees different points of sale, and I'm being asked to divide it by these same PFCs. Thanks to you, the beginning of my Access import is valid; the import was done from a basic .txt file with a ; as the column separator. The import has already been tested and works; I just need the output where I already have my loop on the reference table ready. And the loop will therefore give:
    For X from 0 to EOF
    Create .txt files grouping the points of sale associated with it.

    And so I'm creating a query to create my reference and link the PDVs to the PFCs. And then comes the loop in the code and the output in X files (X = number of PFCs) txt if possible; that would be the best. Uh, I think I'm being as precise as possible. Thank you very much =)
    0
  10. lermite222 Posted messages 9042 Status Contributor 1 199
     
    I understand, but your table... wouldn't it be the file you mentioned earlier?
    If so, do you know (or could you know) exactly the number of rows per PFC?
    --
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  11. Oukapaka Posted messages 120 Status Member 5
     
    I can indeed know it, but I wanted to speed up the execution via VBA rather than making several queries in Access. However, there would be a huge number of tables or counts to make in order to get that information, and I wonder if it would be tedious without being useful.
    And if I understood your question correctly, I have three files at the beginning: PDV and the reference PFC. My goal is to split the PDV table into X tables, with X being the number of different PFCs.
    0
  12. lermite222 Posted messages 9042 Status Contributor 1 199
     
    You import the txt file into Access, why not review your PFCs and export them as text?
    Or maybe I didn't understand.
    --
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  13. Oukapaka Posted messages 120 Status Member 5
     
    Well, that's because creating a mini Access database is simpler; I can automate SQL queries in VB, which is way cooler/easier. And I’m being asked to do this to maintain a history/reference. It has to be in txt format; there doesn't seem to be any Excel use afterward. I was told this at the last minute because I was initially going with Excel, which would have greatly simplified the processing. And can we perform the same processing as Access directly in a text file by splitting everything into several different files?
    0
  14. Oukapaka Posted messages 120 Status Member 5
     
    Hello, I'm getting back to you after a few tests (I was ill recently and couldn't make progress on my work, so I apologize for the silence). So I've tested both of your solutions and I keep getting "File already open" and I've even tried creating a new file and copying my data into this new file (naming it similarly, just adding "test" at the end of the file name).
    The other code, on the other hand, tells me that the file access mode is incorrect (I’m going through an open: ...) or otherwise if I give it an input and an output it tells me that the file is already open. Anyway, I don’t really understand and I feel somewhat lost on this. I appreciate the help you've provided before and any future assistance if you take the time to respond again =)
    0
  15. lermite222 Posted messages 9042 Status Contributor 1 199
     
    It's not that I don't want to follow, but I don't understand anything, put your code in a next post, I'll see if I can adapt it.
    --
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  16. lermite222 Posted messages 9042 Status Contributor 1 199
     
    your file ..
    FUM_MOBISTORE_15314CEN_31122011(test).txt
    Does it really exist? I doubt it. !!

    --
    If you hit a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to PMs for technical questions.
    0
  17. Oukapaka Posted messages 120 Status Member 5
     
    It is empty but it exists; I created it specifically for this (hence the little test at the end to see if it worked better by duplicating the file in bulk). And that's where I'm stuck; I don't see why I'm being told the file is already open on the output when it is there and just waiting to be filled.
    0
  18. lermite222 Posted messages 9042 Status Contributor 1 199
     
    It's likely that I understood; if you use Output, you can only add once.
    I should do some tests but still try..
    Open "C:\Documents and Settings\sghn4628\Desktop\Sujet PFC\FUM_MOBISTORE_15314CEN_31122011(test).txt" For Output As #F2

    by replacing Output with Append.
    I think it should work.
    Otherwise,.. you say I will look into it more deeply.
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  19. Oukapaka Posted messages 120 Status Member 5
     
    The append always gives me the same response. I don't feel like I'm making any declaration errors, omissions, or anything like that. And then this error 55 remains something I didn't really think was possible: a file open when nothing is open (I also tested to see if leaving the folder open was causing a problem, but nothing changes; I really don't understand this thing. And honestly, I don't know what to do anymore; I'm stunned by such absurdities... Thank you again for the trouble you're going through for me =)
    0
  20. lermite222 Posted messages 9042 Status Contributor 1 199
     
    I don't feel like you replaced Output with Append.
    Anyway... a little calm would be much better.
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that is empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0