#1
  1. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Apr 2005
    Posts
    191
    Rep Power
    14

    Using FSO to replace a line in a text file


    I'm still fairly new to FSO, so please excuse my ignorance. I have a text file named "enterintro.bat". The contents of the file are:
    Code:
    @ECHO OFF
    start /w C:\sk\AppActivate.vbs "F4 Event and Entity" 
    start /w Sleep.vbs 2
    start /w C:\sk\SendKeys.vbs "BEC201_IPA{TAB}IPA_SS{ENTER}^S"
    What I'm trying to do is to be able to swap out the "BEC201_IPA" and "IPA SS" with different variables. The format of the text file should never change except for swapping out these values. I've used the OpenTextFile method before, but only to append lines to a file, and never to replace an existing line, and I'm not sure how to do it. The best I've come up with so far is this:
    Code:
    <%
    dim fs,f
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set f=fs.OpenTextFile(Server.MapPath("enterintro.bat"),2,false)
    f.SkipLine
    f.SkipLine
    f.SkipLine
    f.WriteLine("start /w C:\sk\SendKeys.vbs 'BEC201_MOD2{TAB}BLU_SS{ENTER}^S'")
    f.Close
    set f=Nothing
    set fs=Nothing
    %>
    But that generates a "Bad File Mode" error. Any help would be greatly appreciated. Thanks again to you all! -ST
    Last edited by steeltrap; August 22nd, 2006 at 01:51 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    ASP Mastermind (5000+ posts)

    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    140
    You could read the contents of the file, close it, then open it for writing.

    asp Code:
     
    <%
    fileName = Server.MapPath("text.bat")
    var1 = "BEC201_IPA"
    var1_r = "Test1"
     
    var2 = "IPA_SS"
    var2_r = "Test2"
     
    Set fs=Server.CreateObject("Scripting.FileSystemObject  ")
     
    Set f=fs.OpenTextFile(fileName, 1)
    fileContents = (f.ReadAll)
    f.Close
    Set f=Nothing
     
     
    Response.Write "Orig:<br>" & Replace(fileContents,vbCRLF,"<br>")
     
    fileContents = Replace(fileContents,var1,var1_r)
    fileContents = Replace(fileContents,var2,var2_r)
     
    set f=fs.CreateTextFile(fileName,true)
    f.write(fileContents)
    f.close
    set f=nothing
     
     
    Set f=fs.OpenTextFile(fileName, 1)
    Response.Write("<br><br>New:<br>" & Replace(f.ReadAll,vbCRLF,"<br>"))
    f.Close
    Set f=Nothing
    set fs=nothing
    %>

    Comments on this post

    • steeltrap agrees : I always trust answers from degsy. As usual, this works like a champ without issue.
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Apr 2005
    Posts
    191
    Rep Power
    14

    Thanks again!


    Thanks again, degsy! I always benefit immensely from your expertise. That was exactly what I was looking for! -ST
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Sep 2006
    Posts
    1
    Rep Power
    0

    Thumbs up Oh Thank You!!!!!!!!!!!!! I Have Been Looking For This!!!!


    Thank you! I have been searching for this for HOURS and have tried so many things without any luck! You have made my life so much easier! Thanks so much!



    Originally Posted by degsy
    You could read the contents of the file, close it, then open it for writing.

    asp Code:
     
    <%
    fileName = Server.MapPath("text.bat")
    var1 = "BEC201_IPA"
    var1_r = "Test1"
     
    var2 = "IPA_SS"
    var2_r = "Test2"
     
    Set fs=Server.CreateObject("Scripting.FileSystemObject  ")
     
    Set f=fs.OpenTextFile(fileName, 1)
    fileContents = (f.ReadAll)
    f.Close
    Set f=Nothing
     
     
    Response.Write "Orig:<br>" & Replace(fileContents,vbCRLF,"<br>")
     
    fileContents = Replace(fileContents,var1,var1_r)
    fileContents = Replace(fileContents,var2,var2_r)
     
    set f=fs.CreateTextFile(fileName,true)
    f.write(fileContents)
    f.close
    set f=nothing
     
     
    Set f=fs.OpenTextFile(fileName, 1)
    Response.Write("<br><br>New:<br>" & Replace(f.ReadAll,vbCRLF,"<br>"))
    f.Close
    Set f=Nothing
    set fs=nothing
    %>

Similar Threads

  1. IIS 6.0 Crashing Daily on Production Server
    By JMelsoner in forum Microsoft IIS
    Replies: 5
    Last Post: April 12th, 2006, 05:43 PM
  2. How do I design a browser to detect text file?
    By lionel84 in forum Programmers for Hire
    Replies: 1
    Last Post: July 10th, 2005, 02:49 AM
  3. Loop through Text File -Find String - Delete Line
    By werD in forum Visual Basic Programming
    Replies: 1
    Last Post: March 22nd, 2005, 06:39 PM
  4. Suppress warnings when saving text file?
    By Snowsride in forum Microsoft Access Help
    Replies: 1
    Last Post: October 12th, 2004, 08:23 AM
  5. Web serverístatistical analysis
    By cso in forum ASP Development
    Replies: 0
    Last Post: April 19th, 2003, 11:35 AM

IMN logo majestic logo threadwatch logo seochat tools logo