Excel VBA to look for and delete or move 2 files in Downloads folder

gmooney

Active Member
Joined
Oct 21, 2004
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Looking for some code that will search a users Downloads folder for 2 particular files. If it finds one called Category Review BUCategory.txt I want to delete that file before the rest of my code adds a new version of this file.

Then if a file called Category Review Business Unit - Category.pptx is found, prompt the user that if they want to save that file move it somewhere outside of the Downloads folder because. If they are ok with not needing the file, to go ahead and delete the file because a new version of the file will be downloaded into the Downloads folder.

Any thoughts?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi gmooney,

try this code:
VBA Code:
Sub test()
'https://www.mrexcel.com/board/threads/excel-vba-to-look-for-and-delete-or-move-2-files-in-downloads-folder.1176421/post-5721748
    
    Dim MyPath As String, MyFileTxt As String, MyfilePPT As String
    
    MyPath = Environ("USERPROFILE") & "\Downloads\"
    MyFileTxt = "Category Review BUCategory.txt"
    MyfilePPT = "Category Review Business Unit - Category.pptx"
    
    If Not Dir(MyPath & MyFileTxt, vbDirectory) = vbNullString Then
'delete file if exists
        Kill MyPath & MyFileTxt
    End If
    
    If Not Dir(MyPath & MyfilePPT, vbDirectory) = vbNullString Then
'prompt the user
        MsgBox "Please....."
    End If
    
End Sub
 
Upvote 0
Hi gmooney,

try this code:
VBA Code:
Sub test()
'https://www.mrexcel.com/board/threads/excel-vba-to-look-for-and-delete-or-move-2-files-in-downloads-folder.1176421/post-5721748
   
    Dim MyPath As String, MyFileTxt As String, MyfilePPT As String
   
    MyPath = Environ("USERPROFILE") & "\Downloads\"
    MyFileTxt = "Category Review BUCategory.txt"
    MyfilePPT = "Category Review Business Unit - Category.pptx"
   
    If Not Dir(MyPath & MyFileTxt, vbDirectory) = vbNullString Then
'delete file if exists
        Kill MyPath & MyFileTxt
    End If
   
    If Not Dir(MyPath & MyfilePPT, vbDirectory) = vbNullString Then
'prompt the user
        MsgBox "Please....."
    End If
   
End Sub
Hi Sequoyah.....I had already figured out how to Kill the text file because it needs to be killed every time. As far as the PPTX file any way to prompt the user with a dialog box that give them the ability to select Move or Delete? and if Delete kill it and if Move, another dialog box to move file to new folder?
 
Upvote 0
Hi, here is the new code:
VBA Code:
Sub test2()
'https://www.mrexcel.com/board/threads/excel-vba-to-look-for-and-delete-or-move-2-files-in-downloads-folder.1176421/post-5721748
    
    Dim MyPath As String, MyNewPath As String, MyFileTxt As String, MyfilePPT As String
    
    MyPath = Environ("USERPROFILE") & "\Downloads\"
    MyNewPath = Environ("USERPROFILE") & "\Downloads\Backup\"        'Modify as needed
    MyFileTxt = "Category Review BUCategory.txt"
    MyfilePPT = "Category Review Business Unit - Category.pptx"
    
    If Not Dir(MyPath & MyFileTxt, vbDirectory) = vbNullString Then
'delete txt file if exists
        Kill MyPath & MyFileTxt
    End If
    
    If Not Dir(MyPath & MyfilePPT, vbDirectory) = vbNullString Then
'prompt the user if file PPTX exixts
        If MsgBox("The file " & MyfilePPT & " already exists." & Chr(10) & _
" Do you want To keep it?" & Chr(10) & "If you press NO it will be deleted!", vbYesNo + vbExclamation, "test") = vbYes Then
'if YES is pressed, create new folder if it doesn't exists
        With CreateObject("Scripting.FileSystemObject")
            If Not .FolderExists(MyNewPath) Then .CreateFolder MyNewPath
            
            .CopyFile MyPath & MyfilePPT, MyNewPath & MyfilePPT, True        'Copy fie to new folder, overwrite if exists, then delete it
            Kill MyPath & MyfilePPT
        End With
        
    Else
'if NO is pressed, delete PPTX file
        Kill MyPath & MyfilePPT
    End If
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,663
Messages
6,126,097
Members
449,291
Latest member
atfoley16

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top