Replace a simple TXT

R

rebelscum0000

Dear all,

I have a .txt file and I want to replace some lines

Results I want:

==============================
Search Type: Find Files

C:\Documents and Settings\Antonio Macias\Desktop
A:\
C:\
C:\Documents and Settings
C:\Downloads
C:\Dups V.1.0
C:\DVD APPZ X
C:\DVD Basic_8116
C:\HP PMD P
C:\HP PMD P II
C:\HP PMD S
C:\MSOCache
C:\NO OS 80 G
C:\Program Files
C:\WINDOWS
D:\
E:\
C:\Documents and Settings\All Users\Documents
C:\Documents and Settings\Antonio Macias\My Documents
C:\Documents and Settings\Antonio Macias\Desktop\Appz Moved
C:\Documents and Settings\Antonio Macias\Desktop\FIX DVD Basic_8116
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S G
C:\Documents and Settings\Antonio Macias\Desktop\HP PMD S

Included Patterns: *nero*.*

This is my Original .txt File without modifications

----Start Original .txt File without modifications------
Specifications for Current Search
==============================

[ Session Options ]
Exclude Internet Cache Files: YES
Exclude Internet Cache Files (Duplicates): YES
Exclude Windows Folders: YES
Exclude Windows Folders (Duplicates): YES
Exclude in Program Files Folder: YES
Exclude Recycle Bin: YES
==============================

[ Search Options ]
==============================
Search Type: Find Files

Search Folder 1: C:\Documents and Settings\Antonio Macias\Desktop
Search Folder 2: A:\
Search Folder 3: C:\
Search Folder 4: C:\Documents and Settings
Search Folder 5: C:\Downloads
Search Folder 6: C:\Dups V.1.0
Search Folder 7: C:\DVD APPZ X
Search Folder 8: C:\DVD Basic_8116
Search Folder 9: C:\HP PMD P
Search Folder 10: C:\HP PMD P II
Search Folder 11: C:\HP PMD S
Search Folder 12: C:\MSOCache
Search Folder 13: C:\NO OS 80 G
Search Folder 14: C:\Program Files
Search Folder 15: C:\WINDOWS
Search Folder 16: D:\
Search Folder 17: E:\
Search Folder 18: C:\Documents and Settings\All Users\Documents
Search Folder 19: C:\Documents and Settings\Antonio Macias\My
Documents
Search Folder 20: C:\Documents and Settings\Antonio
Macias\Desktop\Appz Moved
Search Folder 21: C:\Documents and Settings\Antonio Macias\Desktop\FIX
DVD Basic_8116
Search Folder 22: C:\Documents and Settings\Antonio Macias\Desktop\Fix
HP PMD S
Search Folder 23: C:\Documents and Settings\Antonio Macias\Desktop\Fix
HP PMD S G
Search Folder 24: C:\Documents and Settings\Antonio Macias\Desktop\HP
PMD S

Included Patterns: *nero*.*

----End of the Original .txt File without modifications------

This is my code, which of course is not working; could someone please
help me to understand what I am doing wrong?

Thanks in Advance
Regards
Antonio Macias

----Code Start ------
Option Compare Database
Sub Reading_TxtFile_R()

Dim OriginTxtPath As String
Dim DestinationTxtPath As String
Dim inFile As Integer
Dim outFile As Integer
Dim ReadText As String
Dim PatternsLine As String
Dim SearchFolderLine As String
Dim MyCounter As Integer

OriginTxtPath = "C:\Program
Files\Fineware\hound4\Searches\LastSearch.txt"
DestinationTxtPath = "C:\Dups V.1.0\LastSearchMacias.txt"
PatternsLine = "Included Patterns:"
SearchFolderLine = "Search Folder"
MyCounter = 0

'Obtain legal handles
'inFile = FreeFile
'Open OriginTxtPath For Input As #inFile
inFile = FreeFile()
'Open the files: this needs to be error-trapped properly
Open OriginTxtPath For Input As #inFile
outFile = FreeFile()
Open DestinationTxtPath For Output As #outFile

