Results 1 to 9 of 9

Thread: [RESOLVED] Operation on a text (.txt) file using excel macro

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    10

    Resolved [RESOLVED] Operation on a text (.txt) file using excel macro

    Hi all,
    i am working on a VBA code.
    I want to open a text (.txt) file in notepad (Not in excel) using excel VBA. after opening this text file, i want to delete some specific repeated words in that text file. then save this file (after deletion) with a new name with .txt extension.
    so my questions are, 1) first of all, is it possible to do using excel VBA??
    2) if yes, please get me started with this, share any data, web link or code you have. if No, please suggest what can be other possible way to do this instead of excel VBA.

    please suggest, really need your help here.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Operation on a text (.txt) file using excel macro

    is it possible to do using excel VBA??
    though you would not open in notepad

    open file, read into memory, modify as required, write to file with new name (old file is not changed)

    vb Code:
    1. f = freefile
    2. open "sompath\filename.txt" for input as f
    3. strfile = input(lof(f), #f)     ' read whole file into variable
    4. close f    ' no need to keep file open
    5. ' do stuff here to delete words in strfile
    6. f = freefile
    7. open "someotherpath\newfile.txt" for output as f
    8. print #f, strfile
    9. close f
    if you want to delete the original file use the kill statement, or you could just overwrite it
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    10

    Re: Operation on a text (.txt) file using excel macro

    Hi,
    Thanks a lot for replying.
    I understood the code which you replied but i am new to file I/O programming so i don't know how to perform deletion.
    basically i have many strings (e.g. "abc * gt") in the text file which i need to delete. these strings have same starting & ending name but the in between data can be different.
    So please suggest how to perform this??

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Operation on a text (.txt) file using excel macro

    there would be several ways to do what you require, you would need to post a sample of your text file with clear details of which strings you want to delete
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    10

    Re: Operation on a text (.txt) file using excel macro

    Hi,
    thanks for reply.

    I am attaching the text file here.

    in this test file i want to delete following string
    "Bye...
    Output has been logged to file /var/opt/ericsson/amos/moshell_logfiles/v46248C/logs_mobatch/2012-01-20/site.mo/15-38/CTL01270.log
    ".

    such strings are repeated in this text file with same start & end name but in between data can be different.

    i am using replace function, i am able to replace a continuous string with a "". but when the string has multiple lines then i am not able to do so. please suggest how can i do this with replace function ??
    or
    is there any other way to do that??
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Operation on a text (.txt) file using excel macro

    you would need to find the start position of each "Output has been logged", then the subsequent, corresponding end position of ".log", then concantenate the strings before and after those positions, so the text is deleted by omission
    use instr function the find the start and end positions and left and mid functions to join the strings

    try like
    vb Code:
    1. do
    2.   strt = instr(strfile, "Output has been logged")
    3.   if strt = 0 then exit do    ' not found finish
    4.   nd = instr(strt, strfile, ".log") + 5
    5.   strfile = left(strfile, strt - 1) & mid(strfile, nd)
    6. loop
    not tested
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Operation on a text (.txt) file using excel macro

    I have had a look at you file... may i suggest you look at it in write the format will be more apparent.

    The problem is that the "whole file read in" process is possibly not the best solution for this problem "line by line" might be better.

    BUT you can use the split() technique to work through the problem...

    It appears that the bye... and the logging file information cover more than 1 line and the file info is too random for a simple replace() to work...

    SO by splitting() the whole file input in to an array you would simply walk the array until you find the starting sentinel "bye" and delete the contents or do not rewrite the contents depending on how you wish to proceed, and continue to delete until you hit the ending sentinel "ends ...log" at which point you start looking for the next start sentinel

    hope this helps

    I can send code if needed ( later i am needing to travel some 100 miles just now )

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    10

    Re: Operation on a text (.txt) file using excel macro

    hi guys,
    thanks a lot for replying.
    i have resolved the issue with the help of codes sent by westconn1. thanks again.

    for now, my code is working perfectly fine. i am closing this thread now.
    if i have any further issue, i will bother you guys again ..

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    10

    Re: [RESOLVED] Operation on a text (.txt) file using excel macro

    Hi guys,
    Need your help again.
    I have installed cygwin software (Unix) in my computer. Now i want to create a shell script (.sh file)/other script which is equivalent of VBA code (at the bottom) and then put this .sh file into bin directory of c:/cygwin.
    so when i login into cygwin and type some command, it should be able to perform the same function as in VBA code.
    Please help me out with this script and its programming.


    VBA code:
    Sub Macro3()
    sFile = Application.GetOpenFilename("All files (*.*), *.*", 1, "Select Input File", "Open", False) 'asks for file location
    If TypeName(sFile) = "Boolean" Then ' if input file "Kget_utran cell" is not provided, exit.
    Exit Sub
    End If
    f = FreeFile
    Open sFile For Input As f
    strfile = Input(LOF(f), #f) ' read whole file into variable
    Close f ' no need to keep file open
    Do
    strt = Strings.InStr(strfile, "Bye...") 'Gives start value of this string in the input file.
    If strt = 0 Then Exit Do ' not found finish
    nd = InStr(strt, strfile, ".log") + 5
    pp = Mid(strfile, (strt - 16), (nd - strt + 16))
    strfile = Replace(strfile, pp, "")
    Loop
    Length = Len(sFile)
    j = 1
    rr = 1
    Do Until rr = 0
    rr = InStr(j, sFile, "\")
    If rr = 0 Then
    GoTo here
    End If
    j = rr + 1
    Loop
    here: y = Left(sFile, j - 2)
    Open y & "\output.txt" For Output As f 'saves at this location
    Print #f, strfile
    Close f
    MsgBox "DONE"
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width