Re: FilesSystemObject line parsing Question by McKirahan
McKirahan
Tue Dec 23 14:56:45 CST 2003
"ProgGix" <A_Thaper@yahoo.com> wrote in message
news:66893481.0312231110.1a30ff31@posting.google.com...
> Thanks for the follw-up Ray. Greatly appreciated but not exactly what
> I'm looking for. I need to loop through all the lines of my text file
> until I find a line that states "KeyUsername=BobbyB.ID" and replace
> that line with "k:\myserver\myfiles\BobbyB.id". This line will vary
> with every user. So there might be a "KeyUsername=janeS.id" and
> "KeyUsername=Robert.id" and they'll need to be mapped to
> "k:\myserver\myfiles\xxxxx.id". I suspect I need a Mid routine that
> extracts the "x.id" and concactenates it to my server path. The line
> above will always stary routine except for the .id at the end. Hope
> this makes sense. Any help still greatly appreciated
>
> "Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:<unp$ZiWyDHA.3520@tk2msftngp13.phx.gbl>...
> > One way would be to do a replace, like so:
> >
> >
> > Dim oFSO, sContents
> > Set oFSO = CreateObject("Scripting.FileSystemObject")
> > sContents = oFSO.OpenTextFile("C:\yourfile.txt", 1).ReadAll
> > sContents = Replace(sContents, "KeyUsername=", "k:\myserver\myfiles\")
> > oFSO.MoveFile "C:\yourfile.txt", "C:\yourfile.bak"
> > oFSO.CreateTextFile("C:\yourfile.txt", True).Write sContents
> > Set oFSO = Nothing
> >
> > Ray at work
> >
> >
> > "ProgGix" <A_Thaper@yahoo.com> wrote in message
> > news:66893481.0312230714.173a2a7c@posting.google.com...
> > > Hello everyone, hope you're all well!
> > > I've recently started experimenting with the WSH FSO and needed some
> > > help with a scenario.
> > > Let's say I have a line in a text file that looks like the following:
> > > "KeyUsername=username.ID"
> > > I'd like to change this to "k:\myserver\myfiles\username.id". How
> > > would I go about parsing this line and modifying it, taking into
> > > consideration that the username is unique for every user's file.
> > >
> > > Thanks
> > > MT
How about this? Watch for word-wrap.
This creates a new file; however,
you can use "objFSO.MoveFile" to overwrite the old one, though.
Option Explicit
'...........................................................................
'
' "replacer.vbs"
'
' This VBS (Visual Basic Script) program does the following:
' 1) Reads a file, optionally replaces a string, and writes a new file.
'
' "KeyUsername=BobbyB.ID" replaced with "k:\myserver\myfiles\BobbyB.id".
'
' To test, either double-click on the filename in Windows Explorer or
' from the Command prompt, type "cscript replacer.vbs".
'...........................................................................
'
'*
'* Declare Constants
'*
Const cVBS = "replacer.vbs"
Const cOT1 = "replacer.1"
Const cOT2 = "replacer.2"
Comst cRE1 = "KeyUsername="
Const cRE2 = "k:\myserver\myfiles\"
'*
'* Start Message
'*
MsgBox Now & ": '" & cVBS & "' started."
'*
'* Declare Variables
'*
Dim strOT1
Dim strOT2
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOT1
Set objOT1 = objFSO.OpenTextFile(cOT1,1)
Dim objOT2
Set objOT2 = objFSO.OpenTextFile(cOT2,2,True)
'*
'* Read, Replace, Write
'*
Do While Not objOT1.AtEndOfStream
strOT1 = objOT1.ReadLine()
strOT2 = Replace(strOT1,cRE1,cRE2)
objOT2.WriteLine(strOT2)
Loop
'*
'* Destroy Objects
'*
Set objOT1 = Nothing
Set objOT2 = Nothing
Set objFSO = Nothing
'*
'* Finish Message
'*
MsgBox Now & ": '" & cVBS & "' finished."