windows/ Linux 遍历目录(包括远程共享目录)权限

// searchFile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"





#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <Strsafe.h>//#include <shlwapi.h>

using namespace std;

//A

void find(char * lpPath, char * secName = ".*")
{
 char szFind[MAX_PATH*10];
 char szFile[MAX_PATH*10];

 WIN32_FIND_DATAA FindFileData;

 strcpy(szFind,lpPath);
 strcat(szFind,"//*");
 strcat(szFind,secName);//过虑的名字

 HANDLE hFind=::FindFirstFileA(szFind,&FindFileData);

 if(INVALID_HANDLE_VALUE == hFind)    return;
 while(TRUE)
 {
  if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if(FindFileData.cFileName[0]!='.')
   {
    strcpy(szFile,lpPath);
    strcat(szFile,"//");
    strcat(szFile,FindFileData.cFileName);
    find(szFile);
   }
  }
  else
  {
       OutputDebugStringA(FindFileData.cFileName);
   //cout << FindFileData.cFileName<<endl;
  }
  if(!FindNextFileA(hFind,&FindFileData))    break;
 }

 FindClose(hFind);
}


//w

//Max size of the buffer
#define MAX_BUFF_SIZE 1024
//calculates the array size in bytes
#define STRSIZE_IN_BYTES(x)  (_countof(x) * sizeof(TCHAR))
//Replaces the StringCbCopy API with user defined Macro
#define StringCopy(x, y) StringCbCopy(x, STRSIZE_IN_BYTES(x), y);



void DirectoryTraverse(TCHAR * lpPath, TCHAR * szExtension = _T(".*"))
{
 TCHAR szFind[MAX_PATH*5];//遍历深度 最大PATH是多少呢?260*260?
 TCHAR szFile[MAX_PATH*5];

 WIN32_FIND_DATA FindFileData;


 size_t strLength1(0);


 //safe string func
 ULONG64 maxSize = STRSAFE_MAX_CCH * sizeof(TCHAR);


 //count bytes length  S_OK   、单位(Byte)
 StringCbLength(lpPath,maxSize,&strLength1);//count string in byte(not number)  //CountBytes = number*sizeof(TCHAR)
// cout<<"StringCbLength(lpPath,MAX_PATH,strLength1)==="<<strLength1<<endl;




//count chars length 字符个数  、单位(TCHAR)
 //StringCchLength(lpPath,MAX_PATH*sizeof(TCHAR),&strLength1);
 //cout<<"StringCchLength(lpPath,MAX_PATH,strLength1)==="<<strLength1<<endl;


 StringCbCopy(szFind,strLength1+sizeof(TCHAR),lpPath);// strLength1+sizeof(TCHAR)   加上结束符 '\0'
 StringCbCat(szFind,maxSize,_T("//*"));//StringCbCat(szFind,MAX_PATH,_T("//*"))
 StringCbCat(szFind,maxSize,szExtension);//过虑的名字




 HANDLE hFind=::FindFirstFile(szFind,&FindFileData);

 if(INVALID_HANDLE_VALUE == hFind)    return;
 while(TRUE)
 {
  if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   if( FindFileData.cFileName[0] != '.' )//'.'sub folder     '..'parente folder
   {
    StringCbCopy(szFile,strLength1+sizeof(TCHAR),lpPath);
    StringCbCat(szFile,maxSize,_T("//"));// StringCbCat(szFile,MAX_PATH,_T("//"));
    StringCbCat(szFile,maxSize,FindFileData.cFileName);
    DirectoryTraverse(szFile);
   }
  }
  else
  {
   OutputDebugString(FindFileData.cFileName);
     // cout<<&FindFileData.cFileName<<endl;
  }
  if(!FindNextFile(hFind,&FindFileData))    break;
 }

 FindClose(hFind);
}


int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR path[MAX_PATH*2] = _T("E://BOOK");//TEXT("E://BOOK//wndows");
    TCHAR extension[MAX_PATH] = _T(".pdf");


    DirectoryTraverse(path);

    //find("E://BOOK");


    return 0;
}
View Code

=============上面的是Windows multibyte版本和wideChar版本

wideChar用了更安全的函数

 

 

 

 

 

 

一些讨论点:

为什么不能遍历到所有文件,为什么每次结果都不一样?

深层次遍历的难点在哪?  路径

有什么办法查找更快呢?能的话,要用什么算法,而不是用windows提供的API

 

 

 

 

 

 

 

2.访问共享目录

//======================

和普通的一样,

比如"\\\\192.168.1.254\\share Folder" 访问的是“\\192.168.1.254\share Folder” 这个共享目录(我用的是ubuntu 14.04,samba: 4.1.6-Ubuntu)权限是对everyone完全开放

 

 

那么如果要访问有权限的目录呢?(参考http://zhidao.baidu.com/question/368042349591885604.html)

 

1、创建一个作业(CreateJobObject);
2、使用CreateProcess("net use...")代替system调用,然后调用AssignProcessToJobObject将进程附加到作业;
3、CreateProcess("explorer...")并指定CREATE_SUSPENDED标识,然后将进程添加到作业并ResumeThread开始执行。

另外,
使用
CreateProcessWithLogonW
CreateProcessWithTokenW
CreateProcessAsUser

应该也可以实现



 

 
windows相关   
1.
Process and Thread Functions

2.
Creating Processes
Creating Threads
Creating a Child Process with Redirected Input and Output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

===============linux 版本

待续。

 

 

tips:

C11和C++11 语言中都有unicode版本支持。将来如何改?

 

 

 

posted @ 2014-10-23 18:29  scott_h  阅读(37)  评论(0编辑  收藏  举报