# How to delete JPG files, but only if the matching RAW file exists?

My early (Canon G2) photos are all JPG, but when I got my Nikon D90 I initially shot in JPG, then switched to RAW+JPG, and now I would like to switch to RAW only.

I have literally thousands of photos on my HDD. The photos are in sub-directories (by date) under a single directory called Import.

I am about to import all these photos into Lightroom 3.0, however, I would like to delete all the JPG files, but only where there is already a corresponding RAW file (ie. I no longer want to keep JPG and RAW versions of the same file).

If I can do this easily within Lightroom (after importing everything, including the duplicate JPG files) that would be great. It would also be OK if there were an easy way to do this before importing the files (but hopefully this wouldn't involve having to visit every directory looking for filenames with both JPG and NEF extensions).

Does anybody know of a way to do this (in Lightroom, or with some tool/script in Windows)?

-
Do all of your jpg files and RAW files have at a minimum the same string(other characters could be added to either)? Eg IMG_1234_portrait_picture.jpg & IMG_1234.CR2. –  dpollitt Oct 15 '11 at 3:28
I think that all of my JPG/NEF pairs have exactly the same filename (except for the extension). –  seanmc Oct 15 '11 at 3:29
I will vote to move this to Stack Overflow, where you should get an answer within minutes =) –  anon Oct 15 '11 at 15:17
@anon: How exactly does this belong on StackOverflow? This is most definitely an on-topic question here, as it deals with image management and photo editing tools. Outside of a tangential reference to script...this has nothing to do with programming. –  jrista Oct 15 '11 at 17:04
@jrista - The asker said it was okay to process the files/folders before importing into Lightroom. To me, a scripting solution would be ideal because it would be tool-independent, faster, more portable, and customizable. In an abstract sense it is even independent of the actual type of files we're dealing with. But then again, I'm a programmer =) –  anon Oct 15 '11 at 17:27

In your command prompt, go to the folder and run this:

for /f "delims==" %r in ('dir /b *.nef') do del "%~dpr%~nr.jpg" 2> nul


Basically, it goes through the current folder, runs through the .NEF files, and deletes the JPG if present. It ignores any errors if the JPG is not there.

If you want subfolders, include "/s" (without quotes) in the dir command.

-
Perfect, thanks! Of course before I ran it the first time, I changed "del" to "echo". Then I ran "help for" to understand what it was doing. It's obviously been a long time since I've looked at command prompt scripting, because I had no idea that the "for" command had so many options. –  seanmc Oct 16 '11 at 20:14
No problem! As I was testing, I also used "echo" =) To see more output, remove the "2> nul." I'd been meaning to do something like this for my own NEF/JPG files, and this was the perfect opportunity. –  anon Oct 17 '11 at 2:14
• Create an empty Library
• From the Lightroom main menu, choose Edit > Preferences (Windows) or Lightroom > Preferences (Mac OS).
• In the General preferences unselect "Treat JPEG Files Next To Raw Files As Separate Photos"
• This should be the default.
• Import all of your files (you can select search subfolders), telling it to move to a new location
• The JPG files that have RAW files will be left in the original location for you to remove

As I understand it, the thumbnail in lightroom might say RAW+JPG, but the JPG isn't actually stored or accessible in any way.

You can also write a pretty simple batch script with any programming language.

-

Here's a Python script which moves .jpg files, if no RAW exists. Useful in Mac OS!

import os
import shutil

raw_ext = '.CR2'
jpg_ext = '.JPG'
destination = '/Users/JohnSmith/Desktop/jpgs/'

for filename in os.listdir('.'):
(shortname, extension) = os.path.splitext(filename)

if extension == raw_ext:
if os.path.isfile(shortname + jpg_ext):
print 'Moving ' + shortname + jpg_ext + '...'
shutil.move(shortname + jpg_ext, destination)

-

I wrote following Pyhton script for this.


