Find Directory using C on Linux and Windows

#Find Files in Directory.
///////FIND FIRST///Only Work on WINDOWS (O.S)/////////////
#include <stdio.h>
#include <time.h>
#include<string.h>
#include<sys/io.h>
int main(void)
{
    struct _finddata_t c_file;
    long hFile;
    char path[100]="D:\\sharjeel\\Messages\\"; 
    strcat(path,"/*.wav");
    
        hFile=_findfirst(path ,&c_file);
    /* Find first .wav file in current directory */
    if(hFile== -1L )
       printf( "No *.wav files in current directory!\n" );
   else
   {
            printf( "Listing of .wav files %s\n\n",c_file.name);
            /* Find the rest of the .wav files */
            while( _findnext( hFile, &c_file ) == 0 )
            {
                printf( "Listing of .wav files %s\n\n",c_file.name);
            }

       _findclose( hFile );
   }
}

//////////Below Written Function Will work on LINUX as well as WINDOS//////////////
->Search files from directory by date
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char s[200]="";
    snprintf(s,sizeof(s),"dir *.* /od /home/sharjeel/IvChat/MessageBox/");
    system(s);    
}

->Using OPen Dir, Read Dir & Close DIR functions
CODE :1
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
int main()
{
    DIR             *dip;
        struct dirent   *dit;
     char path[150]="/home/sharjeel/IvChat/MessageBox/";
    char s[100]="";
        int i = 0;

    if ((dip = opendir(path)) == NULL)
        {
                perror("opendir");
                return 0;
        }

        printf("Directory stream is now open\n");
    while ((dit = readdir(dip)) != NULL)
        {
                i++;
        printf("NAME:::%s\n", dit->d_name);
        strcpy(s,dit->d_name);
        printf("NAME IS:::--->%s\n", dit->d_name);
        }
     printf("\n\nreaddir() %sfound a total of %i files\n",s,i);

        /* int closedir(DIR *dir);
         *
         * Close the stream to argv[1]. And check for errors. */
        if (closedir(dip) == -1)
        {
                perror("closedir");
                return 0;
        }

        printf("\nDirectory stream is now closed\n");
        return 1;
    
}

->Using Scan Dir Function
CODE :2

#include <dirent.h>
#include <stdio.h>
main(){
    struct dirent **namelist;
    int n,i;
char s[100=]"";

n = scandir("/home/sharjeel/IvChat/MessageBox/", &namelist, 0, alphasort);
if(n < 0)
    perror("scandir");
else
{
    while(n--){
        printf("%s\n", namelist[n]->d_name);
        free(namelist[n]);
    } free(namelist);
}
for(i=0;i<=strlen(namelist[n]->d_name);i++){
        
}


CODE :3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

char *path_cat(const char *str1, char *str2);

int main () {
    struct dirent *dp;

        // enter existing path to directory below
    const char *dir_path="/home/sharjeel/IvChat/MessageBox/";
    DIR *dir = opendir(dir_path);
    while ((dp=readdir(dir)) != NULL) {
        char *tmp;
        tmp = path_cat(dir_path, dp->d_name);
        printf("%s\n", tmp);
        free(tmp);
        tmp=NULL;
    }
    closedir(dir);
    return 0;
}

char *path_cat (const char *str1, char *str2) {
    size_t str1_len = strlen(str1);
    size_t str2_len = strlen(str2);
    char *result;
    result = malloc((str1_len+str2_len+1)*sizeof *result);
    strcpy (result,str1);
    int i,j;
    for(i=str1_len, j=0; ((i<(str1_len+str2_len)) && (j<str2_len));i++, j++) {
        result[i]=str2[j];
    }
    result[str1_len+str2_len]='\0';
    return result;
}

Comments

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'

Error : Sorry, there was an error : unauthorized_client in Sitecore 9.1 login