Showing posts with label Delete Folder. Show all posts
Showing posts with label Delete Folder. Show all posts

17 June 2014

Delete Files and Folders - VBA

In this blog you will learn how to:
  • Delete a file
  • Delete all the files in a folder
  • Delete/Create a directory using VBA 
Please note that Kill command permanently deletes the file. we cant "undo" the delete.
'Macro Purpose: To Create/Remove a folder
Sub Create_RemoveDirectory()
    On Error Resume Next
    Kill "C:\Test\*"          ' Deletes all the files in the folder Test
    RmDir "C:\Test\"       ' Deletes empty folder
    MkDir "C:\Test"         ' Creates a Folder name Test
    On Error GoTo 0
End Sub

'Macro Purpose : Deletes given file & deletes all *.TXT files in current directory.
Sub DeleteFile()
    On Error Resume Next
    Kill ("C:\Test1.xlsx")
    Kill ("C:\Test\*.txt")
    On Error GoTo 0
End Sub
Execute the code step by step for better understanding
Sub SampleProcedure()
    Dim fpath As String, ffile As String
    fpath = "C:\Test\"
    ffile = Dir(fpath + "*.txt")
    
    Do While ffile <> ""
        Kill (fpath & ffile)
        ffile = Dir
    Loop        
    MsgBox "Files deleted in the given folder"
End Sub
                                                Hope this would help beginners J

30 May 2014

Unable to Delete Folder - "Could not find this item"

Could not find this item - when deleting files/folders

Issue:


Folder that will not allow you to rename, move or delete. But you could successfully delete the contents of the folder.

However when you try to delete the folder you get the error message à

Solution:


The only way to fix this is :
Delete the file from the command prompt with administrator privileges.

Steps:

Step A:
  • Go to Start menu
  • Type 'Cmd' in the search box
  • Select and Right click it
  • Select run as Administrator

Step B:

Step B1: Refer - point 1 in the below image
  • Browse to the folder containing the affected file/folder 
  • Enter the path command window
  • CD \to\the\folder\containing\the\problem\file

Step B2: Refer - point 2 in the below image
  • Type 'dir /a/x/p' to obtain the 8.3 formatted filename 

Step B3: Refer - point 3 in the below image
  • Note the short filename(8.3 formatted filename) of the problem file
  • It will look for example like SAMPLE~1


Refer this screen shot
Step B4: Refer - point 4 

Delete folder
  • To delete use the 8.3 filename
  • Eg : Type rd/s AFFECT~1 or del AFFECT~1 and enter then select Y to delete

Rename folder
  • Use Rename or Ren command to rename the File or Folder(use 8.3 filename)
  • Eg : ren AFFECT~1 "New filename"

                !************************************************** It's Done J ***************************************!