Displaying a file and writing in vbscript

Discussion in 'Web Design, HTML And CSS' started by shadowcat2021, Apr 19, 2010.

  1. shadowcat2021

    shadowcat2021 New Member

    Joined:
    Apr 19, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I want to access a file that already has data in it and I want to be able to edit or write information underneath the data? here is what I have so far
    Code:
    Option Explicit
    
    Const conForReading = 1
    
    'Declare variables
    Dim objFSO, objReadFile, contents
    
    'Set Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objReadFile = objFSO.OpenTextFile("C:\windows\system32\drivers\etc\hosts.txt", 1, False)
    
    'Read file contents
    contents = objReadFile.ReadAll
    
    'Close file
    objReadFile.close
    
    'Display results
    wscript.echo contents
    
    'Cleanup objects
    Set objFSO = Nothing
    Set objReadFile = Nothing
    
    'Quit script
    
    
    
    I can't get this to work correctly
     
  2. shadowcat2021

    shadowcat2021 New Member

    Joined:
    Apr 19, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Okay I figured how to get it to read but its not displaying the message
    Code:
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objDictionary = CreateObject("Scripting.Dictionary")
    
    Const ForReading = 1
    
    Set objFile = objFSO.OpenTextFile ("c:\hosts.txt", ForReading)
    i = 0
    Do Until objFile.AtEndOfStream
    strNextLine = objFile.Readline
    If strNextLine <> "" Then
    objDictionary.Add i, strNextLine
    End If
    i = i + 1
    Loop
    objFile.Close
    
    
    
    Could anyone help with this?
     
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Hi,

    The approach you used first is simple and correct !
    The code works perfectly fine for me, I just had to make a small change.

    The only thing is changed is the path : "C:\windows\system32\drivers\etc\hosts.txt", 'cuz there is no such file !
    The correct path should be : "C:\windows\system32\drivers\etc\hosts"

    Try it ;)
     
  4. shadowcat2021

    shadowcat2021 New Member

    Joined:
    Apr 19, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
  5. shadowcat2021

    shadowcat2021 New Member

    Joined:
    Apr 19, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hey there I was wondering how I can append, basically add text to an already written document for example I found this code but I am not understanding how it works:

    Code:
    
    Option Explicit
    Dim objFSO, objFolder, objShell,  objTextFile, objFile
    Dim strDirectory, strFile, strText
    strDirectory  =  "e:\logs3"
    strFile = "\Summer.txt"
    strText = "Book Another  Holiday"
    
    ' Create the File System Object
    Set objFSO =  CreateObject("Scripting.FileSystemObject")
    
    ' Check that the  strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
        Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set  objFolder = objFSO.CreateFolder(strDirectory)
        WScript.Echo "Just created " & strDirectory
    End If
    
    If  objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder =  objFSO.GetFolder(strDirectory)
    Else
       Set objFile  = objFSO.CreateTextFile(strDirectory & strFile)
       Wscript.Echo  "Just created " & strDirectory & strFile
    End If 
    
    set  objFile = nothing
    set objFolder = nothing
    ' OpenTextFile Method  needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const  ForAppending = 8
    
    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory  & strFile, ForAppending, True)
    
    ' Writes  strText every time you run this VBScript
    objTextFile.WriteLine(strText)
    objTextFile.Close
    
    '  Bonus or cosmetic section to launch explorer to check file
    If  err.number = vbEmpty then
        Set objShell = CreateObject("WScript.Shell")
       objShell.run  ("Explorer" &" " & strDirectory & "\" )
    Else WScript.echo  "VBScript Error: " & err.number
    End If
    
    WScript.Quit
    
    '  End  of VBScript to write to a file with error-correcting Code
    
    
    I don't understand the results of this and what is the need for strDirectory when you can go immediately to the source. Can anyone explain that to me

    Thanks for your help
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Ok .. lemme explain the whole code.

    Code:
    Option Explicit
    Dim objFSO, objFolder, objShell,  objTextFile, objFile
    Dim strDirectory, strFile, strText
    strDirectory  =  "e:\logs3"
    strFile = "\Summer.txt"
    strText = "Book Another  Holiday"
    
    ' Create the File System Object
    Set objFSO =  CreateObject("Scripting.FileSystemObject")
    
    The above code-segment does mostly initialization stuff and is pretty obvious.

    Code:
    ' Check that the  strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
        Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set  objFolder = objFSO.CreateFolder(strDirectory)
        WScript.Echo "Just created " & strDirectory
    End If
    
    The above code checks for existence of strDirectory. If it exists, that's good .. the script creates a folder object that represents strDirectory, else .. it creates the directory.
    Now, about your question .. "I don't understand the results of this and what is the need for strDirectory when you can go immediately to the source."
    Well, yeah you can go immediately to the source if it exists, else you would end up getting a run-time error that the specified path was not found !

    Code:
    If  objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder =  objFSO.GetFolder(strDirectory)
    Else
       Set objFile  = objFSO.CreateTextFile(strDirectory & strFile)
       Wscript.Echo  "Just created " & strDirectory & strFile
    End If 
    
    The above segment similarly checks for the existence of strFile and creates it if it's not found.
    Always remember to check for paths before trying to access them, that would ensure smooth and error-free running of your program.

    Code:
    set  objFile = nothing
    set objFolder = nothing
    
    As objFile and objFolder are no longer required, unload them from memory.

    Code:
    ' OpenTextFile Method  needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const  ForAppending = 8
    
    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory  & strFile, ForAppending, True)
    
    ' Writes  strText every time you run this VBScript
    objTextFile.WriteLine(strText)
    objTextFile.Close
    
    Pretty obvious .. gets a text-stream from strFile and then writes strText to it.

    Code:
    '  Bonus or cosmetic section to launch explorer to check file
    If  err.number = vbEmpty then
        Set objShell = CreateObject("WScript.Shell")
       objShell.run  ("Explorer" &" " & strDirectory & "\" )
    Else WScript.echo  "VBScript Error: " & err.number
    End If
    
    WScript.Quit
    
    Shells a Windows Explorer to let the user verify that the file exists.


    I hope things are clear to you now.
    No probs if not, keep posting :)
     
  7. shadowcat2021

    shadowcat2021 New Member

    Joined:
    Apr 19, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Gosh your a genius I was looking deeply further and I found something simpler to write the text i need:

    Code:
    
    Dim fiParagraph
    Dim fsoParagraph
    set fsoParagraph = CreateObject("Scripting.FileSystemObject")
    set fiParagraph = fsoParagraph.OpenTextFile(filelocation, 2, true)
    
    if err.number = 0 then
        fiMessage.write vendorparagraph
        fiMessage.close
    end if
    
    set fiMessage = nothing
    set fsoMessage = nothing
    
    
    Now I placed my own file where the filelocation is but an error comes up with Object Required: fiMessage

    I am not quite sure how to fix can you help me. Thank you so much I really do appreciate the help.
     
  8. shadowcat2021

    shadowcat2021 New Member

    Joined:
    Apr 19, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Nevermind but I was wondering what is the code for a newline? Not for html but for regular
     
  9. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Check your code :
    (1) fiMessage is undeclared and un-initialized.
    (2) vendorparagraph is undeclared an un-initialized.
    (3) filelocation is undeclared an un-initialized.
    (4) fsoMessage is undeclared and un-initialized.

    Even if you change filelocation to some "real" path, what about the rest 2 problems ?
    Get rid of fiMessage at all. You're already using a text-stream as fiParagraph. So, write your text through it.
    And. you are opening the stream in mode 2 which means for writing it fresh (not appending).
    And do remember to change vendorparagraph to some real text.

    A working copy of your code might look like :

    Code:
    Dim fiParagraph
    Dim fsoParagraph
    set fsoParagraph = CreateObject("Scripting.FileSystemObject")
    set fiParagraph = fsoParagraph.OpenTextFile("C:\test.txt", 2, true)
    
    if err.number = 0 then
        fiParagraph.write "Saswat Padhi"
        fiParagraph.close
    else
        msgbox "Error opening stream !"
    end if
    
    set fiParagraph = nothing
    set fsoParagraph = nothing
    

    To put a new line in a file, through VB, you can use vbCrlLf.
    For example :
    (1) The output of :
    Code:
        fiParagraph.write "I am "+"Saswat Padhi"
    is :
    Code:
    I am Saswat Padhi
    (2) The output of :
    Code:
        fiParagraph.write "I am "+vbCrLf+"Saswat Padhi"
    is :
    Code:
    I am 
    Saswat Padhi
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice