Avoid double .png file with same data

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Avoid double .png file with same data

Post by jmburnod » Thu Jan 30, 2020 7:25 pm

Hi All,
Before copy an image file .png in a destination folderI i needed to check if destination folder contains an image file .png with the same data (file name can be different).
Here is a script does the job, but a simpler way would be welcome

Code: Select all

on mouseUp
   answer file "Select one image file .png" 
   if it = empty then exit mouseup
   put it into tFileS
   answer folder "Select a destination folder contains image files .png"
   if it = empty then exit mouseup
   put it into tFolD
   put getImageWithSameDataInFolder(tFileS,tFolD) into tSameImg
   if tSameImg = empty then
      put "no image file .png with a same data in folder" && tFolD into tMes
   else
      put tFileS & cr & tSameImg & cr & "have same data" into tMes
   end if
   answer tMes
end mouseUp

--•• get files with same data in destination folder
function getImageWithSameDataInFolder pFileS,pFolD
   put empty into rgetImageWithSameDataInFolder
   set the itemdel to "/" 
   put item -1 of pFileS into tNameFileS
   put item 1 to -2 of pFileS into tPahFolS
   set the itemdel to "," 
   put LesDetailsFi(tPahFolS) into tDetFilesS
   put LesDetailsFi(pFolD) into tDetFilesD
   put urlencode(tNameFileS) into tNameImgUrl
   put GetFilesSameSizeInFolder(pFileS,tNameImgUrl,tDetFilesS,tDetFilesD) into tFilesSameSize
   if tFilesSameSize <>empty  then --•• files with same size exits
      put GetSameImgInFilesSameSize(pFileS,tFilesSameSize,pFolD) into tPathFileSameImg
      if tPathFileSameImg <> empty then --•• one img with same data exists
         put tPathFileSameImg into rgetImageWithSameDataInFolder
      else
      end if
   end if
   return rgetImageWithSameDataInFolder
end getImageWithSameDataInFolder

--•• get all detailed files
function LesDetailsFi pFol --•• les infos détaillées d'un fichier
   local tOldDefaultFolder, tDetailedFiles
   put the defaultFolder into tOldDefaultFolder
   set the defaultFolder to pFol
   put the detailed files into tDetailedFiles
   set the defaultFolder to tOldDefaultFolder
   return tDetailedFiles
end LesDetailsFi

--••  get files with same size in destination folder
function GetFilesSameSizeInFolder pFileS,pNameFi,pDetFilesS,pDetFilesD
   put empty into rGetFilesSameSizeInFolder
   put lineoffset(pNameFi,pDetFilesS) into tFoundLi
   get line tFoundLi of pDetFilesS
   put item 2 of it into tSize1
   put item 3 of it into tSize2 
   put "*," & tSize1 & "," & tSize2 & ",*" into tFilter
   filter pDetFilesD  with tFilter
   put pDetFilesD into rGetFilesSameSizeInFolder
   return rGetFilesSameSizeInFolder
end GetFilesSameSizeInFolder
   
--•• get files with the same data among files of destination folder have same size 
function GetSameImgInFilesSameSize pOneFile,pFiles,pFolD
   put empty into rGetSameImgInFilesSameSize
   put url("File:" & pOneFile) into tDataImg
   repeat for each line tFileUrl in pFiles
      put urldecode(item 1 of tFileUrl) into tNameFile
      put pFolD & "/" & tNameFile into tPathVerif
      put url("File:" & tPathVerif) into tDataImgVerif
      if tDataImgVerif = tDataImg then
         put tPathVerif into rGetSameImgInFilesSameSize
      end if
   end repeat
   return rGetSameImgInFilesSameSize --(tDataImgVerif = tDataImg)
end GetSameImgInFilesSameSize
Best regards
Jean-Marc
https://alternatic.ch

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Avoid double .png file with same data

Post by [-hh] » Fri Jan 31, 2020 1:32 am

Hi Jean-Marc,

there is certainly no simpler way.
But you could also try the following that is a little bit shorter (and assumes that the resource fork size isn't needed any more).

Code: Select all

-- reports *all* png files in destination folder that have compare-data
on mouseUp
  put url("binfile:/Users/admin/tum.png") into tData --> compare-data
  put "/Users/admin/Pictures/" into tFolder --> destination folder
  put files (tFolder,"detailed") into tList
  filter tList with ("*.png",length(tData),"*") --> same sizes
  repeat for each line L in tList --> urlDecode in case comma is used
    put urlDecode(tFolder & item 1 of L) into tFile
    if url("binfile:" & tFile) is tData
    then put tFile & cr after tResult --> same data
  end repeat
  put tResult & cr & tList into fld "OUT"
end mouseUp
shiftLock happens

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Avoid double .png file with same data

Post by jmburnod » Fri Jan 31, 2020 5:56 pm

Hi Hermann,
Thanks again for help
Your script works like a charm
I particulary love

Code: Select all

  filter tList with ("*.png",length(tData),"*") --> same sizes
Simpler and clearer than mine. Caramba ! why i didnt think to use the lenght of data to know the size.
Kind regards
Jean-Marc
https://alternatic.ch

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”