Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have found answers to so many questions by random google searches on this page, so I'm hoping my own question can be answered as well.
I have a directory full of a hundreds of identical text files (copied from a template), each one named for a different process.
Every file has a string called 'replace1' which I would like to replace with the filename.
ex:
filename = dostext.txt
replace1 becomes dostext.txtIf thats solvable, then the next step is to replace a second variable, replace2 with a truncated version of the filename
ex:
filename=dostext.txt
replace2 becomes dostextIs this possible to do through a batch file? If not, are there any other recommended methods?

There's probably a better way to do this (I.E. PERL), but I'm not about to care. Largely untested VBS:
With CreateObject("Scripting.FileSystemObject")
sTmp = .GetTempName
For Each i In .GetFolder(".").Files
Set oTmp = .CreateTextFile(sTmp)
sFName = i.Name
With i.OpenAsTextStream(1)
Do Until .AtEndOfStream
sLine = .ReadLine
sLine = Replace(sLine, "replace1", i.Name)
sLine = Replace(sLine, "replace2", SGetSName(i.Name))
oTmp.WriteLine sLine
Loop
End With
oTmp.Close
i.Delete
.GetFile(sTmp).Name = sFName
Next
End WithFunction SGetSName(sIn)
If InStr(sIn, ".") Then
SGetSName = Left(sIn, InStrRev(sIn, ".") - 1)
Else
SGetSName = sIn
End If
End Function

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |