Author Topic: Recursive searching files  (Read 674 times)

caballero

  • Member
  • *****
  • Posts: 1211
  • Matrix - Noah
    • abre ojos ensamblador
Recursive searching files
« on: July 29, 2019, 07:10:42 AM »
Code: [Select]
/* ----------------------------------------------------------------------------
   -       TITULO  : Encontrar ficheros con Wildcards recursivamente          -
   -                 en directorios y subdirectorios  W-TinyC                 -
   -----                                                                  -----
   -       AUTOR   : Alfonso Víctor Caballero Hurtado                         -
   -----                                                                  -----
   -       VERSION : 1.0                                                      -
   -----                                                                  -----
   -      (c) 2019. Abre los Ojos al Ensamblador                              -
   ---------------------------------------------------------------------------- */

#include <stdio.h>
#include <io.h>
#include <string.h>

int wildcmp (const char *wild, const char *string) {
  int lw = strlen(wild),     // longitud de wild
      ls = strlen(string),   // longitud de string
      iw = 0, is = 0;        // punteros que los recorrerán
  for (iw = 0; iw < lw; iw++) {                      // Recorremos wild
    if ((wild[iw] != '*') && (wild[iw] != '?')) {    // Si el char es normal
      if (wild[iw] != string[is]) return(0);         // Si no coinciden no son iguales, salimos
      is++;                                          // En otro caso aumentamos el puntero a string
      if (is >= ls) {                                // Si hemos terminado de recorrer string
        if (iw == lw-1) return(1);                   // Si el puntero a wild ya había terminado también salimos 1
        else            return(0);                   //     en otro caso no son iguales, salimos 0
      }
    } else if (wild[iw] == '?') {                    // Si el carácter es '?', éste puede ser cualquiera, pero tiene que existir
      is++;                                          // Incrementamos el puntero a string porque es indiferente el carácter que tuviera
      if (is >= ls) {                                // Si hemos terminado string
        if (iw == lw-1) return(1);                   // Si también hemos terminado wild, terminamos 1
      }
    } else if (wild[iw] == '*') {                    // Si el carácter es '*' pueden ser varios caracteres o ninguno
      if (iw == lw-1) return(1);                     // Si hemos terminado wild, terminamos 1
      iw++;                                          // Incrementamos puntero a wild
      while (is < ls)                                // Seguimos recorriendo string
        if (wild[iw] == string[is++]) break;         // Si coincide wild[iw] == string[is] seguimos comparando. En todo caso incrementamos is
      if (is == ls)
        if (iw == lw-1) return (1);                  // Si hemos terminado, son iguales, salimos 1
        else           return (0);                   //     sino no son iguales, salimos 0
    }
  }
  if (is < ls) return (0);
  return (1);
}

void find_recursive (const char *folder, const char *filename) {
  char wildcard[500], NewPath[500];
  sprintf(wildcard, "%s\\*", folder);

  struct _finddata_t fileinfo;

  int x = _findfirst(wildcard, &fileinfo);
  if(x == 0) return;
  if(strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
    if(fileinfo.attrib & 0x10) {
      sprintf(NewPath, "%s\\%s", folder, fileinfo.name);
      find_recursive(NewPath, filename);
    } else {
      // if(_stricmp(fileinfo.name, filename) == 0)
      if(wildcmp(filename, fileinfo.name))
        printf("%s\\%s\n" ,folder, fileinfo.name);
    }
  }
 
  while (!_findnext(x, &fileinfo)) {
     if(strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)  continue;
     if(fileinfo.attrib & 0x10) {
       sprintf(NewPath, "%s\\%s", folder, fileinfo.name);
       find_recursive(NewPath, filename);
    } else {
      // if(_stricmp(fileinfo.name, filename) == 0)
      if(wildcmp(filename, fileinfo.name))
        printf("%s\\%s\n" ,folder, fileinfo.name);
    }
  } ;
}

int main(void) {
    find_recursive("d:\\Trabajo", "*.c");
    return 0;
}

Hello, I have made a tiny c code for searching recursivelly some files, that accepts wildcards too. May aim is modify loads of files massivelly. The first step is looking for them.

If anyone want to check it, it would be appreciate.

compile: tcc Find.c
using:
  At the bottom of the file you have to put:
  - Your starting folder, in my case: "d:\\Trabajo".
  - The files you want to find, in my case: "*.c"
 
  Personally, I run it with
  Find > Files.txt
 
  That way you will obtain the result in the file "Files.txt"

