Difference between revisions of "File Existance in Windows"

From PeformIQ Upgrade
Jump to navigation Jump to search
(New page: =Example= See - http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx <pre> // For File::Exists, Directory::Exists using namespace System; using namespace System::IO; u...)
 
Line 65: Line 65:


   }
   }
}
</pre>
=Make Sure it Exists=
<pre>
BOOL MakeSurePathExists( CString &Path,
                        bool FilenameIncluded=true)
{
  int Pos=0;
  while((Pos=Path.Find('\\',Pos+1))!=-1)
      CreateDirectory(Path.Left(Pos),NULL);
  if(!FilenameIncluded)
      CreateDirectory(Path,NULL);
  return ((!FilenameIncluded)?!_access(Path,0):
        !_access(Path.Left(Path.ReverseFind('\\')),0));
}
int make_sure_path_exists( const char *iPath,
                          bool FilenameIncluded=true)
{
  char *Path=(char*)iPath,
        *TmpPath=Path,
        TmpSmb=0,
        *LastDPtr=NULL;
  while((TmpPath=strpbrk(TmpPath+1,"\\/")))
  {
      TmpSmb=Path[TmpPath-Path];
      Path[TmpPath-Path]=0;
      CreateDirectory(Path,NULL);
      Path[TmpPath-Path]=TmpSmb;
      LastDPtr=TmpPath;
  }
  int Res=1;
  if(!FilenameIncluded)
  {
      CreateDirectory(iPath,NULL);
      Res=!_access(iPath,0);
  }
  else
  {
      if(LastDPtr)
      {
        Path=(char*)iPath;
        TmpSmb=Path[LastDPtr-Path];
        Path[LastDPtr-Path]=0;
        Res=!_access(Path,0);
        Path[LastDPtr-Path]=TmpSmb;
      }
  }
  return Res;
}
}
</pre>
</pre>

Revision as of 13:40, 21 July 2008

Example

See - http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx

// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;

// Insert logic for processing found files here.
void ProcessFile( String^ path )
{
   Console::WriteLine( "Processed file '{0}'.", path );
}


// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
void ProcessDirectory( String^ targetDirectory )
{

   // Process the list of files found in the directory.
   array<String^>^fileEntries = Directory::GetFiles( targetDirectory );
   IEnumerator^ files = fileEntries->GetEnumerator();
   while ( files->MoveNext() )
   {
      String^ fileName = safe_cast<String^>(files->Current);
      ProcessFile( fileName );
   }


   // Recurse into subdirectories of this directory.
   array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory );
   IEnumerator^ dirs = subdirectoryEntries->GetEnumerator();
   while ( dirs->MoveNext() )
   {
      String^ subdirectory = safe_cast<String^>(dirs->Current);
      ProcessDirectory( subdirectory );
   }
}

int main( int argc, char *argv[] )
{
   for ( int i = 1; i < argc; i++ )
   {
      String^ path = gcnew String(argv[ i ]);
      if ( File::Exists( path ) )
      {

         // This path is a file
         ProcessFile( path );
      }
      else
      if ( Directory::Exists( path ) )
      {

         // This path is a directory
         ProcessDirectory( path );
      }
      else
      {
         Console::WriteLine( "{0} is not a valid file or directory.", path );
      }

   }
}

Make Sure it Exists

BOOL MakeSurePathExists( CString &Path,
                         bool FilenameIncluded=true)
{
   int Pos=0;
   while((Pos=Path.Find('\\',Pos+1))!=-1)
      CreateDirectory(Path.Left(Pos),NULL);
   if(!FilenameIncluded)
      CreateDirectory(Path,NULL);
   return ((!FilenameIncluded)?!_access(Path,0):
        !_access(Path.Left(Path.ReverseFind('\\')),0));
}

int make_sure_path_exists( const char *iPath,
                           bool FilenameIncluded=true)
{
   char *Path=(char*)iPath,
        *TmpPath=Path,
        TmpSmb=0,
        *LastDPtr=NULL;
   while((TmpPath=strpbrk(TmpPath+1,"\\/")))
   {
      TmpSmb=Path[TmpPath-Path];
      Path[TmpPath-Path]=0;
      CreateDirectory(Path,NULL);
      Path[TmpPath-Path]=TmpSmb;
      LastDPtr=TmpPath;
   }

   int Res=1;
   if(!FilenameIncluded)
   {
      CreateDirectory(iPath,NULL);
      Res=!_access(iPath,0);
   }
   else
   {
      if(LastDPtr)
      {
         Path=(char*)iPath;
         TmpSmb=Path[LastDPtr-Path];
         Path[LastDPtr-Path]=0;
         Res=!_access(Path,0);
         Path[LastDPtr-Path]=TmpSmb;
      }
   }

   return Res;
}