Home › Forums › C Programming › List all files in a Directory
- This topic has 0 replies, 1 voice, and was last updated 19 years, 10 months ago by will.
Viewing 0 reply threads
- AuthorPosts
- February 25, 2005 at 9:26 am #1885willParticipant
Here is a code posted by a user.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134<br />/*********************File Lister 1.0********************<br />This lists all the files in the directory specified with in the command line and does so with the current working directory as default.It takes command line arguments that specify the path and type of files to be listed.The path has to be a DOS path and may or may not include DOS wild cards such as * or ?.The second command line argument may be l,h,s,d,a or r to include volume label, hidden files, system files directories archive files or read only files respectively in the list.But it does not specify an the attribute of the listed files.This can quite be something which tha public domain can look into.The directories are listed as files.Please work on this program.<br />This compiled under Turbo C++ 1.01 Please do send me the improved versions.<br />Deepak,Computer Engg Student,Kerala,India.<br />[email protected]<br />********************************************************/<br />#include<stdio.h><br />#include<conio.h><br />#include<process.h><br /># include <dir.h><br />#include<ctype.h><br /># include <io.h><br />#include<string.h><br /># include <dos.h><br />union f<br />{<br />int a[2];<br />struct ftime b;<br />}c;<br />int getdiskno(char a[])<br />{<br />if(a[1]==':')<br />return(tolower(a[0])-'a'+1);<br />return (getdisk()+1);<br />}<br />long int ret(int a)<br />{<br />struct dfree b;<br />getdfree(a,&b);<br />return (long int)((long int)(b.df_avail)*(long int)(b.df_sclus)*(long int)(b.df_bsec));<br />}<br />int countdig(long int a)<br />{<br />int count=0;<br />if(a==0)return 1;<br />while(a!=0)<br />{<br />a=a/10;<br />count++;<br />}<br />return count;<br />}<br />main(int argc,char *argv[])<br />{<br />int count=0,mode=0,i;<br />long int totsize=0;<br />struct ffblk a;<br />char string[15];<br />if(argc>=2)<br />strcpy(string,argv[1]);<br />else<br />strcpy(string,"*.*");<br />if(argc>=3)<br />{<br />if(strcmp(argv[2],"r")==0)<br />mode=FA_RDONLY;<br />if(strcmp(argv[2],"h")==0)<br />mode=FA_HIDDEN;<br />if(strcmp(argv[2],"d")==0)<br />mode=FA_DIREC;<br />if(strcmp(argv[2],"s")==0)<br />mode=FA_SYSTEM;<br />if(strcmp(argv[2],"a")==0)<br />mode=FA_ARCH;<br />if(strcmp(argv[2],"l")==0)<br />mode=FA_LABEL;<br />}<br />//clrscr();<br />if(findfirst(string,&a,mode)!=0)<br />{<br />printf("nCould not find a file to match your criterionn");<br />exit(0);<br />}<br />count++;<br />printf("nListing files...n");<br />totsize+=a.ff_fsize;<br />printf("n%s",a.ff_name);<br />for(i=0;i<(15-strlen(a.ff_name));i++)<br />printf(" ");<br />printf("%ld",a.ff_fsize);<br />c.a[0]=a.ff_ftime;<br />c.a[1]=a.ff_fdate;<br />for(i=0;i<(10-countdig(a.ff_fsize));i++)<br />printf(" ");<br />if(c.b.ft_day<10)printf("0");<br />printf("%u-",c.b.ft_day);<br />if(c.b.ft_month<10)printf("0");<br />printf("%u-%u",c.b.ft_month,c.b.ft_year+1980);<br />printf(" ");<br />if(c.b.ft_hour<10)printf("0");<br />printf("%u:",c.b.ft_hour);<br />if(c.b.ft_min<10)printf("0");<br />printf("%u:",c.b.ft_min);<br />if(c.b.ft_tsec<10)printf("0");<br />printf("%u",c.b.ft_tsec);<br />while(findnext(&a)==0)<br />{<br />count++;<br />printf("n%s",a.ff_name);<br />totsize+=a.ff_fsize;<br />for(i=0;i<(15-strlen(a.ff_name));i++)<br />printf(" ");<br />printf("%ld",a.ff_fsize);<br />c.a[0]=a.ff_ftime;<br />c.a[1]=a.ff_fdate;<br />for(i=0;i<(10-countdig(a.ff_fsize));i++)<br />printf(" ");<br />if(c.b.ft_day<10)printf("0");<br />printf("%u-",c.b.ft_day);<br />if(c.b.ft_month<10)printf("0");<br />printf("%u-%u",c.b.ft_month,c.b.ft_year+1980);<br />printf(" ");<br />if(c.b.ft_hour<10)printf("0");<br />printf("%u:",c.b.ft_hour);<br />if(c.b.ft_min<10)printf("0");<br />printf("%u:",c.b.ft_min);<br />if(c.b.ft_tsec<10)printf("0");<br />printf("%u",c.b.ft_tsec);<br />if(count%20==0)<br />{<br />if(count!=20)printf("n");<br />printf("nnListed %d files",count);<br />printf("nPress a key for next page....");<br />getch();<br />clrscr();<br />}<br />}<br />printf("nnListed %d filesn%ld bytes in alln",count,totsize);<br />printf("n%ld bytes available in drive %c:",ret(getdiskno(string)),'a'+getdiskno(string)-1);<br />return 0;<br />}<br /><br />
- AuthorPosts
Viewing 0 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.