컴퓨터 엑셀 워드 포토샵 구글어스 WINDOWS JAVASCRIPT JAVA C++

 
Tuesday, January 23, 2007

C언어 VC] 도스 DIR 명령 구현; 파일 목록(리스트) 출력; 와일드 카드(Wild Card) 지원


도스(DOS)의 dir 명령처럼, 파일 목록을 구하는 소스입니다. 진짜 dir 명령 같은 복잡한 기능은 전혀 없고, 최소한의 기초적인 것만 있습니다.

파일 이름과 날짜와 속성만 나옵니다.

DIR (ls) 명령, FILE LIST 출력 예제; DIR Command Example Source Code


소스 파일명: 0.cpp
#include <stdio.h>
#include <stdlib.h> // exit()
#include <io.h>
#include <errno.h>
#include <time.h>

char* attribToString(unsigned attrib);
char* timeToString(struct tm *t);


int main(void) {
  _finddatai64_t c_file;
  intptr_t hFile;
  struct tm *t;
  char path[] = "*.*";


  if ( (hFile = _findfirsti64(path, &c_file)) == -1L ) {
    switch (errno) {
      case ENOENT:
        printf(":: 파일이 없음 ::\n"); break;
      case EINVAL:
        fprintf(stderr, "Invalid path name.\n"); exit(1); break;
      case ENOMEM:
        fprintf(stderr, "Not enough memory or file name too long.\n"); exit(1); break;
      default:
        fprintf(stderr, "Unknown error.\n"); exit(1); break;
    }
  } // end if
  else {
    printf("-- 파일 목록 --\n");
    do {
      t = localtime(&c_file.time_write);

      printf("%8I64d  %s %s %s\n",
        c_file.size,
        timeToString(t),
        attribToString(c_file.attrib),
        c_file.name);

    } while (_findnexti64(hFile, &c_file) == 0);
  } // end else
  _findclose(hFile);


  return 0;
}




char* attribToString(unsigned attrib) {
  static char s[4 + 1];

  (attrib & _A_ARCH  ) ? s[0] = 'A' : s[0] = ' ';
  (attrib & _A_RDONLY) ? s[1] = 'R' : s[1] = ' ';
  (attrib & _A_HIDDEN) ? s[2] = 'H' : s[2] = ' ';
  (attrib & _A_SYSTEM) ? s[3] = 'S' : s[3] = ' ';

  return s;
}


char* timeToString(struct tm *t) {
  static char s[20];

  sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
              t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
              t->tm_hour, t->tm_min, t->tm_sec
          );

  return s;
}


디렉토리(폴더)는 별도로 처리해 주어야 하는데, 디렉토리 처리 부분은 현재 없습니다. 디렉토리도 그냥 일반 파일처럼 보여줍니다.

"*.*" 라는 와일드 카드는 "모든 파일"이라는 뜻입니다.

파일 날짜의 연도는 4자리로, 시간은 초 단위까지 자세히 나옵니다.


컴파일 및 실행 결과 화면:
D:\Z>cl /nologo 0.cpp && 0.exe
0.cpp
-- 파일 목록 --
       0  2007-01-23 14:44:34      .
       0  2007-01-23 14:44:34      ..
      15  2007-01-03 12:49:36 A    0.bat
    1667  2007-01-23 14:44:34 A    0.cpp
   45056  2007-01-23 14:48:15 A    0.exe
      50  2007-01-18 15:31:15 A    0.html
    3735  2007-01-23 14:48:15 A    0.obj
     112  2006-12-16 18:01:10 A    0.php
      46  2007-01-20 11:53:19 A    0.pl
      48  2007-01-20 14:05:58 A    0.py
     151  2007-01-11 12:43:09      0.txt
   45056  2007-01-22 14:54:12 A    00.exe
    2985  2007-01-22 14:54:12 A    00.obj
   45056  2006-12-25 11:06:48 A    1.exe
    3721  2006-12-25 11:06:48 A    1.obj
 2003696  2001-10-27 15:47:34  R   Beatles, The - All My Loving.mp3
    1705  2007-01-22 17:18:22 A    Foo.class
    1864  2007-01-22 17:28:18 A    Foo.java
     941  2007-01-23 09:24:02 A    Foo2.class
       0  2007-01-18 11:06:52      out.txt
      46  2007-01-18 10:51:45 A    test.html
       0  2007-01-13 11:27:49      test.txt
       0  2007-01-17 12:19:19 A    tt
     350  2007-01-17 11:49:17 A    ttt
   45056  2006-12-23 16:12:26 A    x.exe
     515  2006-12-28 16:16:29 A    x.html
    1195  2006-12-23 16:12:26 A    x.obj
     171  2007-01-21 12:03:08 A H  문서.rtf
       0  2006-12-26 11:48:17      새 폴더

D:\Z>



위의 소스에 대한 설명은 여기에 있습니다:

▶▶ C언어] DIR 함수; 디렉토리 파일 검색; 와일드카드 지원; findfirst findnext

▶▶ C언어-VC] 파일 속성(읽기전용/히든 속성 등) 출력; Print File Attributes: ARHS

▶▶ C언어] 파일 타임 스탬프, 파일 날짜 시간 구하기 함수; File Time Stamp, Last Write Date




tag: cpp
C언어 | C/C++ (Visual C++)

0 Comments:

Post a Comment

<< Home RSS 2.0 feed

구글 Google 에서 제공하는 무료 블로그 서비스인 블로거 Blogger 의 인터넷 주소는 www.blogger.com 입니다. Blogger 에 블로그를 만들면, blogspot.com 이라는 주소에 블로그가 생성됩니다.
블로그를 직접 방문하지 않고도 최신 게시물을 구독하려면 RSS 2.0 feed 주소를 리더기에 등록하시면 됩니다.
Previous Posts
Monthly Archives
Top