Results 1 to 13 of 13

Thread: [RESOLVED] search & replace in notepad

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Resolved [RESOLVED] search & replace in notepad

    Hello everyone

    i need to knw how can i search a .txt file for all words between brackets ()

    if the word was (code) then
    nothing;

    else
    do a replacement for each letter
    the replacement based on a table that i have
    but i need to know how can i replace each letter by another one


    it's a kind of decrypting system

    thanks all

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: search & replace in notepad

    Try this:

    VB Code:
    1. Dim pos1 As Long, pos2 As Long, i As Long
    2. Dim file As String, line As String
    3.  
    4.     file = vbNullString
    5.    
    6.         Open "c:\test.txt" For Input As #1
    7.             Do While Not EOF(1)
    8.                 Line Input #1, line
    9.                     i = i + 1
    10.                     pos1 = InStr(1, line, "(")
    11.                     pos2 = InStr(1, line, ")")
    12.                    
    13.                         If pos1 > 0 And pos2 > 0 Then
    14.                             line = Left(line, pos1 - 1) & "(" & "replacement" & ")" & Right(line, Len(line) - pos2)
    15.                         End If
    16.                        
    17.                             If i = 1 Then
    18.                                 file = line
    19.                             Else
    20.                                 file = file & vbCrLf & line
    21.                             End If
    22.             Loop
    23.         Close #1
    24.        
    25.             Open "c:\test.txt" For Output As #1
    26.                 Print #1, file
    27.             Close #1

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: search & replace in notepad

    Yes Gavio but what if the word was (code)
    and where should i write the replacement ?

    sorry because i'm new in VB

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: search & replace in notepad

    VB Code:
    1. Dim replacementStr As String
    2.     replacementStr = "something" 'define it here
    3.         'other code...
    4.         'then change this line
    5.             line = Left(line, pos1 - 1) & "(" & [B]replacementStr[/B] & ")" & Right(line, Len(line) - pos2)
    Then everything between () will be equal to replacementStr. The code basically opens the file and check every line for existance of (). If they are found, it replaces it.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: search & replace in notepad

    ohhh gavio i need to change every letter by another one

    i have a replacement letter for each alphabetical letter

    for example if the word was (Yes)
    then
    replase Y with n
    replase e with o
    replase s with t
    so it will become (not)

    and so on for all letters


    this proceduer should be applied in all cases execpt if the word was (code) then it should be left as it is

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: search & replace in notepad

    VB Code:
    1. Dim letters(25) As String * 1
    2.     letters(0) = "z" 'instead of a
    3.     letters(1) = "y" 'instead of b
    4.     letters(2) = "x" 'instead of c
    5.     letters(4) = "w" 'instead of c
    6.     letters(5) = "v" 'instead of c
    7.     '...
    8.     'define all the letters up to letters(25)
    9.  
    10. Dim pos1 As Long, pos2 As Long, i As Long
    11. Dim file As String, line As String, word As String
    12.     file = vbNullString
    13.     i = 0
    14.         Open "c:\test.txt" For Input As #1
    15.             Do While Not EOF(1)
    16.                 Line Input #1, line
    17.                     i = i + 1
    18.                     pos1 = InStr(1, line, "(")
    19.                     pos2 = InStr(1, line, ")")
    20.                         If pos1 > 0 And pos2 > 0 Then
    21.                             word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
    22.                            
    23.                                 word = Replace(word, "a", letters(0))
    24.                                 word = Replace(word, "b", letters(1))
    25.                                 word = Replace(word, "c", letters(2))
    26.                                 word = Replace(word, "d", letters(3))
    27.                                 word = Replace(word, "e", letters(4))
    28.                                 '...
    29.                                 'define all replacements up to letters(25)
    30.                                
    31.                             line = Left(line, pos1 - 1) & "(" & word & ")" & Right(line, Len(line) - pos2)
    32.                         End If
    33.                             If i = 1 Then
    34.                                 file = line
    35.                             Else
    36.                                 file = file & vbCrLf & line
    37.                             End If
    38.             Loop
    39.         Close #1
    40.        
    41.             Open "c:\test.txt" For Output As #1
    42.                 Print #1, file
    43.             Close #1
    44.                 MsgBox "Done!"

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: search & replace in notepad

    ok it's working but what if the word was (code) i need to keep it as it is

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: search & replace in notepad

    Ohh... Just add a new If block:

    VB Code:
    1. 'in this part...
    2.  
    3. word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
    4.    
    5.     [B]If LCase(word) <> "code" Then[/B]
    6.  
    7.         word = Replace(word, "a", letters(0))
    8.         word = Replace(word, "b", letters(1))
    9.         word = Replace(word, "c", letters(2))
    10.         word = Replace(word, "d", letters(3))
    11.         word = Replace(word, "e", letters(4))
    12.  
    13.         '...
    14.         'define all replacements up to letters(25)
    15.  
    16.         line = Left(line, pos1 - 1) & "(" & word & ")" & Right(line, Len(line) - pos2)
    17.     [B]End If[/B]
    18.  
    19.         '...

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: search & replace in notepad

    thanks alot gavio

  10. #10
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: search & replace in notepad

    Quote Originally Posted by om-yousif
    thanks alot gavio
    No problem Pulll down the thread tools menu and mark thread resolved.

  11. #11
    Member
    Join Date
    Aug 2006
    Location
    Arabian Gulf
    Posts
    46

    Re: [RESOLVED] search & replace in notepad

    Hi gavio...
    i want you to help me if you have any idea to how can we run python2.4 in vb6?

    thanks a lot

  12. #12
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: [RESOLVED] search & replace in notepad

    Quote Originally Posted by bodoora
    i want you to help me if you have any idea to how can we run python2.4 in vb6?
    What the heck does that mean??

  13. #13
    Member
    Join Date
    Aug 2006
    Location
    Arabian Gulf
    Posts
    46

    Re: [RESOLVED] search & replace in notepad

    thanks for read my message...
    i have code in python that will read a file (in notepade) and replace the word
    it work like the replace code above..
    i want to join python language with vb6.. and these (python) is can be run in command prompt (DOS).

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