-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderUtil.cpp
153 lines (118 loc) · 3.75 KB
/
FolderUtil.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "stdafx.h"
#include "FolderUtil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
namespace FolderUtil
{
CString GetFullDirectoryPath( BOOL LastSlash )
{
TCHAR exe_path[MAX_PATH] = { 0 , };
::GetModuleFileName( NULL , exe_path , MAX_PATH );
PathRemoveFileSpec(exe_path);
if ( LastSlash ) PathAddBackslash(exe_path);
return CString( exe_path );
}
void CreateFolder( const CString& FolderPath )
{
SHCreateDirectoryEx( NULL , (LPCTSTR)FolderPath , NULL );
}
BOOL ExistFolder( LPCTSTR FolerPath )
{
return PathFileExists(FolerPath);
}
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED:
{
if ( lpData )
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
break;
}
return 0;
}
CString ShowFolderSelectDialog( HWND Parent , LPCTSTR Title , LPCTSTR FirstFolderPath )
{
BROWSEINFO BrInfo = { 0 , };
TCHAR pszPathname[MAX_PATH] = { 0 , };
BrInfo.hwndOwner = Parent;
BrInfo.pszDisplayName = pszPathname;
BrInfo.lpszTitle = Title;
BrInfo.ulFlags = BIF_RETURNONLYFSDIRS;
BrInfo.lpfn = BrowseCallbackProc;
BrInfo.lParam = (LPARAM)FirstFolderPath;
LPITEMIDLIST pidlBrowse = ::SHBrowseForFolder( &BrInfo );
if ( pidlBrowse )
SHGetPathFromIDList( pidlBrowse , pszPathname );
return pszPathname;
}
int ShowFileSelectDialog( BOOL SingleSelect , LPCTSTR Filter , CStringArray& SelectedFile )
{
CFileDialog dlg( TRUE , NULL , NULL ,
SingleSelect ? OFN_HIDEREADONLY : OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT ,
!Filter ? _T("All Files(*.*)|*.*||") : Filter );
if ( IDOK != dlg.DoModal() ) return 0;
for ( POSITION pos = dlg.GetStartPosition() ; pos != NULL ; )
SelectedFile.Add( dlg.GetNextPathName(pos) );
return (int)SelectedFile.GetCount();
}
int CopyFolderToFolder( CString SrcFolder , CString DestFolder , LPCTSTR Title )
{
if ( SrcFolder.IsEmpty() || DestFolder.IsEmpty() ) return FALSE;
// SHFileOperation 함수에서 파일 및 폴더를 삭제할때는 파일경로 마지막에 널 문자가 2번 존재해야 한다.
SrcFolder.AppendChar( _T('\0') );
DestFolder.AppendChar( _T('\0') );
SHFILEOPSTRUCT shfo = { 0 , };
shfo.hwnd = AfxGetMainWnd()->m_hWnd;
shfo.wFunc = FO_COPY;
shfo.pTo = DestFolder;
shfo.pFrom = SrcFolder;
shfo.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_RENAMEONCOLLISION;
shfo.lpszProgressTitle = Title;
return SHFileOperation( &shfo ); // zero is success
}
void CreateFolder( const CString& FolderPath , BOOL FirstDeleteFolder )
{
if ( FirstDeleteFolder )
DeleteFolder( FolderPath );
if ( ExistFolder( FolderPath ) ) return;
SHCreateDirectoryEx( NULL , (LPCTSTR)FolderPath , NULL );
}
void DeleteFolder( const CString& FolerPath )
{
CFileFind Finder;
BOOL Working = Finder.FindFile( FolerPath + _T("\\*.*") );
while (Working)
{
Working = Finder.FindNextFile();
if ( Finder.IsDots() ) continue;
CString FilePath = Finder.GetFilePath();
if (Finder.IsDirectory()) DeleteFolder(FilePath);
else DeleteFile(FilePath);
}
Finder.Close();
RemoveDirectory(FolerPath);
}
BOOL Search( CString DirPath , const CString& Ext , SearchFunction Func )
{
TCHAR LastCh = DirPath.GetAt( DirPath.GetLength() - 1 );
if ( LastCh != _T('\\') ) DirPath.AppendChar( _T('\\') );
if ( Ext.IsEmpty() ) DirPath += _T("*.*");
else DirPath += _T("*.") + Ext;
WIN32_FIND_DATA Finder = { 0 , };
HANDLE hFind = FindFirstFile( DirPath , &Finder );
DWORD LastError = GetLastError();
if ( hFind == INVALID_HANDLE_VALUE ) return FALSE;
do
{
if ( !Func( Finder ) ) break;
} while ( FindNextFile( hFind , &Finder ) );
FindClose(hFind);
return TRUE;
}
};