我使用以下VBscript在commonapplicationdatafolder中创建了一个文本文件“ list.txt”。我通过写入文本文件来显示变量(strlist)中的某些值。
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
Run Code Online (Sandbox Code Playgroud)
这是我的疑问
1>在这里,在创建该文件之前,我需要删除旧的“ list.txt”,因为在安装过程中,我一直想创建列表文件。所以我想在创建最新文件之前包含删除所有现有文件(任何旧的list.txt)的代码。在这里,我做了以下代码
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
Run Code Online (Sandbox Code Playgroud)
这是怎么回事,它将第一次创建文件夹,下一次它将删除它,但我一直希望在那里存在该文件(新的新鲜文件,带有'strlist'的值)谁能告诉我vbscript代码来做到这一点。删除了其他部分,但只删除了,下面的东西不起作用意味着创建。
2>这里我只是使用“ WriteLine”方法(spoFile.WriteLine(strlist))写入“ list.txt”,但是我读到某个地方需要使用“ OpenTextFile”(Const ForWriting = 2)进行写入,如果是这种情况,我需要在此处进行哪些更改,这是强制性的吗?
您需要先执行删除或不删除决定,然后再执行写决定。
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
Run Code Online (Sandbox Code Playgroud)
使用常量写入值替代您的问题,并使其速度更快(一点点),您可以尝试以下操作:
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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16146 次 |
| 最近记录: |