Announcement

Collapse
No announcement yet.

Trying to catalog all .jpg files. Could someone tell me what I am doing wrong?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to catalog all .jpg files. Could someone tell me what I am doing wrong?

    When this is done it should help me get rid of duplicates of .jpg files - as I have backups of backups of backups!!!
    This is a work in progress!!


    This is giving me back screwy dates and times

    Code:
    FUNCTION MORE_INFO(FILE_NAME AS STRING) AS STRING
    
    
      LOCAL FILE_INFORMATION    AS DIRDATA
      LOCAL FILE_STRING         AS STRING
      LOCAL Time_String_1       AS STRING
      LOCAL Time_String_2       AS STRING
      LOCAL Time_String_3       AS STRING
    
      LOCAL MyTime AS IPOWERTIME
      LET MyTime = CLASS "PowerTime"
    
      LOCAL My_FileAttributes        AS DWORD
      LOCAL My_CreationTime          AS QUAD
      LOCAL My_LastAccessTime        AS QUAD
      LOCAL My_LastWriteTime         AS QUAD
      LOCAL My_FileSizeHigh          AS DWORD
      LOCAL My_FileSizeLow           AS DWORD
      LOCAL My_Reserved0             AS DWORD
      LOCAL My_Reserved1             AS DWORD
      LOCAL My_FileName              AS WSTRINGZ * 260
      LOCAL My_ShortName             AS WSTRINGZ * 14
    
    
      FILE_STRING = DIR$(FILE_NAME TO FILE_INFORMATION)
    
    
      My_FileAttributes  = FILE_INFORMATION.FileAttributes
      My_CreationTime    = FILE_INFORMATION.CreationTime
      My_LastAccessTime  = FILE_INFORMATION.LastAccessTime
      My_LastWriteTime   = FILE_INFORMATION.LastWriteTime
      My_FileSizeHigh    = FILE_INFORMATION.FileSizeHigh
      My_FileSizeLow     = FILE_INFORMATION.FileSizeLow
      My_Reserved0       = FILE_INFORMATION.Reserved0
      My_Reserved1       = FILE_INFORMATION.Reserved1
      My_FileName        = FILE_INFORMATION.FileName
      My_ShortName       = FILE_INFORMATION.ShortName
    
    
    
      MyTime.FileTime     = FILE_INFORMATION.CreationTime
      MyTime.ToLocalTime
      Time_String_1 = MyTime.DateString + " - " + MyTime.TimeStringFull
    
      MyTime.FileTime     = FILE_INFORMATION.LastAccessTime
      MyTime.ToLocalTime
      Time_String_2 = MyTime.DateString + " - " + MyTime.TimeStringFull
    
      MyTime.FileTime     = FILE_INFORMATION.LastWriteTime
      MyTime.ToLocalTime
      Time_String_3 = MyTime.DateString + " - " + MyTime.TimeStringFull
    
    
    
      LOCAL FILE_NAME_NO_PATH AS STRING
            FILE_NAME_NO_PATH = MID$(FILE_NAME, INSTR(-1, FILE_NAME, "\")+1)
    
    
    
       FUNCTION =  $DQ + My_FileName        +  ";" + _
                   $DQ + FILE_NAME_NO_PATH  +  ";" + _
                   $DQ + Time_String_1      +  ";" + _
                   $DQ + Time_String_2      +  ";" + _
                   $DQ + Time_String_3      +  ";" + _
                   $DQ + STR$(My_FileSizeHigh)
    
    
    
    
     END FUNCTION


    The entire thing:

    Code:
    #COMPILE EXE  'pbwin10.03
    #DIM ALL
    
    #INCLUDE "win32api.inc"
    
    
    TYPE DIRDATA
      FileAttributes AS DWORD
      CreationTime   AS QUAD
      LastAccessTime AS QUAD
      LastWriteTime  AS QUAD
      FileSizeHigh   AS DWORD
      FileSizeLow    AS DWORD
      Reserved0      AS DWORD
      Reserved1      AS DWORD
      FileName       AS WSTRINGZ * 260
      ShortName      AS WSTRINGZ *  14
    END TYPE
    
    GLOBAL GL_hDlg AS DWORD
    GLOBAL gFiles()    AS STRING  ' array for found files
    GLOBAL gFileCount  AS LONG    ' counter for found files
    
    %CM_POST_INITDIALOG   =  %WM_USER + 1
    %BUTTON_1             =  %WM_USER + 2
    %LABEL_1              =  %WM_USER + 3
    
    '------------------------------------------------------------------------------
     FUNCTION PBMAIN () AS LONG
    
        OPEN "moo.txt" FOR OUTPUT AS 99
        MAIN_DIALOG
        CLOSE #99
    
     END FUNCTION
    '------------------------------------------------------------------------------
     FUNCTION MAIN_DIALOG() AS LONG
    
       DIALOG NEW PIXELS, %HWND_DESKTOP, "Basic Template", 0, 0, 500, 300, %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR _
             %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_CENTER OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
             %WS_EX_RIGHTSCROLLBAR, TO GL_hDlg
    
       CONTROL ADD BUTTON, GL_hDlg, %BUTTON_1, "Run",  10, 10, 60, 20
       CONTROL ADD LABEL,  GL_hDlg, %LABEL_1 , ""   , 10, 50, 350, 20
       CONTROL SET COLOR   GL_hDlg, %LABEL_1 , %BLACK, %WHITE
    
       DIALOG SHOW MODAL   GL_hDlg, CALL MAIN_DIALOG_CALLBACK
    
     END FUNCTION
    '------------------------------------------------------------------------------
     CALLBACK FUNCTION MAIN_DIALOG_CALLBACK
    
        SELECT CASE AS LONG CB.MSG
    
            CASE %WM_INITDIALOG
              DIALOG POST GL_hDlg, %CM_POST_INITDIALOG, 0, 0
    
            CASE %CM_POST_INITDIALOG
    
    
            CASE %WM_COMMAND
    
              SELECT CASE AS LONG CB.CTL
    
                  CASE %BUTTON_1
                     BUILD_ALL_FILES_LIST
    
              END SELECT
    
        END SELECT
    
     END FUNCTION
    '-------------------------------------------------------------------------------
     FUNCTION MORE_INFO(FILE_NAME AS STRING) AS STRING
    
    
      LOCAL FILE_INFORMATION    AS DIRDATA
      LOCAL FILE_STRING         AS STRING
      LOCAL Time_String_1       AS STRING
      LOCAL Time_String_2       AS STRING
      LOCAL Time_String_3       AS STRING
    
      LOCAL MyTime AS IPOWERTIME
      LET MyTime = CLASS "PowerTime"
    
      LOCAL My_FileAttributes        AS DWORD
      LOCAL My_CreationTime          AS QUAD
      LOCAL My_LastAccessTime        AS QUAD
      LOCAL My_LastWriteTime         AS QUAD
      LOCAL My_FileSizeHigh          AS DWORD
      LOCAL My_FileSizeLow           AS DWORD
      LOCAL My_Reserved0             AS DWORD
      LOCAL My_Reserved1             AS DWORD
      LOCAL My_FileName              AS WSTRINGZ * 260
      LOCAL My_ShortName             AS WSTRINGZ * 14
    
    
      FILE_STRING = DIR$(FILE_NAME TO FILE_INFORMATION)
    
    
      My_FileAttributes  = FILE_INFORMATION.FileAttributes
      My_CreationTime    = FILE_INFORMATION.CreationTime
      My_LastAccessTime  = FILE_INFORMATION.LastAccessTime
      My_LastWriteTime   = FILE_INFORMATION.LastWriteTime
      My_FileSizeHigh    = FILE_INFORMATION.FileSizeHigh
      My_FileSizeLow     = FILE_INFORMATION.FileSizeLow
      My_Reserved0       = FILE_INFORMATION.Reserved0
      My_Reserved1       = FILE_INFORMATION.Reserved1
      My_FileName        = FILE_INFORMATION.FileName
      My_ShortName       = FILE_INFORMATION.ShortName
    
    
    
      MyTime.FileTime     = FILE_INFORMATION.CreationTime
      MyTime.ToLocalTime
      Time_String_1 = MyTime.DateString + " - " + MyTime.TimeStringFull
    
      MyTime.FileTime     = FILE_INFORMATION.LastAccessTime
      MyTime.ToLocalTime
      Time_String_2 = MyTime.DateString + " - " + MyTime.TimeStringFull
    
      MyTime.FileTime     = FILE_INFORMATION.LastWriteTime
      MyTime.ToLocalTime
      Time_String_3 = MyTime.DateString + " - " + MyTime.TimeStringFull
    
    
    
      LOCAL FILE_NAME_NO_PATH AS STRING
            FILE_NAME_NO_PATH = MID$(FILE_NAME, INSTR(-1, FILE_NAME, "\")+1)
    
    
    
       FUNCTION =  $DQ + My_FileName        +  ";" + _
                   $DQ + FILE_NAME_NO_PATH  +  ";" + _
                   $DQ + Time_String_1      +  ";" + _
                   $DQ + Time_String_2      +  ";" + _
                   $DQ + Time_String_3      +  ";" + _
                   $DQ + STR$(My_FileSizeHigh)
    
    
    
    
     END FUNCTION
    
    '------------------------------------------------------------------------------
     FUNCTION BUILD_ALL_FILES_LIST() AS LONG
    
      LOCAL THREAD_HANDLE     AS LONG
      LOCAL THREAD_RESULT     AS LONG
    
      THREAD CREATE THREADED_BUILD_ALL_FILES_LIST (0) TO THREAD_HANDLE
      THREAD CLOSE  THREAD_HANDLE TO THREAD_RESULT
    
    END FUNCTION
    '------------------------------------------------------------------------------
    THREAD FUNCTION THREADED_BUILD_ALL_FILES_LIST(BYVAL NO_VALUE AS LONG)AS LONG
    
    LOCAL i AS LONG
    LOCAL lcounter AS LONG
    
      LOCAL start_timer AS LONG
      start_timer = TIMER
    
    
      Read_Directories "z:\", "jpg"
    
       MSGBOX "Read_Directories Done  " + STR$(TIMER - start_timer)
    
    
       OPEN "Data.txt" FOR OUTPUT AS #1
        FOR i= 0 TO gFilecount - 1
        SLEEP 0
    
        ' IF INSTR(gFiles(i), "c:\Program Files") <> 1 THEN
        ' IF INSTR(gFiles(i), "c:\Users\David\Music\iTunes\")     <> 1 THEN
        ' IF INSTR(gFiles(i), "c:\Windows")       <> 1 THEN
        ' IF INSTR(gFiles(i), "c:\Users\David\Music")       <> 1 THEN
    
          'PRINT #1, gFiles(i)
          PRINT #1, MORE_INFO(gFiles(i))
          INCR lcounter
    
          CONTROL SET TEXT GL_hDlg, %LABEL_1, STR$(lcounter)
          CONTROL REDRAW   GL_hDlg, %LABEL_1
           'SLEEP 1
    
        ' END IF
        ' END IF
        ' END IF
        ' END IF
    
        NEXT i
       CLOSE #1
    
       MSGBOX "File Out Part Done"
    
    END FUNCTION
    '------------------------------------------------------------------------------
    ' Recursive file search
    '------------------------------------------------------------------------------
    SUB Read_Directories(BYVAL PATH AS STRING, FILE_EXTENTION AS STRING)
        LOCAL hSearch AS DWORD               ' Search handle
        LOCAL tmpSize AS QUAD                ' QUAD, in case of huge files...
        LOCAL WFD     AS WIN32_FIND_DATA     ' FindFirstFile structure
        LOCAL curpath AS ASCIIZ * %MAX_PATH  ' What to search for
        LOCAL dirpath AS ASCIIZ * %MAX_PATH  ' Directory name
    
        curpath = PATH & "*." +FILE_EXTENTION                      ' This is what we want to find <-- change if needed
        hSearch = FindFirstFile(curpath, WFD)       ' Get search handle, if success
    
        IF hSearch <> %INVALID_HANDLE_VALUE THEN    ' Loop through directory for files
            DO
                IF (WFD.dwFileAttributes AND %FILE_ATTRIBUTE_DIRECTORY) _  ' If not directory bit is set
                    <> %FILE_ATTRIBUTE_DIRECTORY THEN
                    IF gFileCount MOD 200 = 0 THEN
                        REDIM PRESERVE gFiles(gFileCount + 200) AS GLOBAL STRING
                    END IF
                    'gFiles(gFileCount) = PATH & WFD.cFileName
                    INCR gFileCount
                    PRINT #99,  $DQ + PATH & WFD.cFileName + $DQ + ";" + $DQ + WFD.cFileName + $DQ + MORE_INFO(gFiles(gFileCount))
    
                    CONTROL SET TEXT GL_hDlg, %LABEL_1, STR$(gFileCount)
                    CONTROL REDRAW   GL_hDlg, %LABEL_1
    
                END IF
            LOOP WHILE FindNextFile(hSearch, WFD)
            CALL FindClose(hSearch)
    
    
    
        END IF
    
        curpath = PATH & "*"
        hSearch = FindFirstFile(curpath, WFD)
    
        IF hSearch <> %INVALID_HANDLE_VALUE THEN
            DO
                IF (WFD.dwFileAttributes AND %FILE_ATTRIBUTE_DIRECTORY) = %FILE_ATTRIBUTE_DIRECTORY _
                    AND (WFD.dwFileAttributes AND %FILE_ATTRIBUTE_HIDDEN) = 0 THEN  ' If dirs, but not hidden..
    
                    IF WFD.cFileName <> "." AND WFD.cFileName <> ".." THEN          ' Not these..
                    SLEEP 0
                        '------------------------------------------------------------------
                        ' Now we have a subdir, call function again to find eventual files
                        ' (Each recursive call gets its own place on the stack)
                        '------------------------------------------------------------------
                        CALL Read_Directories(PATH & RTRIM$(WFD.cFileName, CHR$(0)) & "\", FILE_EXTENTION)
                    END IF
                END IF
            LOOP WHILE FindNextFile(hSearch, WFD)
    
            CALL FindClose(hSearch)
    
           'control set text GL_hDlg, %LABEL_1, str$(gFileCount)
           'CONTROL REDRAW   GL_hDlg, %LABEL_1
    
    
        END IF
    
    END SUB
    
    '------------------------------------------------------------------------------

  • #2
    Hi David!

    I've previously posted an app called gbDuplicateFileFinder that might be helpful.

    Comment


    • #3
      Also, here's a compact example of using a function I've posted, that gets a list of all files and folders, placing the information in DIRDATA arrays.

      With that array in hand, you can act on whatever extension(s) on which you want to take action, sich as copying only a list of the JPG's to a file for later action.

      Code:
      'Compilable Example:
      #Compile Exe
      #Dim All
      %Unicode = 1
      #Include "Win32API.inc"
      
      Enum Equates Singular
         IDC_Button = 500
         IDC_TextBox
      End Enum
      
      Global hDlg,hIcon As Dword, ParentFolder$
      Global Files(), Folders() As DirData
      Global FileCount, FolderCount As Long
      
      Function PBMain() As Long
         Dialog Default Font "Tahoma", 12, 1
         Dialog New Pixels, 0, "gbGetFilesAndFolders",300,300,400,100, %WS_OverlappedWindow To hDlg
         Control Add Button, hDlg, %IDC_Button,"Create File List", 20,10,150,25
         Control Add TextBox, hDlg, %IDC_TextBox,"c:\data", 10,50,380,25
         Dialog Show Modal hDlg Call DlgProc
      End Function
      
      CallBack Function DlgProc() As Long
         Select Case Cb.Msg
            Case %WM_InitDialog
               hIcon = LoadIcon(ByVal %Null, ByVal %IDI_Information)  'use a system icon for the dialog
               SendMessage hDlg, %WM_SetIcon, %ICON_BIG, hIcon        'use a system icon for the dialog
            Case %WM_Command
               Select Case Cb.Ctl
                  Case %IDC_Button
                     FileCount = 0 : FolderCount = 0
                     Control Get Text hDlg, %IDC_TextBox To ParentFolder$
                     If IsFolder(ParentFolder$) Then GetFilesAndFolders
                     ? "FolderCount: " + Str$(FolderCount) + $CrLf + "File Count: " + Str$(FileCount)
               End Select
         End Select
      End Function
      
      Sub GetFilesAndFolders
         Local iPos As Long, tempDIR As DirData, temp$
         FolderCount = 0 : FileCount = 0
         ReDim Folders(250000), Files(750000)
         Folders(iPos).FileName = ParentFolder$     'no ending \
         Do While Len(Folders(iPos).FileName)
            temp$ = Dir$(Folders(iPos).FileName + "\*.*", %Normal + %SubDir, To tempDir)
            Do While Len(temp$)
               tempDir.FileName = Folders(iPos).FileName + "\" + tempDir.FileName  'add full path to filename
               If (tempDir.FileAttributes And %File_Attribute_Directory) = 0 Then  'files
                  Incr FileCount
                  Files(FileCount) = tempDir
               Else                                                                'folder
                  Incr FolderCount
                  Folders(FolderCount) = tempDir
               End If
               temp$ = Dir$(Next, To tempDir)
            Loop
            Incr iPos
         Loop
         ReDim Preserve Files(FileCount)
         ReDim Preserve Folders(FolderCount)
      End Sub

      Comment


      • #4
        Function MORE_INFO is okay for me. Made a small adjustment in callback to isolate it -
        Code:
            ...
                      case %BUTTON_1
                       '  BUILD_ALL_FILES_LIST
                       ? More_Info("ST_File.txt") 'added for test. (a known file my PC)
                  end select
            ...
        Since it only takes a file name is your system clock set wrong? (maybe like UTC set to local time and the display set to show UTC so you don't notice the difference on Windows time display or File Explorer file times.)

        (maybe I'll do a quick test program for that)

        Cheers,
        Dale

        Comment


        • #5
          This is giving me back screwy dates and times
          Two problems,
          1. you commented the line of code that populates the global array with full path file names, so all calls to MORE_INFO look for an empty FILE_NAME
          2. you call the MORE_INFO function, after you increase the array index gFileCount, basically feeding it an empty string

          Code:
           'gFiles(gFileCount) = PATH & WFD.cFileName
          INCR gFileCount
          PRINT #99,  $DQ + PATH & WFD.cFileName + $DQ + ";" + $DQ + WFD.cFileName + $DQ + MORE_INFO(gFiles(gFileCount))
          MORE_INFO does give propers dates and times if you DO give it a valid FILE_NAME

          Also I think your function should return

          Code:
            FUNCTION =  $DQ + FILE_NAME          + $DQ +  ";" + _           'FILE_NAME fed to function (full Path)
                         $DQ + My_FileName        + $DQ +  ";" + _           'Returned in DIRDATA
                         $DQ + Time_String_1      + $DQ +  ";" + _
                         $DQ + Time_String_2      + $DQ +  ";" + _
                         $DQ + Time_String_3      + $DQ +  ";" + _
                         $DQ + STR$((My_FileSizeHigh * &H100000000) +  My_FileSizeLow) + $DQ  
          ' FileSize = (FileSizeHigh * &H100000000) + FileSizeLow

          Comment


          • #6
            I'd suggest using MD5 or SHA1 hashing as a comparator. If a file name was changed, or the timestamp is different for some reason you will miss some matches.,
            <b>George W. Bleck</b>
            <img src='http://www.blecktech.com/myemail.gif'>

            Comment


            • #7

              Dave, here is a fast Hash check test of the jpg files as suggested by George

              So you would need to filter off each of the enumerated jpg files through the FNVHash() function and this
              will tell you whether you have had the same file or a different file.


              Code:
              ' FNV file Hash Test
              
              #COMPILE EXE
              #DIM ALL
              
              
              '============================
              FUNCTION PBMAIN () AS LONG
                  LOCAL FnvFilehashsum  AS DWORD
                  LOCAL MyFileBst AS STRING
                  LOCAL ffnam AS STRING
              
                   ' Given filename
                     ffnam ="books.jpg"
              
                 '   Calculate the FNV hash sum of the given file
                     MyFileBst = ReadFileStr(ffnam)
                     IF LEN(MyFileBst) > 0 THEN
                        FnvFilehashsum = FNVHash(BYVAL STRPTR(MyFileBst), BYVAL LEN(MyFileBst), 0)
                     END IF
              
              
                      ?  " File Name : " + ffnam + $CR + $CR + _
                         " Fnv file hash sum : " + STR$(FnvFilehashsum)
              
              END FUNCTION
              
              
              
              '===================================
              FUNCTION ReadFileStr(gfnam AS STRING) AS STRING
                ' reads in the entire file as a  string
                ' for FNV to hash
                LOCAL ff AS LONG
                LOCAL MyFilBst AS STRING
              
                  IF ISFALSE ISFILE(gfnam) THEN
               '      File not found error
                      FUNCTION = ""
                      EXIT FUNCTION
                 END IF
              
              
                 ff = FREEFILE
                 OPEN gfnam FOR BINARY AS #ff
                 GET$ #ff, LOF(ff), MyFilBst
                 CLOSE #ff
                FUNCTION = MyFilBst
              END  FUNCTION
              
              
              
              
              '============================================
              ' FNV hashing routine
              FUNCTION FNVHash(BYVAL dwOffset AS DWORD, BYVAL dwLen AS DWORD, _
                    BYVAL offset_basis AS DWORD) AS DWORD
              #REGISTER NONE
              ! mov esi, dwOffset
              ! mov ecx, dwLen
              ! mov eax, offset_basis
              ! mov edi, &h01000193
              ! xor ebx, ebx
              nextbyte:
              ! mul edi
              ! mov bl, [esi]
              ! xor eax, ebx
              ! inc esi
              ! dec ecx
              ! jnz nextbyte
              ! mov FUNCTION, eax
              END FUNCTION

              Comment


              • #8
                Thanks all! I will give each answer a good look.

                Comment

                Working...
                X