如何使用 Vbscript 覆盖和写入文本文件 [英] How to over write and write in to a textfile by using Vbscript

查看:37
本文介绍了如何使用 Vbscript 覆盖和写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下 VBscript 在 commonapplicationdatafolder 中创建了一个文本文件list.txt".我正在显示一些通过写入文本文件从变量(strlist)中获取值.

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

这是我的疑惑

1> 在创建该文件之前,我需要删除旧的现有list.txt" 因为在安装过程中我总是想创建列表文件.所以我想包含删除任何现有文件(任何旧的 list.txt)的代码,在创建最新的之前.在这里我做了以下代码

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

这是怎么回事将第一次创建文件夹,下次它将删除它,但我总是希望该文件在那里(新的新文件具有来自strlist"的价值)任何人都可以告诉我 vbscript 代码来做到这一点.他们我删除了其他部分,但只有删除,下面的东西不起作用意味着创建.

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>这里我只是使用'WriteLine'方法(spoFile.WriteLine(strlist))写入list.txt",但我读到了我们需要使用的地方'OpenTextFile'(Const ForWriting = 2) 用于写入,如果是这种情况,我需要在这里做哪些更改,是否是强制性的?

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?

推荐答案

您需要在写决定之前移动您的删除或不删除决定.

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

替代您使用 Constant write values 的问题并使其更快一点(非常少),您可以尝试以下操作:

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'

这篇关于如何使用 Vbscript 覆盖和写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