|
-
Aug 27th, 2006, 12:25 PM
#1
Thread Starter
Addicted Member
[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
-
Aug 27th, 2006, 01:06 PM
#2
Re: search & replace in notepad
Try this:
VB Code:
Dim pos1 As Long, pos2 As Long, i As Long
Dim file As String, line As String
file = vbNullString
Open "c:\test.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, line
i = i + 1
pos1 = InStr(1, line, "(")
pos2 = InStr(1, line, ")")
If pos1 > 0 And pos2 > 0 Then
line = Left(line, pos1 - 1) & "(" & "replacement" & ")" & Right(line, Len(line) - pos2)
End If
If i = 1 Then
file = line
Else
file = file & vbCrLf & line
End If
Loop
Close #1
Open "c:\test.txt" For Output As #1
Print #1, file
Close #1
-
Aug 27th, 2006, 01:16 PM
#3
Thread Starter
Addicted Member
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
-
Aug 27th, 2006, 01:19 PM
#4
Re: search & replace in notepad
VB Code:
Dim replacementStr As String
replacementStr = "something" 'define it here
'other code...
'then change this line
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.
-
Aug 27th, 2006, 01:24 PM
#5
Thread Starter
Addicted Member
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
-
Aug 27th, 2006, 01:45 PM
#6
Re: search & replace in notepad
VB Code:
Dim letters(25) As String * 1
letters(0) = "z" 'instead of a
letters(1) = "y" 'instead of b
letters(2) = "x" 'instead of c
letters(4) = "w" 'instead of c
letters(5) = "v" 'instead of c
'...
'define all the letters up to letters(25)
Dim pos1 As Long, pos2 As Long, i As Long
Dim file As String, line As String, word As String
file = vbNullString
i = 0
Open "c:\test.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, line
i = i + 1
pos1 = InStr(1, line, "(")
pos2 = InStr(1, line, ")")
If pos1 > 0 And pos2 > 0 Then
word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
word = Replace(word, "a", letters(0))
word = Replace(word, "b", letters(1))
word = Replace(word, "c", letters(2))
word = Replace(word, "d", letters(3))
word = Replace(word, "e", letters(4))
'...
'define all replacements up to letters(25)
line = Left(line, pos1 - 1) & "(" & word & ")" & Right(line, Len(line) - pos2)
End If
If i = 1 Then
file = line
Else
file = file & vbCrLf & line
End If
Loop
Close #1
Open "c:\test.txt" For Output As #1
Print #1, file
Close #1
MsgBox "Done!"
-
Aug 27th, 2006, 02:04 PM
#7
Thread Starter
Addicted Member
Re: search & replace in notepad
ok it's working but what if the word was (code) i need to keep it as it is
-
Aug 27th, 2006, 02:10 PM
#8
Re: search & replace in notepad
Ohh... Just add a new If block:
VB Code:
'in this part...
word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
[B]If LCase(word) <> "code" Then[/B]
word = Replace(word, "a", letters(0))
word = Replace(word, "b", letters(1))
word = Replace(word, "c", letters(2))
word = Replace(word, "d", letters(3))
word = Replace(word, "e", letters(4))
'...
'define all replacements up to letters(25)
line = Left(line, pos1 - 1) & "(" & word & ")" & Right(line, Len(line) - pos2)
[B]End If[/B]
'...
-
Aug 28th, 2006, 12:49 AM
#9
Thread Starter
Addicted Member
Re: search & replace in notepad
-
Aug 28th, 2006, 12:22 PM
#10
Re: search & replace in notepad
 Originally Posted by om-yousif
thanks alot gavio
No problem Pulll down the thread tools menu and mark thread resolved.
-
Sep 9th, 2006, 07:24 AM
#11
Member
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
-
Sep 9th, 2006, 09:23 AM
#12
Re: [RESOLVED] search & replace in notepad
 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??
-
Sep 9th, 2006, 01:01 PM
#13
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|