代码之家  ›  专栏  ›  技术社区  ›  Wizard

删除C中的目录

  •  0
  • Wizard  · 技术社区  · 6 年前

    我想删除C,Windows中的一个目录。我知道我必须首先删除目录中的所有文件,这是我感到困惑的部分。我找到了 SHFileOperation IFileOperation 尽管我不知道它们是如何从文档中实现的。

    我目前的实施:

    int dir_exists = 0;
    snprintf(dir_name, STR_SIZE, "%s\\%s", LOCAL_DIR, tokens[1]);
    
    // Check if directory exists
    DWORD dwAttrib = GetFileAttributes(dir_name);
    if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
        dir_exists = 1;
    }
    
    if (dir_exists == 1) {
    
        // Remove contents of directory (CURRENTLY UNIX)
        DIR *dir;
        struct dirent *d;
        char filepath[FNAME_SIZE];
        dir = opendir(dir_name);
        while (d = readdir(dir)) {
            sprintf(filepath, "%s/%s", dir_name, d->d_name);
            remove(filepath);
        }
    
        // Remove directory
        if ((RemoveDirectory(dir_name)) == 0) {
            printf("Error removing dictionary\n");
            return 0;
        }
    }
    

    我想换一个windows的评论部分 没有 使用 #include <dirent.h> 由于我的VS没有此头文件:

    // Remove contents of directory (CURRENTLY UNIX)
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   Wizard    6 年前

    解决方案信用: Link

    如解决方案所述,使用此函数时请小心,并确保双null终止 pFrom .

    dir_name[STR_SIZE];
    int dir_exists = 0;
    snprintf(dir_name, STR_SIZE, "folder/dir_to_be_deleted");
    
    // Check if directory exists
    DWORD dwAttrib = GetFileAttributes(dir_name);
    if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
        dir_exists = 1;
    }
    
    if (dir_exists == 1) {
    
        // Remove contents of directory 
        snprintf(dir_name, STR_SIZE, "%s/%s", LOCAL_DIR, tokens[1]);
        TCHAR szDir[MAX_PATH];
        StringCchCopy(szDir, MAX_PATH, dir_name);
        StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
    
        int len = _tcslen(szDir);
        TCHAR* pszFrom = malloc((len + 4) * sizeof(TCHAR)); //4 to handle wide char
        StringCchCopyN(pszFrom, len + 2, szDir, len + 2);
        pszFrom[len] = 0;
        pszFrom[len + 1] = 0;
    
        SHFILEOPSTRUCT fileop;
        fileop.hwnd = NULL;    // no status display
        fileop.wFunc = FO_DELETE;  // delete operation
        fileop.pFrom = pszFrom;  // source file name as double null terminated string
        fileop.pTo = NULL;    // no destination needed
        fileop.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;  // do not prompt the user
        fileop.fAnyOperationsAborted = FALSE;
        fileop.lpszProgressTitle = NULL;
        fileop.hNameMappings = NULL;
    
        if (SHFileOperation(&fileop) != 0) {
            printf("Error SHFileOperation\n");
            return 0;
        }
        free(pszFrom);
    
        // Remove directory
        if ((RemoveDirectory(dir_name)) == 0) {
            printf("Error removing dictionary\n");
            return 0;
        }
    }
    
        2
  •  0
  •   Marichyasana    6 年前

    下面是一个列出文件夹中所有文件和目录的简单程序:

    #include <stdio.h>
    #include <dirent.h>
    
    int main(void)
    {
        struct dirent *de;  // Pointer for directory entry
    
        // opendir() returns a pointer of DIR type. 
        DIR *dr = opendir(".");
    
        if (dr == NULL)  // opendir returns NULL if couldn't open directory
        {
            printf("Could not open current directory" );
            return 0;
        }
    
        while ((de = readdir(dr)) != NULL)
                printf("%s\n", de->d_name);
    
        closedir(dr);    
        return 0;
    }
    

    它看起来不像你的代码那么复杂。 通过查看文件夹中每个条目的目录结构,应该很容易找出它是否是一个目录,如果是,则中止。然后将printf更改为文件的某种delete语句。

    您可能需要调用stat来告诉文件或目录。

        3
  •  -1
  •   Marichyasana    6 年前

    这是一个简单的windows批处理文件,可以完成这项工作。

    @echo off
    echo (batch file to remove a directory on windows)
    cd  /d %tmp%
    for /d %%i in (*) do (echo this directory contains a folder "%%i") & (echo so it cannot be deleted) & (goto :END)
    for /r %%i in (*) do (echo del %%i)
    :END  
    

    更改第三行以使用您的目录。删除第5行的“echo”。 如果您被锁定使用“c”,则可以使用“system()”来运行它。