To compare results, open a cmd window and go to the same folder, then use the dir command to send the list to another file to compare with a text compare tool, like examdiff or winmerge

dir /s /b *.c > Files_dir.txt

 
Thank you
« Last Edit: July 29, 2019, 06:47:21 PM by caballero »
The logic of the error is hidden among the most unexpected lines of the program

Biterider

  • Member
  • ***
  • Posts: 391
  • ObjAsm32 + ObjAsm64 = ObjAsm
    • ObjAsm
Re: Recursive searching files
« Reply #1 on: July 29, 2019, 05:30:09 PM »
Hi
You can use "dir" and redirect the output stream to a file to verify your results.  :icon_idea:
Biterider

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Recursive searching files
« Reply #2 on: July 29, 2019, 06:50:47 PM »
with Win32 API:
for example ScanDirs.exe C:\temp *.zip
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int printf(const char * format, ...);

int ScanFiles(TCHAR *szPath, TCHAR *szMask, int nLen)
{
WIN32_FIND_DATA fdFile;
HANDLE hFile;

if (!(*(szPath + nLen - 1) == '\\' || *(szPath + nLen - 1) == '/'))
*(szPath + nLen++) = '\\';
strcpy(szPath + nLen, szMask); // append mask
hFile = FindFirstFile(szPath, &fdFile);
*(szPath + nLen) = 0; // reset path length
if (hFile != INVALID_HANDLE_VALUE)
do {
if ((fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
!= FILE_ATTRIBUTE_DIRECTORY) {
printf("file %s%s\n", szPath, fdFile.cFileName);
}
} while (FindNextFile(hFile, &fdFile));
FindClose(hFile);
return 0;
}

int ScanDirs(TCHAR *szPath, TCHAR *szMask, int nLen)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind;
int nLen2;

if (*(szPath + nLen - 1) == '\"') nLen--;
if (!(*(szPath + nLen - 1) == '\\' || *(szPath + nLen - 1) == '/'))
*(szPath + nLen++) = '\\';
ScanFiles(szPath, szMask, nLen);
nLen2 = nLen;
// strcpy(szPath + nLen2, szMask); // append mask
strcpy(szPath + nLen2, TEXT("*.*")); // append mask
hFind = FindFirstFile(szPath, &fdFile);
if (hFind != INVALID_HANDLE_VALUE)
do {
if ((fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
== FILE_ATTRIBUTE_DIRECTORY) {
if (*fdFile.cFileName == '.') {
if (!*(fdFile.cFileName + 1))
continue;
if (*(fdFile.cFileName + 1) == '.' && !*(fdFile.cFileName + 2))
continue;
}
strcpy(szPath + nLen2, fdFile.cFileName);
nLen2 += strlen(fdFile.cFileName);
printf("dir  %s\n", szPath);
ScanDirs(szPath, szMask, nLen2);
*(szPath + nLen) = 0; // reset path length
nLen2 = nLen;
}
} while (FindNextFile(hFind, &fdFile));
FindClose(hFind);
return 0;
}

int main(int argc, char **argv)
{
TCHAR szPath[MAX_PATH];
if (argc < 2) {
puts("Usage: ScanDirs.exe path <mask>");
return 1;
}
puts(argv[1]);
strcpy(szPath, argv[1]);
if (argc == 3) ScanDirs(szPath, argv[2], strlen(szPath));
else ScanDirs(szPath, "*.*", strlen(szPath));
return 0;
}
« Last Edit: July 30, 2019, 01:08:30 AM by TimoVJL »
May the source be with you

caballero

  • Member
  • *****
  • Posts: 1211
  • Matrix - Noah
    • abre ojos ensamblador
Re: Recursive searching files
« Reply #3 on: July 29, 2019, 06:59:12 PM »
@Biterider
Yes, I will do it. In a first glamse it seemed to work, though comparing today it seems a few differences (in a first glance). I will look at it closely.

I forgot yesterday include information on how to obtain the list of files given by the system, that I include today. I didn't want a list of your files, just a proof in another system to see it may be the germen of a searching tool. Not probably I will do that, so no need this test :)

Nevermind, it is close enough to what I wanted. Because of some av gives false positive for some executables made with tinyc or asm, I have removed all of them from my new web site, though the links are still alive, they gives a host error when clicked, so I want to deactivete all of them, for doing so I'm making this program to do such a thing for me :)

@TimoVJL
I will see it
The logic of the error is hidden among the most unexpected lines of the program

caballero

  • Member
  • *****
  • Posts: 1211
  • Matrix - Noah
    • abre ojos ensamblador
Re: Recursive searching files
« Reply #4 on: July 30, 2019, 12:51:36 AM »
There were three files that my program didn't find, that's because they end in "C" while the wildcard is lowercase "*.c". It est, at the moment it works ok as case sensitive. It would be rather easy to make it not distinguishes.

That's good enough for me.

Cheers!
The logic of the error is hidden among the most unexpected lines of the program

jj2007

  • Member
  • *****
  • Posts: 9782
  • Assembler is fun ;-)
    • MasmBasic
