如何判断一个字符串是盘符,目录,还是普通文件

asd_ 2008-02-20 04:44:21
需求是这样的,需要处理一个字符串,比如 c:,c:\ ,c:\windows,c:\windos\my.txt.
大致意思就是如果是c:,表示处理这个盘下面的所有的文件,如果是c:\windows,表示处理这个目录下面的所有文件,如果是c:\windows\my.txt,表示只处理这一个文件。

我会点c,但是对windows api不熟,查了下文档,还是不知道该用什么方法去判断这个字符串代表了一个盘符,一个目录,还是一个文件,还是什么都不是。请指点。
...全文
440 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnzdgs 2008-02-21
  • 打赏
  • 举报
回复
GetFileAttributes,如果成功,则看是否具有目录属性,可以知道是文件还是目录;如果失败,则是盘符或者无效路径,盘符就是字母加冒号很容易判断。
flyhigh 2008-02-20
  • 打赏
  • 举报
回复
GetFileAttributes
lwykj 2008-02-20
  • 打赏
  • 举报
回复
可以 判断 是否 为 . 或者 ..
cofanz 2008-02-20
  • 打赏
  • 举报
回复
PathIsFileSpec,PathIsDirectory,PathIsRoot...
shakaqrj 2008-02-20
  • 打赏
  • 举报
回复
盘符格式固定
不难判断
ouyh12345 2008-02-20
  • 打赏
  • 举报
回复
GetDriveType
判断盘符

shakaqrj 2008-02-20
  • 打赏
  • 举报
回复
CFileFind::IsDirectory
BOOL IsDirectory( ) const;

Return Value

Nonzero if successful; otherwise 0.

Remarks

Call this member function to determine if the found file is a directory. A file that is a directory is marked with FILE_ATTRIBUTE_DIRECTORY a file attribute identified in theWIN32_FIND_DATA structure.

See the member function MatchesMask for a complete list of file attributes.

Example

This small program recurses every directory on the C:\ drive and prints the name of the directory.

#include <afx.h>
#include <iostream>

using namespace std;

void Recurse(LPCTSTR pstr)
{
CFileFind finder;

// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");

// start working for files
BOOL bWorking = finder.FindFile(strWildcard);

while (bWorking)
{
bWorking = finder.FindNextFile();

// skip . and .. files; otherwise, we'd
// recur infinitely!

if (finder.IsDots())
continue;

// if it's a directory, recursively search it

if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
cout << (LPCTSTR) str << endl;
Recurse(str);
}
}

finder.Close();
}

void main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
cout << "panic!" << endl;
else
Recurse(_T("C:"));
}

16,473

社区成员

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

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

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