Skip to content

Instantly share code, notes, and snippets.

@error454
Created September 19, 2014 08:39
Show Gist options
  • Save error454/0943cf7899c20f5f2b48 to your computer and use it in GitHub Desktop.
Save error454/0943cf7899c20f5f2b48 to your computer and use it in GitHub Desktop.
Delete JPGs that don't have a matching raw file
lfs = require "lfs"
local sPath = 'D:/temp photos/'
function hasExtension ( sFilename, sExt )
if sFilename then
return string.match ( string.lower ( sFilename ), "." .. string.lower( sExt ) ) ~= nil
end
return false
end
function getFileNameAndExtension ( sFilename )
if sFilename then
-- match (anything not a .).(anything not a .)
for k, v in string.gmatch ( sFilename, "([^\.]+)\.([^\.]+)" ) do
return k, v
end
end
return nil, nil
end
function file_exists(name)
local f=io.open(sPath .. name,"r")
if f~=nil then io.close(f) return true else return false end
end
local nJpg = 0
local nRAW = 0
local nMissingRaws = 0
local nMissingJPGS = 0
local nOther = 0
for file in lfs.dir(sPath) do
local sName, sExt = getFileNameAndExtension ( file )
if sExt ~= nil then
if hasExtension ( file, "jpg" ) then
nJpg = nJpg + 1
if not file_exists ( sName .. ".CR2" ) then
nMissingRaws = nMissingRaws + 1
end
elseif hasExtension ( file, "cr2" ) then
nRAW = nRAW + 1
if not file_exists ( sName .. ".JPG" ) then
nMissingJPGS = nMissingJPGS + 1
--print ( "Delete ", file )
os.remove ( sPath .. file )
end
else
nOther = nOther + 1
end
end
end
print("Found JPG: ", nJpg)
print("Found RAW: ", nRAW)
print("Found other: ", nOther)
print("Total: ", (nJpg + nRAW + nOther))
print ( "Missing JPG: ", nMissingJPGS )
print ( "Missing RAW: ", nMissingRaws )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment