Home >Computer Tutorials >Computer Knowledge >VB code: How to write code to delete specified files on the 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. . .
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.
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!