목표 : MFC Dialog 환경에서 폴더를 선택하면 해당 폴더내 파일명 앞에 년원일 이 붙도록 한다.


폴더 선택 경로 얻기 -> 해당 경로내 파일 검색 -> 시간 얻기 -> isDot -> 파일명 변경 -> 계속



1. 폴더 경로 얻기.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    ITEMIDLIST *pidBrowse;
 
    char  pszPathname[MAX_PATH];
 
    BROWSEINFO BrInfo;
 
    BrInfo.hwndOwner = GetSafeHwnd();
 
    BrInfo.pidlRoot = NULL;
 
    memset(&BrInfo,0,sizeof(BrInfo));
 
    BrInfo.pszDisplayName = pszPathname;
 
    BrInfo.lpszTitle="selec Folder";
 
    BrInfo.ulFlags = BIF_RETURNONLYFSDIRS;
 
    pidBrowse = SHBrowseForFolder(&BrInfo);
 
    if(pidBrowse != NULL)
 
    {
 
        BOOL bSuccess = ::SHGetPathFromIDList(pidBrowse,pszPathname);
 
        if(bSuccess)
 
        {
 
            m_strPath = pszPathname;
 
            UpdateData(FALSE);
 
        }
 
        else{
 
            MessageBox("wrong Folder naem!","lol",MB_ICONASTERISK);
 
        }
 
    }
 
 
cs


2. 시간 얻기



3. 경로내 파일 검색


 

1
2
3
4
5
6
7
8
    CFileFind finder;
 
    bool working = finder.FindFile(m_strPath+"\\*.*");
 
    while(working)
    {
        working = finder.FindNextFile();
    }
cs



4. IsDots()

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


1
2
3
4
5
6
7
8
9
10
    if(finder.IsDots())    continue;
 
    ex)CFileFind finder;
 
    bool working = finder.FindFile(m_strPath+"\\*.*");
    while(working)
    {
        if(finder.IsDots())    continue;
        working = finder.FindNextFile();
    }
cs



5. 파일명 변경

CFile::Rename(strFindname,strTemp);


첫번째 인자는 전체 경로를 포함한 이전 파일명, 두번째 인자는 전체 경로를 포함한 새로 바뀔 파일명




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

단계 :

폴더 경로 얻기 -> 폴더 경로 내의 파일 검색 -> 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("기존 파일명 (경로포함)" , "바뀔 파일명(경로포함);














+ Recent posts