Utilities

Sample code for basic directory & file manipulation.

Download fsutils.zip

Synopsis:

fsutils.cpp
fsutils.h
fsutils_test.cpp


fsutils.cpp

Synopsis
#include "fsutils.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static DWORD cFileAttribute = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE;
static DWORD cDirAttribute =  FILE_ATTRIBUTE_DIRECTORY;

class FileFinder
  {
  public:
    bool first(const string& path)
      {
      string fspec = path + "\\*.*";
      h = ::FindFirstFile(fspec.c_str(), &fdata);
      return h != INVALID_HANDLE_VALUE;
      }
    bool next()
      {
      return ::FindNextFile(h, &fdata) == TRUE;
      }
    string getName()
      {
      return fdata.cFileName;
      }
    bool isDotFiles()
      {
      return getName() == "." || getName() == "..";
      }
    void close()
      {
      ::FindClose(h);
      }
    bool isDir()
      {
      return (fdata.dwFileAttributes & cDirAttribute) != 0;
      }
    bool isFile()
      {
      return (fdata.dwFileAttributes & cFileAttribute) != 0;
      }
  private:
    WIN32_FIND_DATA fdata;
    HANDLE h;
  } ;

void FSUtils::CreateEmptyFile(const string& path)
  {
  HANDLE h = ::CreateFile(path.c_str(),
    GENERIC_READ|GENERIC_WRITE, 
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    0,
    CREATE_NEW,
    cFileAttribute,
    0);
  ::CloseHandle(h);
  }
void FSUtils::DelFile(const string& path)
  {
  ::DeleteFile(path.c_str());
  }
void FSUtils::CreateDir(const string& path)
  {
  ::CreateDirectory(path.c_str(), 0);
  }
void FSUtils::DelDirTree(const string& path)
  {
  FileFinder finder;
  if (!finder.first(path)) return;
  do {
    if (finder.isDotFiles()) continue;
    if (finder.isFile()) 
      DelFile(path + "\\" + finder.getName());
    else if (finder.isDir()) 
      DelDirTree(path + "\\" + finder.getName());
    } while(finder.next());
  finder.close();
  ::RemoveDirectory(path.c_str());
  }
bool FSUtils::DirExists(const string& path)
  {
  return EntityExists(path, cDirAttribute);
  }
bool FSUtils::FileExists(const string& path)
  {
  return EntityExists(path, cFileAttribute);
  }
bool FSUtils::EntityExists(const string& path, unsigned long typeflag)
  {
  DWORD res = ::GetFileAttributes(path.c_str());
  if (res == 0xFFFFFFFF) return false;
  if (res & typeflag) return true;
  return false;
  }

fsutils.h

Synopsis
#pragma once
#include <string>
using std::string;
class FSUtils
  {
  public:
    void CreateEmptyFile(const string& path);
    void DelFile(const string& path);
    void CreateDir(const string& path);
    void DelDirTree(const string& path);
    bool DirExists(const string& path);
    bool FileExists(const string& path);
  private:
    bool EntityExists(const string& path, unsigned long typeflag);
  } ;

fsutils_test.cpp

Synopsis
#include "utx.h"

#include "FSUtils.h"
static const string testpath = "d:\\projects\\test\\fsutils";

TEST(fsUtils_dirs)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));
  fsu.CreateDir(testpath);
  utxBoolAssert(fsu.DirExists(testpath));
  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));
  }

TEST(fsUtils_subdirs)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));

  string sub1 = testpath + "\\sub1";
  string sub2 = testpath + "\\sub2";
  fsu.CreateDir(testpath);
  fsu.CreateDir(sub1);
  fsu.CreateDir(sub2);
  utxBoolAssert(fsu.DirExists(sub1));
  utxBoolAssert(fsu.DirExists(sub2));

  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));
  }

TEST(fsUtils_files)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  fsu.CreateDir(testpath);
  utxBoolAssert(fsu.DirExists(testpath));
  string filepath = testpath + "\\" + "test1.txt";
  fsu.CreateEmptyFile(filepath);
  utxBoolAssert(fsu.FileExists(filepath));
  fsu.DelFile(filepath);
  utxBoolAssert(!fsu.FileExists(filepath));

  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));
  utxBoolAssert(!fsu.FileExists(filepath));
  }

TEST(fsUtils_dupDelDirTree)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));
  }

TEST(fsUtils_dupCreateDir)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  utxBoolAssert(!fsu.DirExists(testpath));

  fsu.CreateDir(testpath);
  utxBoolAssert(fsu.DirExists(testpath));
  fsu.CreateDir(testpath);
  utxBoolAssert(fsu.DirExists(testpath));
  }

TEST(fsUtils_dupCreateFile)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  fsu.CreateDir(testpath);
  utxBoolAssert(fsu.DirExists(testpath));

  string filepath = testpath + "\\" + "test2.txt";
  fsu.CreateEmptyFile(filepath);
  utxBoolAssert(fsu.FileExists(filepath));
  fsu.CreateEmptyFile(filepath);
  utxBoolAssert(fsu.FileExists(filepath));
  }

TEST(fsUtils_dupDelFile)
  {
  FSUtils fsu;
  fsu.DelDirTree(testpath);
  fsu.CreateDir(testpath);
  utxBoolAssert(fsu.DirExists(testpath));

  string filepath = testpath + "\\" + "test3.txt";
  fsu.CreateEmptyFile(filepath);
  utxBoolAssert(fsu.FileExists(filepath));

  fsu.DelFile(filepath);
  utxBoolAssert(!fsu.FileExists(filepath));
  fsu.DelFile(filepath);
  utxBoolAssert(!fsu.FileExists(filepath));
  }






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010