Do Until EOF(inFile) 'Loop until end of file.
Line Input #inFile, ReadText 'Read line into variable.

'Then you can read it line by line:

'Do something with the line.
If Left(ReadText, Len(SearchFolderLine)) = SearchFolderLine Then
MsgBox "Found Search Folder"
MyCounter = MyCounter + 1
End If

For FolderLineFound = 1 To MyCounter

SearchFolderLineReplace = Replace(SearchFolderLine & _
" " & FolderLineFound & ":", SearchFolderLine & " " & FolderLineFound &
_
":", "DO SOMETHING PLEASE!!!", , , vbTextCompare)
Debug.Print SearchFolderLineReplace
Next
'Write it out to the DestinationTxtPath
Print #outFile, ReadText
Loop 'Now go round again for the next line

'tidy up
Close #inFile
Close #outFile

End Sub
 
G

Gerwin Berentschot

I took a quick look at your code. Looks to me you're writing the line you
read in stead of the modified line in which you replaced a certain string.

Shouldn't
Print #outFile, ReadText
say
Print #outFile, SearchFolderLineReplace
?

Gerwin Berentschot
 
G

Gerwin Berentschot

Noticed something else. This code is in loop that is not needed and you are
doing nothing with the line read (ReadText).

Try this:
SearchFolderLineReplace = Replace(ReadText, SearchFolderLine & _
" " & FolderLineFound & ":", SearchFolderLine & " " & FolderLineFound &
_
":", "DO SOMETHING PLEASE!!!", , , vbTextCompare)

After this SearchFolderLineReplace contact the modifief ReadText. Write that
string to your output file (see my earlier posting).

--
Gerwin Berentschot
(e-mail address removed).(nospam)
Mr. Access - Access development and training



rebelscum0000 said:
Dear all,

I have a .txt file and I want to replace some lines

Results I want:

==============================
Search Type: Find Files

C:\Documents and Settings\Antonio Macias\Desktop
A:\
C:\
C:\Documents and Settings
C:\Downloads
C:\Dups V.1.0
C:\DVD APPZ X
C:\DVD Basic_8116
C:\HP PMD P
C:\HP PMD P II
C:\HP PMD S
C:\MSOCache
C:\NO OS 80 G
C:\Program Files
C:\WINDOWS
D:\
E:\
C:\Documents and Settings\All Users\Documents
C:\Documents and Settings\Antonio Macias\My Documents
C:\Documents and Settings\Antonio Macias\Desktop\Appz Moved
C:\Documents and Settings\Antonio Macias\Desktop\FIX DVD Basic_8116
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S G
C:\Documents and Settings\Antonio Macias\Desktop\HP PMD S

Included Patterns: *nero*.*

This is my Original .txt File without modifications

----Start Original .txt File without modifications------
Specifications for Current Search
==============================

[ Session Options ]
Exclude Internet Cache Files: YES
Exclude Internet Cache Files (Duplicates): YES
Exclude Windows Folders: YES
Exclude Windows Folders (Duplicates): YES
Exclude in Program Files Folder: YES
Exclude Recycle Bin: YES
==============================

[ Search Options ]
==============================
Search Type: Find Files

Search Folder 1: C:\Documents and Settings\Antonio Macias\Desktop
Search Folder 2: A:\
Search Folder 3: C:\
Search Folder 4: C:\Documents and Settings
Search Folder 5: C:\Downloads
Search Folder 6: C:\Dups V.1.0
Search Folder 7: C:\DVD APPZ X
Search Folder 8: C:\DVD Basic_8116
Search Folder 9: C:\HP PMD P
Search Folder 10: C:\HP PMD P II
Search Folder 11: C:\HP PMD S
Search Folder 12: C:\MSOCache
Search Folder 13: C:\NO OS 80 G
Search Folder 14: C:\Program Files
Search Folder 15: C:\WINDOWS
Search Folder 16: D:\
Search Folder 17: E:\
Search Folder 18: C:\Documents and Settings\All Users\Documents
Search Folder 19: C:\Documents and Settings\Antonio Macias\My
Documents
Search Folder 20: C:\Documents and Settings\Antonio
Macias\Desktop\Appz Moved
Search Folder 21: C:\Documents and Settings\Antonio Macias\Desktop\FIX
DVD Basic_8116
Search Folder 22: C:\Documents and Settings\Antonio Macias\Desktop\Fix
HP PMD S
Search Folder 23: C:\Documents and Settings\Antonio Macias\Desktop\Fix
HP PMD S G
Search Folder 24: C:\Documents and Settings\Antonio Macias\Desktop\HP
PMD S

