Help deleting files

Tridiro

Board Regular
Joined
Apr 26, 2007
Messages
97
Hello,

I have about 1300 .jpg files in a folder. the picture files are named a 6 digit number (548712.jpg). In column F resides the corresponding number to the pictures (548712). There are only about 500 numbers in column F.

I need some code that will delete the picture file if it's name is not in column F. Approximately 700 Pictures.

So.... in a nutshell, I need all the files deleted in a folder unless its name is in column F.

Thanks in advance
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi, Tridiro.

Where are all the picture files located at?
Are they all in the same folder and what is their path?
 
Last edited:
Upvote 0
This is what I have come up with, but it does not work. i used .txt files instead of .jpg until i get it to work.

My idea here was to loop thru each cell to determined if the file exist. If it does exist i was going to change the .txt extension to .txt1. Then delete all the remaining .txt files. then rename all the .txt1 back to .txt.

I'm sure there is a better way. just not sure on how to accomplish it.



Code:
'On Error Resume Next
Range("F2").Select

Do Until ActiveCell.Value = ""

    If Dir("C:\Users\Will\Desktop\New\" & ActiveCell.Value & ".txt") <> "" Then
        Oldname = "C:\Users\Will\Desktop\New\" & ActiveCell.Value & ".txt": Newname = "C:\Users\Will\Desktop\New\" & ActiveCell.Value & ".txt1"
        
    Else
    End If

    ActiveCell.Offset(1, 0).Select

Loop

Kill ("C:\Users\Will\Desktop\New\*.txt")

Oldname = "G:\Alternative programs\HIP\Payments\Mugs\*.txt1": Newname = "G:\Alternative programs\HIP\Payments\Mugs\*.txt"

On Error GoTo 0
End Sub
 
Upvote 0
You need something like

Code:
Option Explicit
 
Function FileOrDirExists(PathName As String) As Boolean
       
Dim itemp As Integer
     
On Error Resume Next
itemp = GetAttr(PathName)
Select Case Err.Number
    Case 0
        FileOrDirExists = True
    Case Else
        FileOrDirExists = False
End Select
On Error GoTo 0
End Function

in conjunction with something like

Code:
If FileOrDirExists(filehere) Then
Kill (filehere)
End If

where filehere = the name of the picture you're checking for
 
Upvote 0
Thanks for the help, but if I'm understanding your code correctly, would that not delete the files I'm trying to keep?
 
Upvote 0
Apologies, I misread your post, this

Code:
Option Explicit
 
Function FileOrDirExists(PathName As String) As Boolean
       
Dim itemp As Integer
     
On Error Resume Next
itemp = GetAttr(PathName)
Select Case Err.Number
    Case 0
        FileOrDirExists = True
    Case Else
        FileOrDirExists = False
End Select
On Error GoTo 0
End Function

Sub chuckles1066()
'
'globally declare variables
'
Const fileloc As String = "i:\updates\chuckles"
Dim filelocandname As String
Dim rowc As Long
Dim count As Long

rowc = Range("F" & Rows.count).End(xlUp).Row

For count = 2 To rowc
filelocandname = fileloc & "\" & Cells(count, 6).Value & ".jpg"
If FileOrDirExists(filelocandname) Then
Kill (filelocandname)
End If
Next
End Sub

will delete anything that it finds in Column F.

It's a starting point, I'm still a novice with VBA!
 
Upvote 0
Or:

Code:
Sub Wigi()
    Const fileloc As String = "i:\updates\chuckles"
    On Error Resume Next
    For count = 2 To Range("F" & Rows.count).End(xlUp).Row
        Kill fileloc & "\" & Cells(count, 6).Value & ".jpg"
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
Members
452,902
Latest member
Knuddeluff

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