목표 : 작업폴더내 모든 파일명 앞에 현재 날짜 년 월 일  이 붙도록 하는 콘솔 프로그램

단계 :

폴더 경로 얻기 -> 폴더 경로 내의 파일 검색 -> isDot  - no -> 파일명 수정 -> next

  - yes-> next



1. 폴더 (Directory) 경로 얻기


함수 : GetCurrentDirectory



2. 폴더내 파일 검색


struct _finddata_t cfile;

long hFile;



if( (hFile = _findfirst( "*.*", &c_file )) == -1L )

printf( "No files in current directory!\n" );

else

{

/* Find the rest of the .c files */

do

{

  // 작업

     }while( _findnext( hFile, &c_file ) == 0 );


_findclose( hFile );

}









3. isDot ?


콘솔로 폴더를 검색하면 상위 폴더를 보는 . 과 .. 이 있기때문에 이를 걸러주어야함.

if(!strcmp(".", c_file.name)) _findnext( hFile, &c_file );

if(!strcmp("..", c_file.name)) _findnext( hFile, &c_file );




4. 시스템 시간 얻기

SYSTEMTIME 을 이용하는 방법, time_t를 이용하는 방법이 있는데, 

time_t는 뭔가 부정확한 느낌이다.





5. 파일명 바꾸기.


rename 함수를 이용한다

rename("기존 파일명 (경로포함)" , "바뀔 파일명(경로포함);














C++ 현재시간을 구한다. 

1. SYSTEMTIME 을 이용
CString GetToDay(CString data)
{
    CString strToday;
    SYSTEMTIME    time;    // 시간 구조체.
    ::ZeroMemory(reinterpret_cast<void*>(&time), sizeof(time));     // time 초기화.
    ::GetLocalTime(&time);    // 현재시간을 얻음.
    strToday.Format (_T("%4d%02d%02d%02d%02d"), time.wYear ,time.wMonth ,time.wDay, time.wHour, time.wMinute); // 형식에 맞게 받음..
    return strToday;
}
2. time_t 를 이용
char sysDate[MAX_PATH2] ="";

struct tm* today;

time_t ltime;

time(<ime);

today = localtime(<ime);

sprintf(sysDate, "%04d년%02d월%02d일_",today->tm_yday+1778, today->tm_mon+1, today->tm_mday);


3. CTime 활용


CString strToday = CTime::GetcurrentTime();


+ Recent posts