Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   Displaying a file and writing in vbscript (http://www.go4expert.com/forums/displaying-file-writing-vbscript-t21843/)

shadowcat2021 19Apr2010 23:00

Displaying a file and writing in vbscript
 
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

shadowcat2021 21Apr2010 00:29

Re: Displaying a file and writing in vbscript
 
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?

SaswatPadhi 22Apr2010 21:13

Re: Displaying a file and writing in vbscript
 
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 ;)

shadowcat2021 23Apr2010 00:13

Re: Displaying a file and writing in vbscript
 
Got it thx ;)

shadowcat2021 26Apr2010 21:27

Re: Displaying a file and writing in vbscript
 
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

SaswatPadhi 27Apr2010 11:42

Re: Displaying a file and writing in vbscript
 
Ok .. lemme explain the whole code.

Code: VB

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: VB

' 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: VB

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: VB

set  objFile = nothing
set objFolder = nothing

As objFile and objFolder are no longer required, unload them from memory.

Code: VB

' 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: VB

'  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 :)

shadowcat2021 27Apr2010 20:46

Re: Displaying a file and writing in vbscript
 
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.

shadowcat2021 27Apr2010 23:38

Re: Displaying a file and writing in vbscript
 
Nevermind but I was wondering what is the code for a newline? Not for html but for regular

SaswatPadhi 28Apr2010 08:58

Re: Displaying a file and writing in vbscript
 
Quote:

Originally Posted by shadowcat2021 (Post 67503)
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.

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: VB

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



Quote:

Originally Posted by shadowcat2021 (Post 67515)
Nevermind but I was wondering what is the code for a newline? Not for html but for regular

To put a new line in a file, through VB, you can use vbCrlLf.
For example :
(1) The output of :
Code: VB

fiParagraph.write "I am "+"Saswat Padhi"

is :
Code:

I am Saswat Padhi
(2) The output of :
Code: VB

fiParagraph.write "I am "+vbCrLf+"Saswat Padhi"

is :
Code:

I am
Saswat Padhi



All times are GMT +5.5. The time now is 13:16.