Included Patterns: *nero*.*

----End of the Original .txt File without modifications------

This is my code, which of course is not working; could someone please
help me to understand what I am doing wrong?

Thanks in Advance
Regards
Antonio Macias

----Code Start ------
Option Compare Database
Sub Reading_TxtFile_R()

Dim OriginTxtPath As String
Dim DestinationTxtPath As String
Dim inFile As Integer
Dim outFile As Integer
Dim ReadText As String
Dim PatternsLine As String
Dim SearchFolderLine As String
Dim MyCounter As Integer

OriginTxtPath = "C:\Program
Files\Fineware\hound4\Searches\LastSearch.txt"
DestinationTxtPath = "C:\Dups V.1.0\LastSearchMacias.txt"
PatternsLine = "Included Patterns:"
SearchFolderLine = "Search Folder"
MyCounter = 0

'Obtain legal handles
'inFile = FreeFile
'Open OriginTxtPath For Input As #inFile
inFile = FreeFile()
'Open the files: this needs to be error-trapped properly
Open OriginTxtPath For Input As #inFile
outFile = FreeFile()
Open DestinationTxtPath For Output As #outFile

Do Until EOF(inFile) 'Loop until end of file.
Line Input #inFile, ReadText 'Read line into variable.

'Then you can read it line by line:

'Do something with the line.
If Left(ReadText, Len(SearchFolderLine)) = SearchFolderLine Then
MsgBox "Found Search Folder"
MyCounter = MyCounter + 1
End If

For FolderLineFound = 1 To MyCounter

SearchFolderLineReplace = Replace(SearchFolderLine & _
" " & FolderLineFound & ":", SearchFolderLine & " " & FolderLineFound &
_
":", "DO SOMETHING PLEASE!!!", , , vbTextCompare)
Debug.Print SearchFolderLineReplace
Next
'Write it out to the DestinationTxtPath
Print #outFile, ReadText
Loop 'Now go round again for the next line

'tidy up
Close #inFile
Close #outFile

End Sub
 
G

Gerwin Berentschot

After this SearchFolderLineReplace contact the modifief ReadText. Write that
string to your output file (see my earlier posting).

should be
After this SearchFolderLineReplace CONTAINS the modifief ReadText. Write that
string to your output file (see my earlier posting).


--
Gerwin Berentschot
(e-mail address removed).(nospam)
Mr. Access - Access development and training



Gerwin Berentschot said:
Noticed something else. This code is in loop that is not needed and you are
doing nothing with the line read (ReadText).

Try this:
SearchFolderLineReplace = Replace(ReadText, SearchFolderLine & _
" " & FolderLineFound & ":", SearchFolderLine & " " & FolderLineFound &
_
":", "DO SOMETHING PLEASE!!!", , , vbTextCompare)

After this SearchFolderLineReplace contact the modifief ReadText. Write that
string to your output file (see my earlier posting).

--
Gerwin Berentschot
(e-mail address removed).(nospam)
Mr. Access - Access development and training



rebelscum0000 said:
Dear all,

I have a .txt file and I want to replace some lines

Results I want:

==============================
Search Type: Find Files

C:\Documents and Settings\Antonio Macias\Desktop
A:\
C:\
C:\Documents and Settings
C:\Downloads
C:\Dups V.1.0
C:\DVD APPZ X
C:\DVD Basic_8116
C:\HP PMD P
C:\HP PMD P II
C:\HP PMD S
C:\MSOCache
C:\NO OS 80 G
C:\Program Files
C:\WINDOWS
D:\
E:\
C:\Documents and Settings\All Users\Documents
C:\Documents and Settings\Antonio Macias\My Documents
C:\Documents and Settings\Antonio Macias\Desktop\Appz Moved
C:\Documents and Settings\Antonio Macias\Desktop\FIX DVD Basic_8116
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S G
C:\Documents and Settings\Antonio Macias\Desktop\HP PMD S

