search
HomeComputer TutorialsComputer KnowledgeVB code: How to write code to delete specified files on the computer?

VB code: How to write code to delete specified files on the computer?

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!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
How to Solve Windows Error Code "INVALID_DATA_ACCESS_TRAP" (0x00000004)How to Solve Windows Error Code "INVALID_DATA_ACCESS_TRAP" (0x00000004)Mar 11, 2025 am 11:26 AM

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

ENE SYS Maintenance: Tips and Tricks to Keep Your System Running SmoothlyENE SYS Maintenance: Tips and Tricks to Keep Your System Running SmoothlyMar 07, 2025 pm 03:09 PM

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

5 Common Mistakes to Avoid During ENE SYS Implementation5 Common Mistakes to Avoid During ENE SYS ImplementationMar 07, 2025 pm 03:11 PM

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

How do I edit the Registry? (Warning: Use with caution!)How do I edit the Registry? (Warning: Use with caution!)Mar 21, 2025 pm 07:46 PM

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.

Discover How to Fix Drive Health Warning in Windows SettingsDiscover How to Fix Drive Health Warning in Windows SettingsMar 19, 2025 am 11:10 AM

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.

How do I manage services in Windows?How do I manage services in Windows?Mar 21, 2025 pm 07:52 PM

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

which application uses ene.syswhich application uses ene.sysMar 12, 2025 pm 01:25 PM

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

why won't driver asio.sys loadwhy won't driver asio.sys loadMar 10, 2025 pm 07:58 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft