Avatar of Andreas Hermle
Andreas Hermle
Flag for Germany asked on

delete specific jpg files from folders and subfolders using Excel VBA

Dear Experts:

I understand that it is possible to delete files from a folder using Excel VBA

I am running Excel 2010 on Windows 7

I would like to delete specific jpg files from a directory with many folders and subfolders. The directory's name is ...
... C\Graphics\...

The jpg files that are to be deleted are only those that have the following characters at the end:

_h.jpg or _n.jpg, i.e. for example 90-147-58-30_h.jpg or 94-111-24-39_n.jpg. All other jpg files that do not match these criteria are not to be touched.

Help is very much appreciated. Thank you very much in advance.

Kind regards, Andreas
Microsoft ExcelVBA

Avatar of undefined
Last Comment
Professor J

8/22/2022 - Mon
Saurabh Singh Teotia

So just to make sure if i understand this correctly you want to delete all the jpg files from folder and sub-folder which ends with h or n?

Is my understanding correct?

Saurabh..
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi Saurabh,

yes you are right, that is exactly what I want.

Thank you. Regards, Andreas
Andreas Hermle

ASKER
Hi Olemo,

thank you very much for your professional help. I am sure that your DOS-command works just fine, but I am afraid to tell you that I use Windows 7 64 bit and I understand that DOS-command won't run on a Windows 7 64 bit system.

I got a workaround (http://windows.microsoft.com/en-us/windows7/install-and-use-windows-xp-mode-in-windows-7) but as a matter of fact I do not want to install anything (Windows XP Mode, and Windows Virtual PC).

Thank you again for your great help. Regards, Andreas
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Qlemo

You were mislead by my usage of "DOS" - it's used sometimes for the good old-fashioned batch file stuff, as I used it here. I meant "cmd.exe batch". Start a command prompt, and try my code.
Doing that in VBA or VBS requires much more of code.
ASKER CERTIFIED SOLUTION
Professor J

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Qlemo

See what I mean? ;-)
Andreas Hermle

ASKER
Hi Qlemo, I am currently trying your DOS command.

Am I right, I press the Windows Key + R to bring up the Run Window. And then I enter cmd.exe to launch the command prompt. The window in the command prompt says: C:\Users\Andreas>

I then enter your command prompt right after the 'greater than' sign, am I right?

Regards, andreas and thank you very much for your help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi ProfJimJam,

great coding, I am really impressed, works just fine. Thank you very much for it.

I will try Qlemo's command prompt and then award the points.

Again, thank you very much for your swift and professional support. Regards, Andreas
Qlemo

Exactly. Make sure to change the path (two times) to a test folder, the command will really delete files!
Andreas Hermle

ASKER
Qlemo, thank you very much for your help. Will make the testing this evening, hence it'll be tonight when I award points.

ProfJimJam, again thank you very much for your superb code.

Regards, Andreas
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Andreas Hermle

ASKER
Dear Qlemo,

works really great, thank you very much for your professional support. I really appreciate it.

Dear Qlemo and ProfJimJam,

Since I was initially asking for a VBA solution, I will award more points to ProfJimJam.

You both deserve 500 points, but regrettably this is not possible. Again, thank you very much for your superb, swift and professional help. This forum has saved me days and months of tedious and exhausting work.

Have a nice day, regards, Andreas
Professor J

Thank you Andreas for your nice feedback
Wish you a nice day too
Regards