如何检查、创建文件夹是否存在

sunada2005 2009-04-20 11:10:21

在C++程序中,什么函数可以分别检查、创建文件夹呢?希望提供的参数为相对路径。
在网上找了一些函数,如windows下的DirectortyExists(),可以用来检查文件夹是否存在,CreateDirectory("path")可以用来创建文件夹。
我在程序中已加入了windows.h这个头文件,为什么编译器还是提示这两个函数未定呢?
还找到了一些API函数,如BOOL FolderExists(LPCTSTR lpPath)之类,但存在同样的问题。在我加了stdio.h这个头文件后,函数仍旧不可用……

这是为什么呢?我的头文件加的不正确么?请大侠们指点一下,我都该加什么头文件……谢谢!!
我是这样用的:

char path[40] = "../abc/";
if(!FolderExists(path))
{
……
}
有什么错误的地方么?
...全文
676 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ky310 2009-04-21
  • 打赏
  • 举报
回复
编译出错的信息贴出来看看
sunada2005 2009-04-21
  • 打赏
  • 举报
回复
真奇怪,刚才又试了下,又可以用CreateDctionery("../abc/", NULL)来检测、创建文件夹了……可为什么msdn上提供的那个程序我会编译出错呢?
sunada2005 2009-04-21
  • 打赏
  • 举报
回复
以下是我在msdn上查到的关于CreateDirectory()的用法的程序示例。但我的编译器提示有错……。我编译环境为:vc6.0,控制台程序。
不知道在这种环境下我应该如何完成以下功能呢?程序功能要求:首先检测路径为"../abc/"的文件夹是否存在,存在就向其中创建一个文件;如果不存在,就创建这样一样新文件夹。
用CreateDirectory()怎么来创建一个新文件夹呢?我试过了,如果路径为"../abc/"的文件夹不存在,用CreateDirectory("../abc/", NULL);这个语句也不会来创建这样一个文件夹。这是为什么呢?要怎么使用这个函数才能创建一个新的文件夹呢?
using namespace System;
using namespace System::IO;
int main()
{

// Specify the directory you want to manipulate.
String^ path = "c:\\MyDir";
try
{

// Determine whether the directory exists.
if ( Directory::Exists( path ) )
{
Console::WriteLine( "That path exists already." );
return 0;
}

// Try to create the directory.
DirectoryInfo^ di = Directory::CreateDirectory( path );
Console::WriteLine( "The directory was created successfully at {0}.", Directory::GetCreationTime( path ) );

// Delete the directory.
di->Delete();
Console::WriteLine( "The directory was deleted successfully." );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}

}

stivenjia 2009-04-21
  • 打赏
  • 举报
回复
PathFileExists Function

--------------------------------------------------------------------------------

Determines whether a path to a file system object such as a file or directory is valid.


#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main(void)
{
// Valid file path name (file is there).
char buffer_1[ ] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;

// Invalid file path name (file is not there).
char buffer_2[ ] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;

// Return value from "PathFileExists".
int retval;

// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

else
{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);

if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
}

OUTPUT
==============
Search for the file path of : C:\TEST\file.txt
The file requested "C:\TEST\file.txt" is a valid file
The return from function is : 1

The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0
贪玩的老鼠 2009-04-21
  • 打赏
  • 举报
回复
CFileStatue fs
if (CFile::GetStatus("d:\\123",fs)&&fs.m_attribute&CFile::directory)
{
//存在
}
路人乙2019 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zhuweiping2003 的回复:]
楼上的通过判断文件属性的方法来判断文件是否存在
是一个非常好的方法
[/Quote]人家是判断有没有,而不是是不是.
BOOL b = CreateDirectory(_T(".../abc/"), NULL);
if (!b && (GetLastError() == ERROR_ALREADY_EXISTS))
{
// 存在
}
zhuweiping2003 2009-04-21
  • 打赏
  • 举报
回复
楼上的通过判断文件属性的方法来判断文件是否存在
是一个非常好的方法
yu19830206 2009-04-21
  • 打赏
  • 举报
回复
利用文件属性来做
BOOL IsDirectory(LPSTR pszDir)
{
DWORD dwAttr;
dwAttr = GetFileAttributes(pszDir);
if(dwAttr == -1L) //pszDir所指目标不存在
return FALSE;
if(dwAttr & FILE_ATTRIBUTE_DIRECTORY) //pszDir所指目标是文件夹
return TRUE;
return FALSE;//pszDir所指目标是文件
}
liuzxchina 2009-04-21
  • 打赏
  • 举报
回复
PathFileExists Function

Determines whether a path to a file system object such as a file or directory is valid.

Syntax

BOOL PathFileExists( LPCTSTR pszPath
);
Parameters

pszPath
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the full path of the object to verify.
Return Value

Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.

Remarks

This function tests the validity of the path. It works only on the local file system or on a remote drive that has been mounted to a drive letter. It returns FALSE if a mounted remote drive is out of service.

yu19830206 2009-04-21
  • 打赏
  • 举报
回复
利用文件属性来做
BOOL IsDirectory(LPSTR pszDir)
{
DWORD dwAttr;
dwAttr = GetFileAttributes(pszDir);
if(dwAttr == -1L)
return TRUE;
if(dwAttr & FILE_ATTRIBUTE_DIRECTORY)
return TRUE;
return FALSE;
}
NineheadedBird 2009-04-21
  • 打赏
  • 举报
回复
GetFileAttribute 判断返回值就可满足了
DavidHsing 2009-04-21
  • 打赏
  • 举报
回复
PathIsDirectory Function

--------------------------------------------------------------------------------

Verifies that a path is a valid directory.

Syntax

BOOL PathIsDirectory( LPCTSTR pszPath
);
Parameters

pszPath
[in] A pointer to a null-terminated string of maximum length MAX_PATH that contains the path to verify.
Return Value

Returns TRUE if the path is a valid directory, or FALSE otherwise.

WaistCoat20 2009-04-20
  • 打赏
  • 举报
回复

BOOL b = CreateDirectory(_T(".../abc/"), NULL);
if (!b && (GetLastError() == ERROR_ALREADY_EXISTS))
{
// 存在
}


#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
BOOL b = PathFileExists(_T(".../abc/"));
if (b)
{
}
liao05050075 2009-04-20
  • 打赏
  • 举报
回复
下面那帖子有N多方法
如何判断一个文件夹是否存在?
http://topic.csdn.net/t/20020724/14/898714.html
力为 2009-04-20
  • 打赏
  • 举报
回复

if( !CreateDirectory(szDirectory, NULL) )
{
DWORD dwError = GetLastError();
//if( ERROR_ALREADY_EXISTS == dwError )
//{
// AfxMessageBox(_T("项目名已经存在,请重新命名!"));
//}

if( ERROR_PATH_NOT_FOUND == dwError )
{
AfxMessageBox(_T("目录创建不成功!"));
return;
}


}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