Included Patterns: *nero*.*

This is my Original .txt File without modifications

----Start Original .txt File without modifications------
Specifications for Current Search
==============================

[ Session Options ]
Exclude Internet Cache Files: YES
Exclude Internet Cache Files (Duplicates): YES
Exclude Windows Folders: YES
Exclude Windows Folders (Duplicates): YES
Exclude in Program Files Folder: YES
Exclude Recycle Bin: YES
==============================

[ Search Options ]
==============================
Search Type: Find Files

Search Folder 1: C:\Documents and Settings\Antonio Macias\Desktop
Search Folder 2: A:\
Search Folder 3: C:\
Search Folder 4: C:\Documents and Settings
Search Folder 5: C:\Downloads
Search Folder 6: C:\Dups V.1.0
Search Folder 7: C:\DVD APPZ X
Search Folder 8: C:\DVD Basic_8116
Search Folder 9: C:\HP PMD P
Search Folder 10: C:\HP PMD P II
Search Folder 11: C:\HP PMD S
Search Folder 12: C:\MSOCache
Search Folder 13: C:\NO OS 80 G
Search Folder 14: C:\Program Files
Search Folder 15: C:\WINDOWS
Search Folder 16: D:\
Search Folder 17: E:\
Search Folder 18: C:\Documents and Settings\All Users\Documents
Search Folder 19: C:\Documents and Settings\Antonio Macias\My
Documents
Search Folder 20: C:\Documents and Settings\Antonio
Macias\Desktop\Appz Moved
Search Folder 21: C:\Documents and Settings\Antonio Macias\Desktop\FIX
DVD Basic_8116
Search Folder 22: C:\Documents and Settings\Antonio Macias\Desktop\Fix
HP PMD S
Search Folder 23: C:\Documents and Settings\Antonio Macias\Desktop\Fix
HP PMD S G
Search Folder 24: C:\Documents and Settings\Antonio Macias\Desktop\HP
PMD S

Included Patterns: *nero*.*

----End of the Original .txt File without modifications------

This is my code, which of course is not working; could someone please
help me to understand what I am doing wrong?

Thanks in Advance
Regards
Antonio Macias

----Code Start ------
Option Compare Database
Sub Reading_TxtFile_R()

Dim OriginTxtPath As String
Dim DestinationTxtPath As String
Dim inFile As Integer
Dim outFile As Integer
Dim ReadText As String
Dim PatternsLine As String
Dim SearchFolderLine As String
Dim MyCounter As Integer

OriginTxtPath = "C:\Program
Files\Fineware\hound4\Searches\LastSearch.txt"
DestinationTxtPath = "C:\Dups V.1.0\LastSearchMacias.txt"
PatternsLine = "Included Patterns:"
SearchFolderLine = "Search Folder"
MyCounter = 0

'Obtain legal handles
'inFile = FreeFile
'Open OriginTxtPath For Input As #inFile
inFile = FreeFile()
'Open the files: this needs to be error-trapped properly
Open OriginTxtPath For Input As #inFile
outFile = FreeFile()
Open DestinationTxtPath For Output As #outFile

Do Until EOF(inFile) 'Loop until end of file.
Line Input #inFile, ReadText 'Read line into variable.

'Then you can read it line by line:

'Do something with the line.
If Left(ReadText, Len(SearchFolderLine)) = SearchFolderLine Then
MsgBox "Found Search Folder"
MyCounter = MyCounter + 1
End If

For FolderLineFound = 1 To MyCounter

SearchFolderLineReplace = Replace(SearchFolderLine & _
" " & FolderLineFound & ":", SearchFolderLine & " " & FolderLineFound &
_
":", "DO SOMETHING PLEASE!!!", , , vbTextCompare)
Debug.Print SearchFolderLineReplace
Next
'Write it out to the DestinationTxtPath
Print #outFile, ReadText
Loop 'Now go round again for the next line

