Results 1 to 6 of 6

Thread: how to delete one file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    ma,usa
    Posts
    485

    how to delete one file?

    How do I delete a file through vb?

  2. #2
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Well..

    VB Code:
    1. Kill "c:\myfile.txt"

    Just type Kill and the pathname

    Hope this helps.

    -Jeremy

  3. #3
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    This maybe extreme but gives you the option of the recycle bin

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type SHFILEOPSTRUCT
    4.     hWnd As Long
    5.     wFunc As Long
    6.     pFrom As String
    7.     pTo As String
    8.     fFlags As Integer
    9.     fAborted As Boolean
    10.     hNameMaps As Long
    11.     sProgress As String
    12. End Type
    13.  
    14. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    15.  
    16. Const FOF_NOCONFIRMATION = &H10             '  Don't prompt the user.
    17. Const FOF_NOCONFIRMMKDIR = &H200            '  don't confirm making any needed dirs
    18. Const FO_COPY = &H2
    19. Const FO_DELETE = &H3
    20. Const FOF_ALLOWUNDO = &H40
    21. Const FOF_FILESONLY = &H80
    22.  
    23.  
    24. Public Function NUKILL(PATH As String, Optional RECYCLE As Boolean = True) As Boolean
    25.    
    26.     ' NUCLEUS
    27.     ' DELETE FILE(S) OR FOLDER(S) (INCLUDING SUBDIRECTORIES) AND OPTIONALLY SEND TO RECYCLE BIN
    28.     ' IN: PATH OF FILE/FOLDER
    29.     ' OUT: BOOLEAN INDICATING SUCCESS
    30.    
    31.     If Right(PATH, 1) = "\" Then PATH = Left(PATH, Len(PATH) - 1)
    32.    
    33.     If Len(Dir$(PATH)) <> 0 Or Len(Dir$(PATH, vbDirectory)) <> 0 Then
    34.        
    35.         Dim SHFileOp As SHFILEOPSTRUCT
    36.        
    37.         With SHFileOp
    38.            
    39.             ' DELETE THE FILE
    40.            
    41.             .wFunc = FO_DELETE
    42.            
    43.             ' SELECT THE FILE
    44.            
    45.             .pFrom = PATH
    46.            
    47.             .fFlags = FOF_NOCONFIRMATION Or FOF_FILESONLY
    48.            
    49.             ' NEXT OPTIONALLY ALLOW MOVE TO RECYCLE BIN
    50.             ' WHICH ONLY WORKS FOR WHOLE FOLDERS OR SINGLE FILE OPERATIONS
    51.             ' IT WILL NOT SEND *.* OR *.TXT TO RECYCLE BIN
    52.             ' DUE TO BUILT-IN LIMITATIONS IN API FUNCTION
    53.            
    54.             If RECYCLE Then .fFlags = .fFlags Or FOF_ALLOWUNDO
    55.            
    56.         End With
    57.        
    58.         ' PERFORM FILE OPERATION
    59.        
    60.         NUKILL = (SHFileOperation(SHFileOp) = 0)
    61.        
    62.     End If
    63.    
    64. End Function
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Omaha, NE
    Posts
    270
    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.
    VB Code:
    1. Dim fso As FileSystemObject
    2.     Dim strFileName As String
    3.     strFileName = "c:\temp\Junk1.txt"
    4.    
    5.     Set fso = New FileSystemObject
    6.     fso.DeleteFile strFileName
    Nate

  5. #5
    Fanatic Member duc's Avatar
    Join Date
    Oct 2002
    Location
    STEAM
    Posts
    702

    easy

    Kill "C:\example.exe"

    you can always change the directory. dont use this maliciously thanks

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Re: easy

    Originally posted by duc
    Kill "C:\example.exe"

    you can always change the directory. dont use this maliciously thanks
    Why are you digging up old threads?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width