如何判定一个文件夹是否存在???

SKJG 2004-04-10 06:49:51
在VC中如何判定文件夹是否存在???

文件夹可能是一个普通的文件夹
如C:\Windows
也可能就是根目录
如C:\
...全文
511 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangy211 2004-09-27
  • 打赏
  • 举报
回复
关注
csdn_cht 2004-09-11
  • 打赏
  • 举报
回复
PathFileExists


BOOL PathFileExists(
LPCTSTR lpszPath
);

Determines if a file exists.

Returns TRUE if the file exists, or FALSE otherwise.
lpszPath
Address of the file to verify.
This function will test for the validity of the file and path.

Example:

#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

Mr-Chen 2004-09-11
  • 打赏
  • 举报
回复
CFileFind file;
BOOL bContinue = file.FindFile(strText);
while(bContinue)
{
bContinue = file.FindNextFile();
if(!file.IsDirectory())
AfxMessageBox("文件夹不存在");
}
大雨仔 2004-09-09
  • 打赏
  • 举报
回复
int CDeleteWrongInfoDlg::IsDirectoryOrFile(CString strFileName)
{
strFileName.TrimLeft();
strFileName.TrimRight();

WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(strFileName,&fd);
::FindClose(hFind);

//不存在同名的文件或文件夹
if (hFind == INVALID_HANDLE_VALUE)
{
return 0 ;
}
//判断是否为目录
else if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
return 2 ;
}
else
{
return 1;
}
}
jacking007 2004-05-09
  • 打赏
  • 举报
回复
INVALID_FILE_ATTRIBUTES
这个没有阿,我搜索msdn也没搜索到,程序报错说这个未定义。
xwqboy 2004-05-09
  • 打赏
  • 举报
回复
如判断c盘"xwq"文件夹是否存在

if (!PathIsDirectory("C:\\xwq"))
{
AfxMessageBox("请先建立'xwq'文件夹!");
return;
}

要包含如下头文件
#include "shlwapi.h"
并在project\setting
的link中导入shlwapi.lib
thundenet 2004-05-09
  • 打赏
  • 举报
回复
用BOOL PathFileExists(
LPCTSTR pszPath
);是最简单不过了
一条晚起的虫 2004-05-09
  • 打赏
  • 举报
回复
MSDN:

Verifies that a path is a valid directory.

BOOL PathIsDirectory(
LPCTSTR pszPath
);

Parameters
pszPath
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the path to verify.
Return Values
Returns TRUE if the path is a valid directory, or FALSE otherwise.

uoyevoli 2004-04-11
  • 打赏
  • 举报
回复
有没有人试试
PathIsDirectory
??

lsp5i5j 2004-04-10
  • 打赏
  • 举报
回复
学习!
ygang76 2004-04-10
  • 打赏
  • 举报
回复
GetFileAttributes
薛定谔之死猫 2004-04-10
  • 打赏
  • 举报
回复
学习
yy315 2004-04-10
  • 打赏
  • 举报
回复
查找文件夹
WIN32_FIND_DATA fdData;
HANDLE hFind=::FindFirstFile(strSrcFile,&fdData);

//如果源文件(包括文件夹)不存在,创建文件
if (hFind == INVALID_HANDLE_VALUE)
{
::CreateDirectory(strFileName,NULL);
}
::FindClose(hFind);
dzqsuper 2004-04-10
  • 打赏
  • 举报
回复
FileExists
eejeff 2004-04-10
  • 打赏
  • 举报
回复
up
wkgenius 2004-04-10
  • 打赏
  • 举报
回复
CString strDir = m_strRootDir + _T("\\");
DWORD dwFileAttr = GetFileAttributes(strDir);
if( dwFileAttr == INVALID_FILE_ATTRIBUTES
|| (dwFileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
if( !CreateDirectory(strDir,NULL) )
{
MessageBox(_T("不能创建目录"));
return;
}
}

上面的代码首先判断目录是否存在,如果不存在,则创建目录
byf2002 2004-04-10
  • 打赏
  • 举报
回复
文件夹和文件的判断方法是一样的.
byf2002 2004-04-10
  • 打赏
  • 举报
回复
#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".
intretval;

// 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
vcforever 2004-04-10
  • 打赏
  • 举报
回复

BOOL PathFileExists(
LPCTSTR pszPath
);

16,472

社区成员

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

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

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