VBA Upper and Lower case

prati

Board Regular
Joined
Jan 25, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hey friends,

I need help in order to write a VBA code much shorter and in a smart way.

I want to delete files located at specific folder, all the files except of pdf files.

The problem is that there are 8 extensions variations for the name "pdf" files

pdf, Pdf, PDf, pDF,pdF,PDF,pDf,PdF

In other words, for every file in the chosen folder the code make 8 checks:



VBA Code:
Sub Delete_Files_Not_Pdf()

'   Enter file path to delete files from

    myFolderName = "C:\Atricles\"
    myFileName = Dir(myFolderName & "*.*")
    
'   Delete all files without an pdf extension

    Do While myFileName <> ""
        If Right(myFileName, 3) <> "pdf" And Right(myFileName, 3) <> "Pdf" And Right(myFileName, 3) <> "PDf" And Right(myFileName, 3) <> "pDF" And Right(myFileName, 3) <> "pdF" And Right(myFileName, 3) <> "PDF" And  Right(myFileName, 3) <> "pDf" And Right(myFileName, 3) <> "PdF" Then Kill myFolderName & myFileName
        myFileName = Dir
    Loop

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi
Try
VBA Code:
 Do While myFileName <> ""
        If LCase(Right(myFileName, 3)) <> "pdf" Then Kill myFolderName & myFileName
        myFileName = Dir
    Loop
 
Upvote 0
Solution
Hi
Try
VBA Code:
 Do While myFileName <> ""
        If LCase(Right(myFileName, 3)) <> "pdf" Then Kill myFolderName & myFileName
        myFileName = Dir
    Loop
Thank a lot
That is exactly what i have searched for
 
Upvote 0
You are welcome
And thank you for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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