Home › Forums › C Programming › List Files and Attributes
- 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:50 am #1891willParticipant
This code will show you how to list the
File Name
Attributes
File Size
Last modify Dateof all the files in a specified folder.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126<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 />}
- AuthorPosts
Viewing 0 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.