VBS删除TXT文件中的某一行
时间: 2023-09-19 16:07:30 浏览: 68
可以使用以下VBScript代码删除TXT文件中的某一行:
```
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\example.txt", ForReading, False, TriStateUseDefault)
strSearchString = "line to delete"
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, strSearchString) = 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\example.txt", ForWriting)
objFile.Write strNewContents
objFile.Close
```
将上面代码中的"C:\example.txt"替换为你想要删除行的TXT文件的路径,将"line to delete"替换为你想要删除的行的内容。执行代码后,TXT文件中的指定行将被删除。
相关问题
VBS 字符串写入文件一行
您可以使用以下VBS脚本将字符串写入文件的一行:
```vbscript
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\path\to\file.txt", 8, True)
strText = "Hello, World!" ' 要写入的字符串
objFile.WriteLine strText
objFile.Close
```
上述代码将字符串 "Hello, World!" 写入到指定路径的文件中。您只需将 `C:\path\to\file.txt` 替换为您想要写入的文件路径,将 `Hello, World!` 替换为您想要写入的字符串内容即可。脚本使用 `OpenTextFile` 方法以追加模式打开文件,并使用 `WriteLine` 方法写入一行字符串。最后,使用 `Close` 方法关闭文件。请确保您有对该文件的写入权限。
vbs 更改某一文件图标
要使用 VBScript (VBS) 更改某一文件的图标,你可以使用 `Shell` 对象和 `Registry` 对象来修改注册表项。下面是一个示例的 VBScript 代码,用于更改文件的图标:
```vbscript
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 设置新的图标文件路径
strIconPath = "C:\path\to\new_icon.ico"
' 设置新的图标索引(如果图标文件中包含多个图标)
intIconIndex = 0
' 设置要更改图标的文件路径
strFilePath = "C:\path\to\file.txt"
' 获取文件扩展名
strFileExt = objFSO.GetExtensionName(strFilePath)
' 创建注册表项
strKeyPath = "HKCU\Software\Classes\" & strFileExt & "\DefaultIcon"
objShell.RegWrite strKeyPath, strIconPath & "," & intIconIndex, "REG_SZ"
' 刷新资源管理器
Set objAppShell = CreateObject("Shell.Application")
objAppShell.Namespace(0).Items.Item(objFSO.GetAbsolutePathName(strFilePath)).InvokeVerb("refresh")
```
在上面的示例中,你需要将 `strIconPath` 设置为你想要使用的新图标文件的路径。如果图标文件中包含多个图标,你可以通过修改 `intIconIndex` 的值来选择特定的图标索引。然后,将 `strFilePath` 设置为你想要更改图标的文件路径。
该 VBScript 代码会创建或修改注册表项来指定新的图标路径和索引。然后,它使用 Shell.Application 对象来刷新资源管理器窗口,以使更改生效。
请注意,在运行 VBScript 代码之前,请确保你具有足够的权限来修改注册表项。此外,更改图标后,可能需要刷新资源管理器窗口才能看到新图标的变化。
希望这可以帮助到你!如有其他问题,请随时提问。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)