How do I delete a file through vb?
Printable View
How do I delete a file through vb?
VB Code:
Kill "c:\myfile.txt"
Just type Kill and the pathname
Hope this helps.
-Jeremy
This maybe extreme but gives you the option of the recycle bin
VB Code:
Option Explicit Private 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 Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Const FOF_NOCONFIRMATION = &H10 ' Don't prompt the user. Const FOF_NOCONFIRMMKDIR = &H200 ' don't confirm making any needed dirs Const FO_COPY = &H2 Const FO_DELETE = &H3 Const FOF_ALLOWUNDO = &H40 Const FOF_FILESONLY = &H80 Public Function NUKILL(PATH As String, Optional RECYCLE As Boolean = True) As Boolean ' NUCLEUS ' DELETE FILE(S) OR FOLDER(S) (INCLUDING SUBDIRECTORIES) AND OPTIONALLY SEND TO RECYCLE BIN ' IN: PATH OF FILE/FOLDER ' OUT: BOOLEAN INDICATING SUCCESS If Right(PATH, 1) = "\" Then PATH = Left(PATH, Len(PATH) - 1) If Len(Dir$(PATH)) <> 0 Or Len(Dir$(PATH, vbDirectory)) <> 0 Then Dim SHFileOp As SHFILEOPSTRUCT With SHFileOp ' DELETE THE FILE .wFunc = FO_DELETE ' SELECT THE FILE .pFrom = PATH .fFlags = FOF_NOCONFIRMATION Or FOF_FILESONLY ' NEXT OPTIONALLY ALLOW MOVE TO RECYCLE BIN ' WHICH ONLY WORKS FOR WHOLE FOLDERS OR SINGLE FILE OPERATIONS ' IT WILL NOT SEND *.* OR *.TXT TO RECYCLE BIN ' DUE TO BUILT-IN LIMITATIONS IN API FUNCTION If RECYCLE Then .fFlags = .fFlags Or FOF_ALLOWUNDO End With ' PERFORM FILE OPERATION NUKILL = (SHFileOperation(SHFileOp) = 0) End If End Function
Add reference to Microsoft Scripting Runtime and use File System Objects. However, after seeing Jim's comment about Recycle Bin, I tested this & the file is NOT put into Recycle Bin.
NateVB Code:
Dim fso As FileSystemObject Dim strFileName As String strFileName = "c:\temp\Junk1.txt" Set fso = New FileSystemObject fso.DeleteFile strFileName
Kill "C:\example.exe"
you can always change the directory. dont use this maliciously thanks;)
Why are you digging up old threads?Quote:
Originally posted by duc
Kill "C:\example.exe"
you can always change the directory. dont use this maliciously thanks;)