Difference between revisions of "File Existance in Windows"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 68: | Line 68: | ||
</pre> | </pre> | ||
=Make Sure it Exists= | =Create a Path to Make Sure it Exists= | ||
<pre> | <pre> | ||
BOOL MakeSurePathExists( CString &Path, | BOOL MakeSurePathExists( CString &Path, bool FilenameIncluded=true) | ||
{ | { | ||
int Pos=0; | int Pos=0; | ||
while((Pos=Path.Find('\\',Pos+1))!=-1) | |||
while ((Pos=Path.Find('\\',Pos+1))!=-1) | |||
CreateDirectory(Path.Left(Pos),NULL); | CreateDirectory(Path.Left(Pos),NULL); | ||
if(!FilenameIncluded) | if(!FilenameIncluded) | ||
CreateDirectory(Path,NULL); | CreateDirectory(Path,NULL); | ||
return ((!FilenameIncluded)?!_access(Path,0): | |||
return ((!FilenameIncluded)?!_access(Path,0): !_access(Path.Left(Path.ReverseFind('\\')),0)); | |||
} | } | ||
</pre> | |||
int make_sure_path_exists( const char *iPath, | <pre> | ||
int make_sure_path_exists( const char *iPath, bool FilenameIncluded=true) | |||
{ | { | ||
char *Path=(char*)iPath | char *Path=(char*)iPath; | ||
char *TmpPath=Path; | |||
char TmpSmb=0; | |||
char *LastDPtr=NULL; | |||
while((TmpPath=strpbrk(TmpPath+1,"\\/"))) | while((TmpPath=strpbrk(TmpPath+1,"\\/"))) | ||
{ | { | ||
TmpSmb=Path[TmpPath-Path]; | TmpSmb = Path[TmpPath - Path]; | ||
Path[TmpPath-Path]=0; | Path[TmpPath-Path] = 0; | ||
CreateDirectory(Path,NULL); | CreateDirectory(Path,NULL); | ||
Path[TmpPath-Path]=TmpSmb; | Path[TmpPath-Path] = TmpSmb; | ||
LastDPtr=TmpPath; | LastDPtr = TmpPath; | ||
} | } | ||
int Res=1; | int Res = 1; | ||
if(!FilenameIncluded) | if(!FilenameIncluded) | ||
{ | { | ||
CreateDirectory(iPath,NULL); | CreateDirectory(iPath, NULL); | ||
Res=!_access(iPath,0); | Res = !_access(iPath, 0); | ||
} | } | ||
else | else | ||
Line 109: | Line 113: | ||
if(LastDPtr) | if(LastDPtr) | ||
{ | { | ||
Path=(char*)iPath; | Path = (char*)iPath; | ||
TmpSmb=Path[LastDPtr-Path]; | TmpSmb = Path[LastDPtr - Path]; | ||
Path[LastDPtr-Path]=0; | Path[LastDPtr - Path] = 0; | ||
Res=!_access(Path,0); | Res = !_access(Path, 0); | ||
Path[LastDPtr-Path]=TmpSmb; | Path[LastDPtr - Path] = TmpSmb; | ||
} | } | ||
} | } |
Revision as of 14:44, 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 ); } } }
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; }