怎样判断一个Path路径(可能包含文件名)所指定的是文件,还是目录?

zyq_123 2012-08-22 05:45:00
就是说,一个Path路径,有可能是目录,有可能是文件,怎样进行判断?
...全文
589 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingfeng2510 2012-08-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

引用 11 楼 的回复:
DWORD WINAPI GetFileAttributes(__in LPCTSTR lpFileName);
获取返回值ret,若ret & FILE_ATTRIBUTE_DIRECTORY(0x10)!= 0则表明是目录,否则是文件。


if(GetFileAttributes(lpFileName) & FILE_ATTRI……
[/Quote]

GetFileAttributes : This function returns attributes for a specified file or directory.
该函数只是判断文件或目录的属性,所以也就只有if...else 而没有if...else if ...else ...
zhaoZero41 2012-08-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

要是最后一个"\"之后的名称包含".",但是目录也是可以有"."的。
[/Quote]

确实没考虑周到。当时要下班了,就随手敲了段代码
矫情狗_____ 2012-08-23
  • 打赏
  • 举报
回复
CFindFile
caoyiwen19830726 2012-08-23
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
WIN32_FIND_DATA fileinfo; //存储文件信息
HANDLE hFile; //文件句柄
string filename;
char filedir[255];
memset(filedir,0,255);
cout<<"请输入文件或目录的路径:";
cin>>filedir;
hFile = FindFirstFile(filedir, &fileinfo);
if(hFile == INVALID_HANDLE_VALUE){
cout<<"该路径下的文件或目录不存在"<<endl;
}else{
if((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0){
cout<<"这是一个文件"<<endl;
}else{
cout<<"这是一个目录"<<endl;
}
}
FindClose(hFile);
return 0;
}
赵4老师 2012-08-23
  • 打赏
  • 举报
回复
Get status information on a file.

int _stat( const char *path, struct _stat *buffer );

The _stat structure, defined in SYS\STAT.H, includes the following fields.

st_mode

Bit mask for file-mode information. The _S_IFDIR bit is set if path specifies a directory; the _S_IFREG bit is set if path specifies an ordinary file or a device. User read/write bits are set according to the file’s permission mode; user execute bits are set according to the filename extension.

zyq_123 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
DWORD WINAPI GetFileAttributes(__in LPCTSTR lpFileName);
获取返回值ret,若ret & FILE_ATTRIBUTE_DIRECTORY(0x10)!= 0则表明是目录,否则是文件。
[/Quote]

if(GetFileAttributes(lpFileName) & FILE_ATTRIBUTE_DIRECTORY(0x10)!= 0)
printf("这是目录。\n");
else
printf("这是文件。\n");

第一个问题,是不是这样?
第二个问题,要是判断是不是文件,那FILE_ATTRIBUTE_DIRECTORY(0x10)应该换成什么代码?
Gloveing 2012-08-22
  • 打赏
  • 举报
回复
直接打开文件,看是否能正确执行
xingfeng2510 2012-08-22
  • 打赏
  • 举报
回复
DWORD WINAPI GetFileAttributes(__in LPCTSTR lpFileName);
获取返回值ret,若ret & FILE_ATTRIBUTE_DIRECTORY(0x10)!= 0则表明是目录,否则是文件。


zyq_123 2012-08-22
  • 打赏
  • 举报
回复
md filename.path
cd filename.path
cd

能说明了,通过检查"."是不可行的,或者说是错误的!
zyq_123 2012-08-22
  • 打赏
  • 举报
回复
要是最后一个"\"之后的名称包含".",但是目录也是可以有"."的。
xingfeng2510 2012-08-22
  • 打赏
  • 举报
回复
最简单的方法就是把路径当做字符串strPath; 如果strPath.find(".") != string::npos;则表明是文件,否则为目录。

或者使用win32函数,使用FindFirstFile函数以及WIN32_FIND_DATA结构体,
根据if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)的结果就可以判断是文件或是目录
给出一个简单的例子

void _find(string path)
{
//取路径名最后一个"//"之前的部分,包括"//"
string prefix=path.substr(0,path.find_last_of('//')+1);

WIN32_FIND_DATA FindFileData;
HANDLE hFind=::FindFirstFile(path.c_str(),&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
{
cout<<"文件通配符错误"<<endl;
return;
}
while(TRUE)
{
//目录
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//不是当前目录,也不是父目录
if(FindFileData.cFileName[0]!='.')
{
//查找下一级目录
_find(prefix+FindFileData.cFileName+'//'+"*.*");
}
}
//文件
else
{
cout<<FindFileData.cFileName<<endl;
}
if(!FindNextFile(hFind,&FindFileData))
break;
}
FindClose(hFind);
}
就想叫yoko 2012-08-22
  • 打赏
  • 举报
回复
3#、4#典型的伸手window毒害的用户。
zyq_123 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
判断后缀,从路径尾部开始查找点号pos.存在pos.即为文件,不存在为目录!
[/Quote]

你错了,目录名也可以有点号。
zhaoZero41 2012-08-22
  • 打赏
  • 举报
回复
#include<iostream>
#include <string>
using namespace std;

void judge(string p);

void main()
{
string f = "D:\temp\a.bat";
string d = "D:\temp\bat";
judge(f);
judge(d);
}

void judge(string p)
{
bool find = false;
for(size_t i = p.size() - 1; i != 0; --i)
{
if(p[i] == '.')
find = true;
else if(p[i] == '\\')
break;
//break at first '\'
}
if(find)
cout<<"This is a file"<<endl;
else
cout<<"This is a path"<<endl;
}


输出:
This is a file
This is a path
Press any key to continue . . .


仅供参考
shen_wei 2012-08-22
  • 打赏
  • 举报
回复
#include <Windows.h>
#include <stdio.h>
#include <strsafe.h>
void _tmain(int argc, _TCHAR* argv[])
{
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
TCHAR szDirPath[] = TEXT("c:\\TextRO\\");
TCHAR szNewPath[MAX_PATH];

BOOL fFinished = FALSE;

// Create a new directory.

if (!CreateDirectory(szDirPath, NULL))
{
printf("Could not create new directory.\n");
return;
}

// Start searching for text files in the current directory.

hSearch = FindFirstFile(TEXT("*.txt"), &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No text files found.\n");
return;
}

// Copy each .TXT file to the new directory
// and change it to read only, if not already.

while (!fFinished)
{
StringCchCopy(szNewPath, MAX_PATH, szDirPath);
StringCchCat(szNewPath, MAX_PATH, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (dwAttrs==INVALID_FILE_ATTRIBUTES) return;

if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
printf("Could not copy file.\n");
return;
}

if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
printf("Copied all text files.\n");
fFinished = TRUE;
}
else
{
printf("Could not find next file.\n");
return;
}
}
}

// Close the search handle.

FindClose(hSearch);
}

tragedyhomeland 2012-08-22
  • 打赏
  • 举报
回复
判断后缀,从路径尾部开始查找点号pos.存在pos.即为文件,不存在为目录!
zyq_123 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
GetFileAttributes( ) 查看返回值吧
[/Quote]

具体呢?
vilnies 2012-08-22
  • 打赏
  • 举报
回复
GetFileAttributes( ) 查看返回值吧

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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