How to over write and write in to a textfile by using Vbscript

后端 未结 2 1614
悲哀的现实
悲哀的现实 2021-01-19 06:10

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 textf

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 06:48

    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'
    

提交回复
热议问题