Deutsch   English   Français   Italiano  
<60029e873eb0172be6fba12f38f855859c1f47fe@i2pn2.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: fir <fir@grunge.pl>
Newsgroups: comp.lang.c
Subject: Re: program to remove duplicates
Date: Sat, 21 Sep 2024 23:13:50 +0200
Organization: i2pn2 (i2pn.org)
Message-ID: <60029e873eb0172be6fba12f38f855859c1f47fe@i2pn2.org>
References: <ecb505e80df00f96c99d813c534177115f3d2b15@i2pn2.org> <4fdc265edcfdf2fea23aa6fa4c1c58cc7cbde376@i2pn2.org> <dadedb8538fff0ea1879112d4c41900e1e005d7c@i2pn2.org> <4def0af4bef141861014c969c081fc909c878906@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 21 Sep 2024 21:13:48 -0000 (UTC)
Injection-Info: i2pn2.org;
	logging-data="2926872"; mail-complaints-to="usenet@i2pn2.org";
	posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
In-Reply-To: <4def0af4bef141861014c969c081fc909c878906@i2pn2.org>
X-Spam-Checker-Version: SpamAssassin 4.0.0
Bytes: 7225
Lines: 219

ok i wrote this duplicates remover but i dont know if it has no errors etc

heres the code you may comment if you some errors, alternatives or 
improvements (note i wrote it among the time i posted on it and that 
moment here so its kinda speedy draft (i reused old routines for loading 
files etc)

#include<windows.h>
#include<stdio.h>

  void StrCopyMaxNBytes(char* dest, char* src, int n)
  {
   for(int i=0; i<n; i++) { dest[i]=src[i]; if(!src[i]) break;  }
  }

  //list of file names
  const int FileNameListEntry_name_max = 500;
  struct FileNameListEntry { char name[FileNameListEntry_name_max]; 
unsigned int file_size; };

  FileNameListEntry* FileNameList = NULL;
  int FileNameList_Size = 0;

  void FileNameList_AddOne(char* name, unsigned int file_size)
  {
      FileNameList_Size++;
      FileNameList = (FileNameListEntry*) realloc(FileNameList, 
FileNameList_Size * sizeof(FileNameListEntry) );
      StrCopyMaxNBytes((char*)&FileNameList[FileNameList_Size-1].name, 
name, FileNameListEntry_name_max);
      FileNameList[FileNameList_Size-1].file_size = file_size;
      return ;
  }


  // collect list of filenames
  WIN32_FIND_DATA ffd;

  void ReadDIrectoryFileNamesToList(char* dir)
  {
   HANDLE h = FindFirstFile(dir, &ffd);

   if(!h) { printf("error reading directory"); exit(-1);}

   do  {
    if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
      FileNameList_AddOne(ffd.cFileName, ffd.nFileSizeLow);
      if(ffd.nFileSizeHigh!=0) { printf("this program only work for 
files up to 4GB"); exit(-1);}
    }
   }
   while (FindNextFile(h, &ffd));

  }

#include <sys/stat.h>

  int GetFileSize2(char *filename)
  {
     struct stat st;
     if (stat(filename, &st)==0) return (int) st.st_size;

     printf("error obtaining file size for %s", filename); exit(-1);
     return -1;
  }

  int FolderExist(char *name)
  {
     static struct stat st;
     if(stat(name, &st) == 0 && S_ISDIR(st.st_mode)) return 1;
     return 0;
  }


//////////

   unsigned char* bytes2 = NULL;
   int bytes2_size = 0;
   int bytes2_allocked = 0;

   unsigned char* bytes2_resize(int size)
    {
    bytes2_size=size;
    if((bytes2_size+100)*2<bytes2_allocked | bytes2_size>bytes2_allocked)
    return  bytes2=(unsigned char*)realloc(bytes2, 
(bytes2_allocked=(bytes2_size+100)*2)*sizeof(unsigned char));
    }

   void bytes2_load(unsigned char* name)
   {
     int flen = GetFileSize2(name);
     FILE *f = fopen(name, "rb");
     if(!f) {  printf( "errot: cannot open file %s for load  ", name); 
exit(-1); }
     int loaded = fread(bytes2_resize(flen), 1, flen, f);
     fclose(f);
    }

/////////////////


   unsigned char* bytes1 = NULL;
   int bytes1_size = 0;
   int bytes1_allocked = 0;

   unsigned char* bytes1_resize(int size)
    {
    bytes1_size=size;
    if((bytes1_size+100)*2<bytes1_allocked | bytes1_size>bytes1_allocked)
    return  bytes1=(unsigned char*)realloc(bytes1, 
(bytes1_allocked=(bytes1_size+100)*2)*sizeof(unsigned char));
    }

   void bytes1_load(unsigned char* name)
   {
     int flen = GetFileSize2(name);
     FILE *f = fopen(name, "rb");
     if(!f) {  printf( "errot: cannot open file %s for load  ", name); 
exit(-1); }
     int loaded = fread(bytes1_resize(flen), 1, flen, f);
     fclose(f);
    }

/////////////



int CompareTwoFilesByContentsAndSayIfEqual(char* file_a, char* file_b)
{
    bytes1_load(file_a);
    bytes2_load(file_b);
    if(bytes1_size!=bytes2_size) { printf("\n something is wrong 
compared files assumed to be be same size"); exit(-1); }

    for(unsigned int i=0; i<=bytes1_size;i++)
      if(bytes1[i]!=bytes2[i]) return 0;

   return 1;

}

#include<direct.h>
#include <dirent.h>
#include <errno.h>

int duplicates_moved = 0;
void MoveDuplicateToSubdirectory(char*name)
{

    if(!FolderExist("duplicates"))
    {
     int n =  _mkdir("duplicates");
     if(n) { printf ("\n i cannot create subfolder"); exit(-1); }
    }

   static char renamed[1000];
   int n = snprintf(renamed, sizeof(renamed), "duplicates\\%s", name);

   if(rename(name, renamed))
    {printf("\n rename %s %s failed", name, renamed); exit(-1);}

   duplicates_moved++;

}

int main()
{
     printf("\n (RE)MOVE FILE DUPLICATES");
     printf("\n ");

     printf("\n this program searches for binaric (comparec byute to 
byte)");
     printf("\n duplicates/multiplicates of files in its own");
     printf("\n folder (no search in subdirectories, just flat)");
     printf("\n and if found it copies it into 'duplicates'");
     printf("\n subfolder it creates  If you want to remove that");
     printf("\n duplicates you may delete the subfolder then,");
     printf("\n if you decided to not remove just move the contents");
========== REMAINDER OF ARTICLE TRUNCATED ==========