Re: Recursive searching files
« Reply #5 on: July 30, 2019, 06:10:55 AM »
For comparison - GetFiles a*.asm|a*.rc finds about 33 files ;-)

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  GetFiles \Masm32\examples\a*.asm|a*.rc      ; find them
  Store "Examples.txt", Files$()        ; save them
  ShEx "Examples.txt"           ; show them
EndOfCode

caballero

  • Member
  • *****
  • Posts: 1211
  • Matrix - Noah
    • abre ojos ensamblador
Re: Recursive searching files
« Reply #6 on: July 30, 2019, 06:35:12 AM »
I can't see the code, I guess that yours find recursively in subfolders too, but don't know. Its execution gives nothing, I imagine because I don't have "\masm32\...". Avast told me that this executable is very "unusual" :azn:. I see that this executable is 29kb in size...
The logic of the error is hidden among the most unexpected lines of the program

jj2007

  • Member
  • *****
  • Posts: 9782
  • Assembler is fun ;-)
    • MasmBasic
Re: Recursive searching files
« Reply #7 on: July 30, 2019, 07:29:14 AM »
I don't have "\masm32\..."

Wow, you don't have a Masm32 installation?

HSE

  • Member
  • *****
  • Posts: 1146
  • <AMD>< 7-32>
Re: Recursive searching files
« Reply #8 on: July 30, 2019, 07:39:06 AM »
Wow, ...nor MasmBasic  :joking:

jj2007

  • Member
  • *****
  • Posts: 9782
  • Assembler is fun ;-)
    • MasmBasic
Re: Recursive searching files
« Reply #9 on: July 30, 2019, 07:51:21 AM »
That I can understand, it's a matter of taste, but no Masm32 sdk is somewhat gross :cool:

caballero

  • Member
  • *****
  • Posts: 1211
  • Matrix - Noah
    • abre ojos ensamblador
Re: Recursive searching files
« Reply #10 on: July 30, 2019, 07:56:49 AM »
Well, I have bought recently a new PC and I try not to install anything that be anything different as unzip and go, until I need it, not for the moment.
The logic of the error is hidden among the most unexpected lines of the program

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Recursive searching files
« Reply #11 on: July 30, 2019, 07:58:00 AM »
Actual recursive search code size is below 1k ?

EDIT:
example program size 2.5 / 3.5 kB
« Last Edit: July 30, 2019, 10:04:52 AM by TimoVJL »
May the source be with you

caballero

  • Member
  • *****
  • Posts: 1211
  • Matrix - Noah
    • abre ojos ensamblador
Re: Recursive searching files
« Reply #12 on: July 30, 2019, 08:01:48 AM »
Mine is exactly 2.50kb
The logic of the error is hidden among the most unexpected lines of the program

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Recursive searching files
« Reply #13 on: July 30, 2019, 08:04:46 AM »
Well, I have bought recently a new PC and I try not to install anything that be anything different as unzip and go, until I need it, not for the moment.
Pelles C almost fit that criteria, just copy it to USB stick from an old PC ?
May the source be with you

felipe

  • Member
  • *****
  • Posts: 1249
  • Eagles are just great!
Re: Recursive searching files
« Reply #14 on: July 30, 2019, 08:51:08 AM »
as unzip and go

The masm sdk can work just like that, no problem. It will make no changes on your system. You can install it on the hard drive, zip the whole (main) folder, store it (cut) in a usb drive. Then you can unzip as needed in your hard drive this folder. When you are done with it, you keep the zip folder in the usb and delete the unzipped one in the hard drive.

Masm sdk  (whichever version) is worth all the effort...Casual publicity intended for new members... :mrgreen:
Felipe.