Computing.Net > Forums > Programming > Batch: replace variable w/ filename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch: replace variable w/ filename

Reply to Message Icon

Name: exo
Date: June 28, 2007 at 15:30:11 Pacific
OS: XP
CPU/Ram: AMD Athlon 3400
Comment:

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.txt

If 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 dostext

Is this possible to do through a batch file? If not, are there any other recommended methods?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: June 28, 2007 at 20:24:25 Pacific
Reply:

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 With

Function SGetSName(sIn)
If InStr(sIn, ".") Then
SGetSName = Left(sIn, InStrRev(sIn, ".") - 1)
Else
SGetSName = sIn
End If
End Function


0

Response Number 2
Name: exo
Date: June 28, 2007 at 23:11:32 Pacific
Reply:

Thank you very much, I will try this.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch: replace variable w/ filename

Batch file variable creation www.computing.net/answers/programming/batch-file-variable-creation/14564.html

Batch script variables www.computing.net/answers/programming/batch-script-variables/17422.html

batch script to parse filenames www.computing.net/answers/programming/batch-script-to-parse-filenames/15286.html