tags:

views:

28

answers:

1

i created a text file "list.txt" in commonapplicationdatafolder by using the following VBscript.i am displaying some values from a variable(strlist) by writing in to textfile.

Const Value = &H23&
Const PATH = "\Cape\ibs"

Dim fso                 ' File System Object
Dim spFile             ' Text File object to write
Dim objApplication      ' Application object
Dim objFolder           ' Folder object
Dim objFolderItem       ' FolderItem object

Set objApplication  = CreateObject("Shell.Application")
Set objFolder = objApplication.Namespace(Value)
Set objFolderItem = objFolder.Self
sname = objFolderItem.Path & PATH & "\list.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set spFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
spoFile.Close

Here are my doubts

1> Here before creating that file i need to delete the old existing "list.txt" Because during installation i always want to create the list file. So i want to include code that removes any existing file(any old list.txt), before creating the latest one.Here i did the following code

If fso.FileExists(sname) Then
    fso.DeleteFile sname, True
Else
    Set spFile = fso.CreateTextFile(sname, True)
    spoFile.WriteLine(strlist)
    Set objFolderItem = Nothing
    Set objFolder = Nothing
    Set objApplication = Nothing
    Set fso = Nothing
    spoFile.Close
 End If

Whats going on is it will create folder first time,next time it will delete it ,But i always want that file there(new fresh one with value from 'strlist' ) Can any one tell me the vbscript code to do that.Their i removed Else part also but only deletion going ,below things are not working means creation.

2>Here i was writing in to "list.txt" by using simply 'WriteLine' method(spoFile.WriteLine(strlist)),but i read somewhere that we need to use 'OpenTextFile'(Const ForWriting = 2) for writing,If that is the case what changes i need to do here,Is it mandatory?

+2  A: 

you need to move your delete or not delete decision before your write decision.

If fso.FileExists(sname) Then
    'you delete if you find it'
    fso.DeleteFile sname, True
End If
'you always write it anyway.'
Set spoFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
Set objFolderItem = Nothing
Set objFolder = Nothing
Set objApplication = Nothing
Set fso = Nothing
spoFile.Close

alternately to your question with Constant write values and making this a little (very little) bit faster, you might try the following:

If fso.FileExists(sname) Then
  Set spoFile = fso.OpenTextFile(sname, 2, True)
Else
  Set spoFile = fso.CreateTextFile(sname, True)
End If
' perform your write operations and close normally'
Gabriel
Thats fine,its working,,any bad logic are there in my coding,,i am new to vbscript
peter
'OpenTextFile'(Const ForWriting = 2) for writing,Is it mandatory? or simply spoFile.WriteLine(strlist) is enough
peter
actually peter you only need the write constant for opening an existing text file. In your case you are deleting it and creating a new one. I've modified my answer to add an alternative for using the condition you are talking about.
Gabriel
Thats correct ,Delete operation is not required,simply .WriteLine(strlist) will overwrite previous file.But the requirement is like this
peter