RemoveDirectory() 怎么删除 非空目录啊
发布网友
发布时间:2022-05-02 04:28
我来回答
共3个回答
热心网友
时间:2022-06-28 05:47
RemoveDirectory( )只能删除空目录,你可以用递归的办法先删除目录下的文件,然后再删除目录。如果不进行递归删除。你可以使用API函数SHFileOperation,它可以一次删除目录及其下面的子目录和文件。
热心网友
时间:2022-06-28 05:47
CString StrSourceFolder="C:\\1";
char SourceFolder[MAX_PATH+1]="";
strcpy(SourceFolder,StrSourceFolder);
SHFILEOPSTRUCT lpFile;
lpFile.hwnd=GetSafeHwnd();
lpFile.wFunc=FO_DELETE;
lpFile.pFrom=SourceFolder;
lpFile.pTo=NULL;
lpFile.fFlags=FOF_ALLOWUNDO;
lpFile.fAnyOperationsAborted=FALSE;
lpFile.hNameMappings=NULL;
lpFile.lpszProgressTitle=NULL;
int ReturnValue=SHFileOperation(&lpFile);
if(ReturnValue==0)
{
if(lpFile.fAnyOperationsAborted==TRUE)
MessageBox("删除文件夹的操作被取消","信息提示",MB_OK+MB_ICONWARNING);
else
MessageBox("删除文件夹操作成功","信息提示",MB_OK+MB_ICONWARNING);
}
else
MessageBox("删除文件夹操作失败","信息提示",MB_OK+MB_ICONEXCLAMATION);
热心网友
时间:2022-06-28 05:48
The RemoveDirectory function deletes an existing empty directory.
BOOL RemoveDirectory(
LPCTSTR lpPathName // address of directory to remove
);
Searching for Files and Changing File Attributes
The following example copies all text files in the current directory to a new directory of read-only files named \TEXTRO. Files in the new directory are changed to read only, if necessary.
The application uses the GetCurrentDirectory function to retrieve the current directory path. This function is also used to return to the current directory after changing to the \TEXTRO directory.
The application then creates the \TEXTRO directory by using the CreateDirectory function.
The application searches the current directory for all .TXT files by using the FindFirstFile and FindNextFile functions. Each .TXT file is copied to the \TEXTRO directory. After a file is copied, the GetFileAttributes function determines whether the file is read only. If the file is not read only, the application changes directories to \TEXTRO and converts the copied file to read only by using the SetFileAttributes function.
After all .TXT files in the current directory have been copied, the application closes the search handle by using the FindClose function.
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
ErrorHandler("Couldn't create new directory.");
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
ErrorHandler("No .TXT files found.");
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
ErrorHandler("Couldn't copy file.");
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
ErrorHandler("Couldn't find next file.");
}
}
}
// Close the search handle.
if (!FindClose(hSearch))
{
ErrorHandler("Couldn't close search handle.");
}
Parameters
lpPathName
Points to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
See Also
CreateDirectory
The DeleteFile function deletes an existing file.
BOOL DeleteFile(
LPCTSTR lpFileName // pointer to name of file to delete
);
Parameters
lpFileName
Points to a null-terminated string that specifies the file to be deleted.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
If an application attempts to delete a file that does not exist, the DeleteFile function fails.
Windows 95: The DeleteFile function deletes a file even if it is open for normal I/O or as a memory-mapped file. To prevent loss of data, close files before attempting to delete them.
Windows NT: The DeleteFile function fails if an application attempts to delete a file that is open for normal I/O or as a memory-mapped file.
To close an open file, use the CloseHandle function.
he FindFirstFile function searches a directory for a file whose name matches the specified filename. FindFirstFile examines subdirectory names as well as filenames.
HANDLE FindFirstFile(
LPCTSTR lpFileName, // pointer to name of file to search for
LPWIN32_FIND_DATA lpFindFileData // pointer to returned information
);
Parameters
lpFileName
Windows 95: Points to a null-terminated string that specifies a valid directory or path and filename, which can contain wildcard characters (* and ?). This string must not exceed MAX_PATH characters.
Windows NT: Points to a null-terminated string that specifies a valid directory or path and filename, which can contain wildcard characters (* and ?).
There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the FindFirstFile function parses paths. An application can transcend this limit and send in paths longer than MAX_PATH characters by calling the wide (W) version of FindFirstFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing; it lets paths longer than MAX_PATH be used with FindFirstFileW. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "
\\?\UNC\bill_g_1\hotstuff\coolapps" is seen as "\\bill_g_1\hotstuff\coolapps".
lpFindFileData
Points to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to the FindNextFile or FindClose function to refer to the file or subdirectory.
Return Values
If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
Remarks
The FindFirstFile function opens a search handle and returns information about the first file whose name matches the specified pattern. Once the search handle is established, you can use the FindNextFile function to search for other files that match the same pattern. When the search handle is no longer needed, close it by using the FindClose function.
This function searches for files by name only; it cannot be used for attribute-based searches.
The FindNextFile function continues a file search from a previous call to the FindFirstFile function.
BOOL FindNextFile(
HANDLE hFindFile, // handle to search
LPWIN32_FIND_DATA lpFindFileData // pointer to structure for data on found file
);
Parameters
hFindFile
Identifies a search handle returned by a previous call to the FindFirstFile function.
lpFindFileData
Points to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to FindNextFile to refer to the found file or directory.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.
Remarks
The FindNextFile function searches for files by name only; it cannot be used for attribute-based searches.