hello everyone,
i want to delete a file which is in a folder. i know the name and path of that file. how can i delete using code of visual basic 6.0
Printable View
hello everyone,
i want to delete a file which is in a folder. i know the name and path of that file. how can i delete using code of visual basic 6.0
VB Code:
Kill "C:\test\text.txt"
This is for deleting a file if it is not read-only. If u want to delete read-only files alo use:
VB Code:
Private Sub filedelete(filename As String) Dim filesystemobject As Object Set filesystemobject = CreateObject("Scripting.filesystemobject") filesystemobject.deletefile filename, True End Sub Private Sub Command1_Click() filedelete (filename) End Sub
thank you bushmobile for your positive response. i got that.
There is an API call for this.
VB Code:
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" _ (ByVal lpFileName As String) _ As Long Private Sub Command1_Click() Dim File As String ' Change Directory where neccesary File = "C:\WINDOWS\Sample.txt" DeleteFile File End Sub
Hope this helps you
Jenova
Yer I agree with the
Kill "c:\log.txt"
But don't forget it doesn't send it to the recycle bin, it just deletes it forever
I m in hurry. anshu if u want then i can send u the code to delete a file and then send it to recycle bin.
Use SHFileOperation to send a file to the recycle bin.Now simply call DeleteFile with the path and name of the file to send to the recycle bin.VB Code:
Private Declare Function SHFileOperation _ Lib "shell32.dll" Alias "SHFileOperationA" ( _ lpFileOp As SHFILEOPSTRUCT _ ) As Long Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40 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 Public Sub DeleteFile(ByVal sFileName As String) Dim fo As SHFILEOPSTRUCT With fo .pFrom = sFileName & vbNullChar .wFunc = FO_DELETE .fFlags = FOF_ALLOWUNDO End With Call SHFileOperation(fo) End Sub
Joacim (or anybody), this is a good method, but how do I know if the user clicked yes or no on the "Are you sure you want to....." dialog box?
I know I could just check to see if the file is still there, but is there a way to get feedback from this process as to which button the user clicked?
For reals? You don't know how a simple MsgBox works? And you dragged up a FIFTEEN year old thread in the process?
-tg
Something like this?
Example:Code:Public Sub Delete_File(ByVal vPathOfFile As String)
Dim vStartTime As Date: vStartTime = Now
If Dir(vPathOfFile, vbArchive Or vbHidden) <> "" Then
If MsgBox("Do you want to delete the file?", vbYesNo, "Warning") = vbYes Then ' < Here is the section you needed to know
If Dir(vPathOfFile, vbArchive Or vbHidden) <> "" Then
SetAttr vPathOfFile, vbArchive
Kill vPathOfFile
End If
Do
If DateDiff("s", vStartTime, Now) >= 5 Then Exit Do
Loop Until Dir(vPathOfFile, vbArchive Or vbHidden) = ""
If Dir(vPathOfFile, vbArchive Or vbHidden) = "" Then MsgBox "File deleted successfully" Else MsgBox "Error deleting file"
End If
End If
End Sub
Delete_File "C:\My folder\My file.txt"
Have a nice day :wave:
I think he's asking how we'd know if the Windows delete confirmation is on (or off). Personally, I don't know how to tell, but I'm certain is some buried registry setting somewhere.
Also, VB6's "Kill" statement ignores that setting, so it's somewhat moot (unless we're using File Explorer to do our deletes for us).
see the returned value in fAborted
https://docs.microsoft.com/en-us/win...Type%3A%20BOOL
Thanks!
So...
VB Code:
Private Declare Function SHFileOperation _ Lib "shell32.dll" Alias "SHFileOperationA" ( _ lpFileOp As SHFILEOPSTRUCT _ ) As Long Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40 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 Public Function DeleteFile(ByVal sFileName As String, Optional vRecycleBin As Boolean) As Boolean Dim fo As SHFILEOPSTRUCT With fo .pFrom = sFileName & vbNullChar .wFunc = FO_DELETE If vRecycleBin Then .fFlags = FOF_ALLOWUNDO ' Send to RecycleBin End With Call SHFileOperation(fo) DeleteFile = Abs(fo.fAborted) - 1 End Function
Examples:
Or...Code:If DeleteFile("C:\My folder\My file.txt") Then
MsgBox "File deleted"
Else
MsgBox "Aborted"
End If
See ya :wave:Code:If DeleteFile("C:\My folder\My file.txt", True) Then
MsgBox "File sent to RecycleBin"
Else
MsgBox "Aborted"
End If
Sorry to resurrect an old thread but I cannot get the kill command to work. I have read and read and read the various threads about this problem but none of the solutions seem to work for me.
I am trying to delete a text file from a non protected directory on a machine where I have admin rights. Specifically:
The file remains and there is no indication it didn't work (no errors thrown) other than the file remainsCode:Dim TempString As String
TempString = "C:\Test Directory\Test.txt"
Kill TempString
Sometimes it takes a few minutes for a deleted file to fully disappear.
Or is it possible you've suppressed errors (e.g. On Error Resume Next) and are trying to delete a read only file? (in which case you need only remove the Read Only attribute first, SetAttr TempString, vbNormal)
File is not RO.
Thanks for the suggestion OptionBase1 but that didn't work either.
I've even tried
DeleteAFile TempString, 0Code:Public Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String, ByVal bFailIfExists As Long) As Long
With the same (ineffective) results
This shell32 API is really nasty...it DOES overrun/ignores ALL the dialoges the shell is so famous for so this does it really "infamous" :bigyello:
It deletes the folder and ALL the subfolders in a blink (without sending them to the recycle bin) and updates the Explorer.exe before you even had said "hmm" :bigyello:Code:Public Declare Function Win32DeleteFile Lib "shell32.dll" (ByVal pszPath As Long) As Long
So this API is TERMINAL for a Folder!!
"SHFileOperation" requires the file name string to be double null terminated otherwise bad things can happen! It's a dangerous API to use improperly. It has an advantage though, namely it allows wildcards (*) in the file name so it can delete multiple files at once:
If you only want to delete a single file then "DeleteFileW" does the job:Code:Public Function FileDeleteWildcard(sPath As String) As Long
Dim tFOS As SHFILEOPSTRUCT, sBuffer As String
Const FOF_NO_UI As Long = FOF_SILENT Or FOF_NOCONFIRMATION Or FOF_NOCONFIRMMKDIR Or &H400 ' FOF_NOERRORUI
sBuffer = String$(cMaxPath, vbNullChar): Mid$(sBuffer, 1) = sPath
With tFOS: .pFrom = StrPtr(sBuffer): .wFunc = FO_DELETE: .fFlags = FOF_NO_UI: End With
FileDeleteWildcard = SHFileOperationW(VarPtr(tFOS))
End Function
Code:Private Declare Function DeleteFileW Lib "kernel32" (ByVal lpFileName As Long) As Long
It prints success (and if I modify the code to report error code) and a 0
I don't see what might prevent it from working however as EVERYONE on this machine has complete access to this computer's file system.
It is Win 11 if that makes any difference
Are you able to manually delete the file yourself via Windows Explorer? If so, does the file re-appear somehow on it's own?
Use this and you don't have to bother more of that file or folder or iif it prints success...:bigyello:
Code:Public Declare Function Win32DeleteFile Lib "shell32.dll" (ByVal pszPath As Long) As Long
Yup, no problem manually deleting it and it does not re-appear on its own, nope
I would start to question if you have some sort of antivirus/antimalware software installed that is doing some sort of activity monitoring and preventing an "unsigned" exe file from deleting files that don't appear related to the exe file. Complete shot in the dark, though.
I do beleve Win32DeleteFile API ignores all this...try to delete Explorer.exe or shell32.dll with Win32DeleteFile API.
Win32DeleteFile returns 1 on MsgBox Win32DeleteFile(StrPtr("C:\Windows\explorer.exe")) :bigyello:
But it still remains in the Windows folder.
Is the only code in your program the code that tries to delete the file? Is there code in your program that interacts with that file that perhaps doesn't complete or close the file before you try to delete it?
Good luck.
Rather than making us guess what your code is doing, at this point you should be posting all the code that touches that file.
You can use a tool like ProcessHacker to find the process which has it open (for PH, under the Hacker menu, 'Find files or DLLs').
If it's your own, or VB6 when running in the IDE, you haven't properly closed it. If it's another app, see why that has it open, and if you could automate closing it if need be.
You *could* forcibly delete it with some advanced methods, but that's not a good idea.
Another option is you could set it to be deleted on reboot;
Then use MoveFileExW StrPtr(TempString), 0, MOVEFILE_DELAY_UNTIL_REBOOTCode:Public Enum MoveFileExFlags
MOVEFILE_REPLACE_EXISTING = &H00000001
MOVEFILE_COPY_ALLOWED = &H00000002
MOVEFILE_DELAY_UNTIL_REBOOT = &H00000004
MOVEFILE_WRITE_THROUGH = &H00000008
MOVEFILE_CREATE_HARDLINK = &H00000010
MOVEFILE_FAIL_IF_NOT_TRACKABLE = &H00000020
End Enum
#If VBA7 Then
Public Declare PtrSafe Function MoveFileExW Lib "kernel32" (ByVal lpExistingFileName As LongPtr, ByVal lpNewFileName As LongPtr, ByVal dwFlags As MoveFileExFlags) As Long
#Else
Public Declare Function MoveFileExW Lib "kernel32" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long, ByVal dwFlags As MoveFileExFlags) As Long
#End If
A final followup on this.
Turns out that old issue of needing a 2nd set of eyes to see something that I kept missing, was needed (yes, I should have posted my code). I was not properly closing the file before I was trying to delete it. Sorry to taken y'all's time and again, thanks for the help