0
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "win32-dirent.h"
#include <windows.h>
#include <io.h>
#include <direct.h>
#define MAXFILEPATH 1024
bool IsDirectory(char* path)
{
WIN32_FIND_DATA w32fd;
HANDLE hFindFile;
hFindFile = FindFirstFile((PTCHAR)path, &w32fd);
if(hFindFile == INVALID_HANDLE_VALUE)
{
return false;
}
return w32fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY);
}
int RD(const char* folderName)
{
DIR *dir;
struct dirent *ent;
dir = opendir(folderName);
if(dir != NULL)
{
while((ent = readdir(dir)) != NULL)
{
if(strcmp(ent->d_name , ".") == 0 ||
strcmp(ent->d_name, "..") == 0)
{
continue;
}
char fileName[MAXFILEPATH];
sprintf(fileName,"%s%c%s", folderName, '\\', ent->d_name);
if(IsDirectory(fileName))
{
RD(fileName);
}
else
{
unlink(fileName);
}
}
closedir(dir);
//chmod(folderName, S_IWRITE | S_IREAD);
if(_rmdir(folderName) != 0)perror(folderName);
}
else
{
printf("%s <%s>\n","Could Not Open Directory.", folderName);
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("usage: ./a.out <target folder name>\n");
return 1;
}
//RD(argv[1]);
//_mkdir("12");
//_mkdir("12\\34");
//_rmdir("12\\34");
//_rmdir("12");
char buf[0xff];
sprintf(buf, "unzip -x -q -d 1234 1234.zip");
system(buf);
RD("1234");
//unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56\\5.txt");
//unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56\\6.txt");
//unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\1_23.zip");
//unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\4.txt");
//_rmdir("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56");
//_rmdir("D:\\dev\\c\\project\\removeFolder\\Debug\\1234");
return 0;
}
輸出是:如何在windows上刪除c中的.zip文件? (錯誤:目錄非空)
--------------------------
Archive: 1234.zip
inflating: 1234/4.txt
inflating: 1234/56/5.txt
inflating: 1234/56/6.txt
inflating: 1234/1_23.zip
--------------------------
您需要發佈*問題*。儘可能簡潔明瞭地描述你的問題,使用簡短的代碼示例在適當的地方澄清你的問題 - 這樣你會得到更好的反應(人們不喜歡理解冗長的代碼示例) – Justin 2011-02-09 07:26:53