'tidy up
Close #inFile
Close #outFile

End Sub
 
R

rebelscum0000

Dear Gerwin,
Noticed something else. This code is in loop that is not needed

How then my code will read until the end of the file, find the words
"Search Folder" and incremental MyCounter if this code is not in
loop?

This is my code and works fine for me, but if you notice something that
I should change please let me know, I want to learn

Finally, I would like to exclude from "Result:"
The following words
C:\Documents and Settings
C:\Program Files
C:\WINDOWS
Before Print #outFile, SearchFolderLineReplaceTrim

Any idea, clue or suggestion?

Thank you very much; finally, I think I understood the replace function

Regards
Antonio Macias
Mexico City

PS: If you training users like me, please send me an email

Sub Reading_TXTFile2()


Dim OriginTxtPath As String
Dim DestinationTxtPath As String
Dim inFile As Integer
Dim outFile As Integer
Dim ReadText As String
Dim PatternsLine As String
Dim SearchFolderLine As String
Dim MyCounter As Integer


OriginTxtPath = "C:\Program
Files\Fineware\hound4\Searches\LastSearch.txt"
DestinationTxtPath = "C:\Dups V.1.0\LastSearchMacias.txt"
SearchFolderLine = "Search Folder"
MyCounter = 0


'Obtain legal handles
inFile = FreeFile()
Open OriginTxtPath For Input As #inFile
outFile = FreeFile()
Open DestinationTxtPath For Output As #outFile

'Loop until end of file.
Do Until EOF(inFile)
'Read line into variable.
Line Input #inFile, ReadText


'Then you can read it line by line:
If Left(ReadText, Len(SearchFolderLine)) = SearchFolderLine Then
'Counter
MyCounter = MyCounter + 1
'Replace
SearchFolderLineReplace = Replace(ReadText, SearchFolderLine & _
" " & MyCounter & ":", "", , , vbTextCompare)
SearchFolderLineReplaceTrim = Trim(SearchFolderLineReplace)
'Write it out
Print #outFile, SearchFolderLineReplaceTrim
End If
'Now go round again for the next line
Loop

'tidy up
Close #inFile
Close #outFile

End Sub


This is "Result:"

C:\Documents and Settings\Antonio Macias\Desktop
A:\
C:\
C:\Documents and Settings
C:\Downloads
C:\Dups V.1.0
C:\DVD APPZ X
C:\DVD Basic_8116
C:\HP PMD P
C:\HP PMD P II
C:\HP PMD S
C:\MSOCache
C:\NO OS 80 G
C:\Program Files
C:\WINDOWS
D:\
E:\
C:\Documents and Settings\All Users\Documents
C:\Documents and Settings\Antonio Macias\My Documents
C:\Documents and Settings\Antonio Macias\Desktop\Appz Moved
C:\Documents and Settings\Antonio Macias\Desktop\FIX DVD Basic_8116
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S
C:\Documents and Settings\Antonio Macias\Desktop\Fix HP PMD S G
C:\Documents and Settings\Antonio Macias\Desktop\HP PMD S
 
R

rebelscum0000

Dear Gerwin,
Executing it one time replaces all instances, no loops needed.

I still do not understand this part; if I do not have loops, the code
does not work,
I deleted the Do Until EOF(inFile) and the Loop from my code and I got
a blank txt file
as #outFile.

In other words: At this moment I have 24 folders (without the exclusion
code) in my entire hard disk, I am not looking for subfolders, lets
suppose I create a new folder and I name it as Mr. Access at C: \ or
in my Desktop, Now I have 25 folders, the code will not replace it or
exclude it, Right? Can you send me a simple air-code of how to read a
txt file until EOF, reading line by line without loops?

About my Exclusion code
I do not if I am lucky or I am coding wrong, I must ask you this:
In your experience no matter how the code is design meanwhile it works
or I have to be more methodic in order to avoid not desire results?

