16,471
社区成员
发帖
与我相关
我的任务
分享
@echo off
dir /S /b dir_name >filepath.txt
exit
#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincon.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <tchar.h>
BOOLEAN IsFileName(LPTSTR pszFileName)
{
if(pszFileName == NULL ||
*pszFileName == TEXT('\0')) {
return FALSE;
}
if(*pszFileName == TEXT('.') &&
(*(pszFileName+1) == TEXT('\0') ||
*(pszFileName+1) == TEXT('.') &&
*(pszFileName+2) == TEXT('\0'))) {
return FALSE;
}
return TRUE;
}
int FindFiles(LPTSTR pszPath)
{
HANDLE hFindFile;
WIN32_FIND_DATA FindData;
TCHAR szCurrPath[MAX_PATH+1];
TCHAR szNextPath[MAX_PATH+1];
PathCombine(szCurrPath, pszPath, TEXT("*"));
hFindFile = FindFirstFile(szCurrPath, &FindData);
if(INVALID_HANDLE_VALUE == hFindFile) {
return -1;
}
do {
if(!IsFileName(FindData.cFileName)) {
continue;
}
if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
PathCombine(szNextPath, pszPath, FindData.cFileName);
_tprintf(TEXT("%s: %s\n"), TEXT("path"), szNextPath);
FindFiles(szNextPath);
} else {
PathCombine(szNextPath, pszPath, FindData.cFileName);
_tprintf(TEXT("%s: %s\n"), TEXT("file"), szNextPath);
}
} while(FindNextFile(hFindFile, &FindData));
FindClose(hFindFile);
return 0;
}
int _tmain(int argc, LPTSTR argv[])
{
if(argc == 1) {
_tprintf(TEXT("Usage: findfile <path>"));
return -1;
}
if(!PathIsDirectory(argv[1])) {
_tprintf(TEXT("Path \"%s\" doesn't exist."), argv[1]);
return -1;
}
return FindFiles(argv[1]);
}
void TraverseDir(CString& strDir, std::vector<CString>& vecDir)
{
WIN32_FIND_DATA FindFileData;
CString strDirTmp;
strDirTmp = strDir;
strDirTmp += "\\*.*";
HANDLE hFind=::FindFirstFile(strDirTmp,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
{
return;
}
while(TRUE)
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(FindFileData.cFileName[0]!= _T('.'))
{
strDirTmp = strDir;
strDirTmp += "\\";
strDirTmp += FindFileData.cFileName;
vecDir.push_back(strDirTmp);
//TraverseDir(strDirTmp,vecFile) ;
}
}
else//是文件
{
/*strDirTmp = strDir;
strDirTmp += "\\";
strDirTmp += FindFileData.cFileName;
vecFile.push_back(strDirTmp);*/
}
if(!FindNextFile(hFind,&FindFileData))
break;
}
FindClose(hFind);
}