|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Change/remove text from file
I want to remove the specified text from a text file or i can change the text to ""
|
|
#2
|
||||
|
||||
|
Quote:
Can you give a little bit more information and post any code that you have already? Do you know how to open the text file? Not tested, but try something like this to loop through the contents of a text file, remove any occurances of a certain string, and re-write the file: Code:
Dim aLine, searchCriteria, outputString As String searchCriteria = "Whatever you want to search for" outputString = "" open "C:\test.txt" for input As #1 While NOT EOF(1) Line Input #1, aLine If InStr(aLine, searchCriteria) Then aLine = Replace(aLine, searchCriteria, "") End If outputString = outputString & aLine & vbCrLf Wend Close #1 Open "C:\test.txt" for output As #1 Print #1, outputString Close #1 If this isn't what you are looking for you will have to post a detailed description of your problem. |
|
#3
|
|||
|
|||
|
Quote:
I know how to write text to a file i know how to over write text in a file and how to open a file but i cant remove text that is surrounded by other text with out affecting the other text. |
|
#4
|
||||
|
||||
|
Quote:
I dont think there is any way you can achieve what you want in a standard text file, unless it is set up for Random access. What format is your text file in? And is there any reason why you cant do as per my example and just recreate the text file without the bit of text that you want to remove? |
|
#5
|
|||
|
|||
|
could do a specific text search, then space() the number of chrs to replace the text with white space?
|
|
#6
|
|||
|
|||
|
Do you have any other info you can share?
For instance let's say the string in your file is: Code:
XXX THIS IS MY TEST STRING XXX and you want to remove the string "TEST" from the file you want it to appear when you're done as: Code:
XXX THIS IS MY STRING XXX or Code:
XXX THIS IS MY STRING XXX Code:
Sub main()
Dim fs
Dim f1
Dim f2
Dim sSourceFile as string
Dim sTargetFile as string
Dim sSearchString as string
Dim sline As String
sSourceFile = "c:\TestText.txt"
sTargetFile = "c:\TestTextTemp.txt"
sSearchString = "TEST"
Set fs = CreateObject("Scripting.filesystemobject")
Set f1 = fs.opentextfile(sSourceFile)
Set f2 = fs.createtextfile(sTargetFile)
Do While f1.atendofstream <> True
sline = f1.readline
'Use this line to replace with a string of equal length spaces
'sline = Replace(sline, sSearchString,
String(Len(sSearchString), " "))
'Use this line to replace the string with a blank
'sline = Replace(sline, sSearchString, "")
f2.writeline sline
Loop
f1.Close
f2.Close
fs.deletefile sSourceFile
fs.movefile sTargetFile, sSourceFile
End Sub
This will replace every instance of your search string with your replacement string on any given line. When the code is done running it will delete your original file and rename the temporary file to the original file name.
__________________
---------------- If we've helped you and you have solved your problem please post that it's been resolved so we know! The suspense kills me! |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Change/remove text from file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|