I ask you this because you suggested
You could exclude lines from your resulting file by testing string.
If SearchFolderLineReplaceTrim <> "C:\WINDOWS" and

I know how to use the Mid, Left, Right, Trim Functions

However, I used this code to exclude lines

If Left(SearchFolderLineReplaceTrim, Len(DocuSet)) = DocuSet Then
ElseIf Left(SearchFolderLineReplaceTrim, Len(MsCa)) = MsCa Then
ElseIf Left(SearchFolderLineReplaceTrim, Len(ProFi)) = ProFi Then
ElseIf Left(SearchFolderLineReplaceTrim, Len(WiWs)) = WiWs Then
Else
'Write it out
Print #outFile, SearchFolderLineReplaceTrim
End If
End If

The results are the way I want:

A:\
C:\
C:\Downloads
C:\Dups V.1.0
C:\DVD APPZ X
C:\DVD Basic_8116
C:\HP PMD P
C:\HP PMD P II
C:\HP PMD S
C:\NO OS 80 G
D:\
E:\

This is my entire code, once again if you notice something that
I should change please let me know, I want to learn

Thanks in Advance
Regards,
Antonio Macias
Mexico City

Dim OriginTxtPath As String 'OriginTxtPath = OTP
Dim DestinationTxtPath As String 'DestinationTxtPath = DTP
Dim RenameTxtPath As String 'RenameTxtPath = RTP
Dim TxtName As String 'TxtName = TN
Dim inFile As Integer 'inFile = iF
Dim outFile As Integer 'outFile = oF
Dim ReadText As String 'ReadText = RT
Dim SearchFolderLine As String 'SearchFolderLine = SFL
Dim DocuSet As String 'DocuSet = Documents and Settings = DaS
Dim MsCa As String 'MsCa =MSOCache = M
Dim ProFi As String 'ProFi = Program Files = PF
Dim WiWs As String 'WiWs = WINDOWS = W
Dim MyCounter As Integer 'MyCounter = MC
Dim SearchFolderLineReplace As String 'SearchFolderLineReplace = SFLR
Dim SearchFolderLineReplaceTrim As String 'SearchFolderLineReplaceTrim
= SFLRT


TxtName = "LastSearch"
SearchFolderLine = "Search Folder"
DocuSet = "C:\Documents and Settings"
MsCa = "C:\MSOCache"
ProFi = "C:\Program Files"
WiWs = "C:\WINDOWS"
MyCounter = 0

OriginTxtPath = "C:\Program Files\Fineware\hound4\Searches\" & TxtName
& ".txt"
DestinationTxtPath = CurrentDb().Name
DestinationTxtPath = Left$(DestinationTxtPath, _
Len(DestinationTxtPath) - Len(Dir$(DestinationTxtPath))) & _
(TxtName & ".txt")

'Obtain legal handles
inFile = FreeFile()
Open OriginTxtPath For Input As #inFile
outFile = FreeFile()
Open DestinationTxtPath For Output As #outFile

Do Until EOF(inFile) 'Loop until end of file.
Line Input #inFile, ReadText 'Read line into variable.

'Then you can read it line by line:
'Do something with the line.
If Left(ReadText, Len(SearchFolderLine)) = SearchFolderLine Then
'Counter
MyCounter = MyCounter + 1
'Replace
SearchFolderLineReplace = Replace(ReadText, SearchFolderLine & _
" " & MyCounter & ":", "", , , vbTextCompare)
SearchFolderLineReplaceTrim = Trim(SearchFolderLineReplace)
'Exclusion code
If Left(SearchFolderLineReplaceTrim, Len(DocuSet)) = DocuSet Then
ElseIf Left(SearchFolderLineReplaceTrim, Len(MsCa)) = MsCa Then
ElseIf Left(SearchFolderLineReplaceTrim, Len(ProFi)) = ProFi Then
ElseIf Left(SearchFolderLineReplaceTrim, Len(WiWs)) = WiWs Then
Else
'Write it out
Print #outFile, SearchFolderLineReplaceTrim
End If
End If
Loop

'Tidy up
Close #inFile
Close #outFile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top