# Script:      remove_jpg_if_raw_exists.py
#
# Description: This script looks in all sub directories for
#              pairs of JPG and RAW files.
#              For each pair found the JPG is moved to a
#              Otherwise JPG is kept.
#
# Author:      Thomas Dahlmann

import os, fnmatch

# define your file extensions here, case is ignored
raw_extension = "nef"
jpg_extension = "jpg"

# define waste basket directory here

##### do not modify below ##########

# recursive find files
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern
in and below root directory.'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)

# get base names from raw's
raw_hash = {}
for raw in locate("*." + raw_extension):
base_name = os.path.basename(raw)
base_name = os.path.splitext(base_name)[0]
raw_hash[base_name] = True

if not os.path.exists(waste_dir):
os.makedirs(waste_dir)

# find pairs and move jpgs of pairs to waste basket
for jpg in locate("*." + jpg_extension):
base_name = os.path.basename(jpg)
base_name = os.path.splitext(base_name)[0]
if base_name in raw_hash:
jpg_base_name_with_ext = base_name + "." + jpg_extension
new_jpg = waste_dir + "\\" + jpg_base_name_with_ext
print "%s => %s" % (jpg, waste_dir)
if os.path.exists(new_jpg):
os.remove(jpg)
else:
os.rename(jpg, new_jpg)

-
Hi and welcome to Photo.SE. How is your answer different from ttaveira's answer? –  Bart Arondson Apr 8 at 14:56
The script does some extra work: also looks in all sub-directories, creates destination waste directory for jpg's if it does no exist and it does remove a jpg instead of moving if it if it already exists in waste directory (avoids moving errors) –  Tomy Apr 30 at 15:58

Here is a modified version of Tomy's script.

The differences:

• multiple raw extensions allowed
• remove jpg only if the pairs are in the same folder (avoid accidental removal of a jpg named like a raw file in an other folder)
• case insensitive

# Script:      remove_jpg_if_raw_exists.py
#
# Description: This script looks in all sub directories for
#              pairs of JPG and RAW files.
#              For each pair found the JPG is moved to a
#              Otherwise JPG is kept.
#
# Author:      Thomas Dahlmann
# Modified by: Renaud Boitouzet

import os

# define your file extensions here, case is ignored.
# multiple raw extensions allowed, single jpg extension only
raw_extensions = (".Dng", ".cR2", ".nef", ".crw")
jpg_extension = ".jPg"

# define waste basket directory here. Include trainling slash or backslash.
# Windows : waste_dir = "C:\path\to\waste\"
waste_dir = "/path/to/waste/"

##### do not modify below ##########

# find files
def locate(folder, extensions):
'''Locate files in directory with given extensions'''
for filename in os.listdir(folder):
if filename.endswith(extensions):
yield os.path.join(folder, filename)

if not os.path.exists(waste_dir):
os.makedirs(waste_dir)

# Make search case insensitive
raw_ext = tuple(map(str.lower,raw_extensions)) + tuple(map(str.upper,raw_extensions))
jpg_ext = (jpg_extension.lower(), jpg_extension.upper())

root=os.curdir
#find subdirectories
for path, dirs, files in os.walk(os.path.abspath(root)):
print path
raw_hash = {}
for raw in locate(path, raw_ext):
base_name = os.path.basename(raw)
base_name = os.path.splitext(base_name)[0]
raw_hash[base_name] = True

# find pairs and move jpgs of pairs to waste basket
for jpg in locate(path, jpg_ext):
base_name = os.path.basename(jpg)
base_name = os.path.splitext(base_name)[0]
if base_name in raw_hash:
jpg_base_name_with_ext = base_name + jpg_extension
new_jpg = waste_dir + jpg_base_name_with_ext
print "%s: %s = %s => %s" % (path, base_name, jpg, waste_dir)
if os.path.exists(new_jpg):
os.remove(jpg)
else:
os.rename(jpg, new_jpg)

-