Loop txt files and make change and save

E

Excel 009

Dear all,

I am trying to modify the same specific content in 100+ txt files (for
example, change xxx to yyy). I need help to write a Word VBA code to
do that. Here is the pesudo code:

loop all files in a folder

open Nth txt file
find xxx
replace with yyy
save the txt file with a new name (add suffix "-mod")
close the file
end loop

Can anyone help? Thank you in advance.

- 009
http://en.wikipedia.org/wiki/Cyborg_009
 
G

Graham Mayor

See the code at http://www.gmayor.com/batch_replace.htm

Add the line
Dim intPos As Integer
at the start of
Public Sub BatchReplaceAnywhere()

Replace in that sub

'Close the file, saving the changes.
myDoc.Close Savechanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub

with

intPos = InStrRev(myFile, ".")
myFile = Left(myFile, intPos - 1)
myFile = myFile & "_MOD.txt"
MyDoc.SaveAs FileName:=myFile, _
FileFormat:=wdFormatDOSText
MyDoc.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$()
Wend
End Sub

This should leave your original files unchanged but save new text versions
with the filename
filename_MOD.txt in the same folder. Test on sample data.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Excel 009

Thank you Graham.

You code works after I did the following changes:

Comment out:

'Close any documents that may be open
' If Documents.Count > 0 Then
' Documents.Close SaveChanges:=wdPromptToSaveChanges
' End If

Because it closes my Word document that contains the macro.

Change
myFile = Dir$(PathToUse & "*.doc")
to
myFile = Dir$(PathToUse & "*.txt")

because my files are txt files.

I code ran, but where are the output files? Where are they located?

- 009
 
R

Russ

Excel 009,
Thank you Graham.

You code works after I did the following changes:

Comment out:

'Close any documents that may be open
' If Documents.Count > 0 Then
' Documents.Close SaveChanges:=wdPromptToSaveChanges
' End If

Because it closes my Word document that contains the macro.

Change
myFile = Dir$(PathToUse & "*.doc")
to
myFile = Dir$(PathToUse & "*.txt")

because my files are txt files.

I code ran, but where are the output files? Where are they located?
Did you check your original files to see if they where changed after
'running code'?
 
R

Russ

Excel 009,
Excel 009,

Did you check your original files to see if they *where typo* changed after
'running code'?
I meant, did original files change, like you wanted them to change?
 
G

Graham Mayor

Put the macro in normal.dot - not the document you are working with and
restore the commented lines.

Your other changes seem OK - but for some reason I am not yet clear about,
the modified documents are saved in the default document folder (tools >
options > file locations > documents).

You'll also need to ensure that tools > options > general > confirm
conversions at open is unchecked or you will be prompted for a text filter
for each document in the folder.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Excel 009

Hi Graham and Russ,

Yes, the output files got populated beautifully. Thanks to Graham's
code.
It was my mistake that despite I listed the changes that I did, but the
changes were not saved and the code was ran unchanged.

I have another question. How can I have the have the whole word
changed and not on partial work. Ex: in

x = 10
text = "ABC"
z = x + 30,

if I want to just change the "x" variable to "y" without making change
on "text", how can I do so?

- 009
 
G

Graham Mayor

You will have to include sufficient of the string to uniquely identify it,
eg search for
z = x + 30
replace with
z = y + 30

If necessary change the line
..MatchWildcards = False
to
..MatchWildcards = True
and use a suitable wildcard pattern
http://www.gmayor.com/replace_using_wildcards.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tony Jollans

The easiest wildcard pattern for your example would be <x> which means
start-of-word lower-case-letter-x end-of-word
 

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