File Existance in Windows

From PeformIQ Upgrade
(Redirected from File Existance in WIndows)
Jump to navigation Jump to search

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 );
      }

   }
}

Create a Path to 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;
   char *TmpPath      = Path;
   char TmpSmb        = 0;
   char *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;
}

Search for File

//  crt_searchenv.c
//
//  This program searches for a file in a directory specified by an environment variable.
//

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   char pathbuffer[_MAX_PATH];
   char searchfile[] = "CL.EXE";
   char envvar[] = "PATH";

   /* Search for file in PATH environment variable: */

   _searchenv( searchfile, envvar, pathbuffer);
   if (*pathbuffer != '\0')
      printf("Path for %s:\n%s\n", searchfile, pathbuffer);
   else
      printf("%s not found\n", searchfile);
}