Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[1878] Re:도스의
임문환 [mhlim] 1464 읽음    2003-01-23 23:56
조잡하지만 아래와같이 하면됩니다.


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

#include <stdio.h>
#include <conio.h>
#include <dir.h>

////////////////////////////////////////////////////////////////////////////
typedef union
{
  unsigned short f_date;
  struct{
   unsigned short day   : 5 ;
   unsigned short month : 4 ;
   unsigned short year  : 7 ;
  };
}TFDate;

typedef union
{
  unsigned short f_time;
  struct{
   unsigned short seconds  : 5 ;
   unsigned short minute  : 6 ;
   unsigned short hour    : 5 ;
  };
}TFTime;

void dirs(char *path, char *filter, int attr, bool subdirs);
void PrintFileInfo(ffblk fb);
////////////////////////////////////////////////////////////////////////////

//FindFiles
//Usage:  ExeName  [/s]  [path]  [filter] 
int main(int argc, char* argv[])
{
  int attr=FA_RDONLY | FA_HIDDEN  | FA_SYSTEM | FA_ARCH | FA_DIREC;
  char path[MAXPATH];
  char *filter;
  bool subdirs=false;
  int off=0;

  if(argc>=2){
   if(argv[1][0]=='/'){
    off=1;
    //if(strstr(strupr(argv[1]),"/S")) subdirs=true;
    if(argv[1][1]=='s' || argv[1][1]=='S') subdirs=true;
   }
  }

  if(argc>=2+off){
   strcpy(path,argv[1+off]);
  }else{
   sprintf(path,"%c:\\",'A'+getdisk());
   getcurdir(0,path+3);
  }

  if(path[strlen(path)-1]!='\\') strcat(path,"\\");

  if(argc>=3+off) filter=argv[2+off];
  else filter="*.*";

  printf("\n%s\n",path);

  dirs(path,filter,attr,subdirs);

  while(kbhit()) getch(); getch();
  return 0;
}

////////////////////////////////////////////////////////////////////////////
#define MAX_DIR_COUNT 2048
void dirs(char *path, char *filter, int attr, bool subdirs)
{
if(!path || !filter) return;
char pathnfilter[MAXPATH];
char **dirNames;
int dirCount=0;
int lenPath=strlen(path);
ffblk fb;
sprintf(pathnfilter,"%s%s",path,filter);

if(subdirs) dirNames = new char*[MAX_DIR_COUNT];

bool done = findfirst(pathnfilter,&fb,FA_DIREC);
while(!done){
  // "."은 현재 디렉터리, ".."은 상위 디렉터리
  if((fb.ff_attrib&FA_DIREC) && fb.ff_name[0]!='.'){
   PrintFileInfo(fb);
   if(subdirs){
     //하위 디렉터리는 현재 디렉터리를 먼저 탐색한 다음 탐색할 것임.
     if(dirCount<MAX_DIR_COUNT){
       dirNames[dirCount] = new char[2+strlen(fb.ff_name)+lenPath];
       sprintf(dirNames[dirCount],"%s%s\\",path,fb.ff_name);
       dirCount++;
     }
   }
  }
  done=findnext(&fb);
}
findclose(&fb);

done = findfirst(pathnfilter,&fb,attr);
while(!done){
  if((fb.ff_attrib&FA_DIREC)==0) PrintFileInfo(fb);
  done=findnext(&fb);
}
findclose(&fb);


if(subdirs){
  //하위 디렉터리 탐색
   for(int i=0 ;i<dirCount ;i++){
     dirs(dirNames[i],filter,attr,true);
     delete dirNames[i];
   }
   delete []dirNames;
}
}


////////////////////////////////////////////////////////////////////////////
void PrintFileInfo(ffblk fb)
{
TFDate fd;
TFTime ft;
char attr[7];
attr[0]='\0';

if(fb.ff_attrib&FA_ARCH) strcat(attr,"A");//Archive File
if(fb.ff_attrib&FA_DIREC) strcat(attr,"D");//Directory
if(fb.ff_attrib&FA_HIDDEN) strcat(attr,"H");//Hidden File
if(fb.ff_attrib&FA_LABEL) strcat(attr,"L");//Volumn Label
if(fb.ff_attrib&FA_RDONLY) strcat(attr,"R");//Read Only File
if(fb.ff_attrib&FA_SYSTEM) strcat(attr,"S");//System File

fd.f_date=fb.ff_fdate;
ft.f_time=fb.ff_ftime;
printf("\n%s\t %10i\t %04i/%02i/%02i\t %02i:%02i:%02i\t %s"
    ,fb.ff_name,fb.ff_fsize
    ,fd.year+1980,fd.month,fd.day
    ,ft.hour,ft.minute,ft.seconds*2
    ,attr
);

//아래는 위의 printf와 동일한 역할을 함
/*
printf("\n%s\t %10i\t %04i/%02i/%02i\t %02i:%02i:%02i\t %s"
    ,fb.ff_name,fb.ff_fsize
    ,1980+(fb.ff_fdate>>9),(fb.ff_fdate>>5)&0x000f,fb.ff_fdate&0x001f
    ,fb.ff_ftime>>11,(fb.ff_ftime>>5)&0x003f,(fb.ff_ftime&0x001f)*2
    ,attr
);
*/
}

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



군휘 님이 쓰신 글 :
: : : : : : :
안녕하세요
:
 
:
잘 머르는것이 있어서리...ㅡㅡ
:
 
:
고수님들께 질문 드립니당....^^;
:
 
:
도스의 "DIR/S"같은 기능을 내려면..??
:
 
:
findfirst와 findnext를 쓰면 된다는것까지는 알겠는데,
:
 
:
그 알고리듬짜는게 너무 어렵더라구요...^^;
:
 
:
 
:
소스좀 주시거나, 아니면 알고리듬을 주시면 감사하겠습니다.
:
 
:
P.s. findfirst에서요, attribute에 FA_DIREC을 넣으면 디렉토리만 검색되어야 하는거 아닌가요?
:
 
:
       디렉토리 안에 있는  화일이 다 나오는건 왜 : 그런건지도 좀 알려주세요..^^
:
 
:
 
:
감사합니다.
: :
:

+ -

관련 글 리스트
1874 도스의 "DIR/S"같은 기능을 내려면..?? 군휘 1654 2003/01/23
1878     Re:도스의 임문환 1464 2003/01/23
1875     Re:도스의 조준회 1288 2003/01/23
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.