


What code should I use to write a VB code to delete a file on my computer?
You can take a look at the kill function:
kill deletes files from disk.
Kill pathname
The pathname parameter is a string expression used to specify a file name.
pathname can contain directories or folders, as well as drives.
illustrate
In Microsoft Windows, Kill supports multi-character (*) and single-character (?) wildcards to specify multiple files.
example:
Private Sub Command1_Click()
Kill "C:\aa.txt" 'The absolute path of the file'
End Sub
If the file is completely deleted, everyone above is right!
But if you put the file in the "Recycle Bin" instead of completely deleting it, you need to use vb
api function!
example:
In public modules:
Option Explicit
Public Const FO_MOVE = &H1
Public Const FO_COPY = &H2
Public Const FO_DELETE = &H3
Public Const FO_RENAME = &H4
Public Const FOF_NOCONFIRMATION = &H10
Public Const FOF_NOCONFIRMMKDIR = &H200
Public Const FOF_ALLOWUNDO = &H40
Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
In the frm module:
Private Sub Command1_Click()
Dim SHFileOp As SHFILEOPSTRUCT
Dim Path As String, pFrom As String, i As Integer
pFrom ="c:\aa.txt"
SHFileOp.wFunc = FO_DELETE
SHFileOp.pFrom = pFrom
SHFileOp.fFlags = FOF_ALLOWUNDO FOF_NOCONFIRMATION
SHFileOperation SHFileOp
End Sub
I think the poster meant to put the file into the "recycle bin", so. . .
How to delete the contents of a specified line in a specified text document in vb
Dim newfile As New List(Of String)
For Each line As String In System.IO.File.ReadAllLines("TextFile1.txt")
If Not line.StartsWith("3") Then newfile.Add(line)
Next
System.IO.File.WriteAllLines("TextFile1.txt", newfile) Create a collection, use System.IO.File's ReadAllLines to read out all the contents, and judge one by one. If it is necessary to add it to the collection, if it is to be deleted Do nothing, and finally write with WriteAllLines.
Let me explain here, the above code is used to delete all text lines starting with 3.
Delete the specified file through the file name in VB
Add a FILELISTBOX control on the form and set it to invisible
Private Sub Form_Load()
On Error GoTo MyError 'Set error trap
Dim MyPath As String 'Define the directory path of the file to be deleted
Dim MyPattern As String 'Define the file type to be deleted
Dim YesterdayName, TodayName As String 'Define the file names of yesterday and today
MyPath = '"D:\TempPath"
MyPattern = "*.TXT" 'File name containing date, for example YYYY-MM-DD.TXT
File1.Path = MyPath
File1.Pattern = MyPattern
YesterdayName = Format(DateAdd("d", -1, Date), "YYYY-MM-DD") ".TXT" 'File name containing yesterday's date
TodayName = Format(Date$, "YYYY-MM-DD") ".TXT" 'File name containing today's date
If File1.ListCount > 0 Then
For i% = 0 To File1.ListCount - 1
File1.ListIndex = i%
If File1.FileName CurrentName And File1.FileName CurrentName Then 'If the file name is not equal to today's or yesterday's file name, delete it
Kill File1.Path "\" File1.FileName
End If
Next i%
End If
MyError:
File1.Refresh 'After all deletions are completed, refresh the list
End Sub
The above is the detailed content of VB code: How to write code to delete specified files on the computer?. For more information, please follow other related articles on the PHP Chinese website!

This article addresses the Windows "INVALID_DATA_ACCESS_TRAP" (0x00000004) error, a critical BSOD. It explores common causes like faulty drivers, hardware malfunctions (RAM, hard drive), software conflicts, overclocking, and malware. Trou

This article provides practical tips for maintaining ENE SYS systems. It addresses common issues like overheating and data corruption, offering preventative measures such as regular cleaning, backups, and software updates. A tailored maintenance s

This article identifies five common pitfalls in ENE SYS implementation: insufficient planning, inadequate user training, improper data migration, neglecting security, and insufficient testing. These errors can lead to project delays, system failures

Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

What does the drive health warning in Windows Settings mean and what should you do when you receive the disk warning? Read this php.cn tutorial to get step-by-step instructions to cope with this situation.

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

This article identifies ene.sys as a Realtek High Definition Audio driver component. It details its function in managing audio hardware, emphasizing its crucial role in audio functionality. The article also guides users on verifying its legitimacy

This article addresses the failure of the Windows asio.sys audio driver. Common causes include corrupted system files, hardware/driver incompatibility, software conflicts, registry issues, and malware. Troubleshooting involves SFC scans, driver upda


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
