Click here to Skip to main content
15,910,877 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Process Directory Pin
Tim Deveaux5-Jul-01 13:28
Tim Deveaux5-Jul-01 13:28 
GeneralRe: Process Directory Pin
John Uhlenbrock5-Jul-01 13:30
John Uhlenbrock5-Jul-01 13:30 
GeneralRe: Process Directory Pin
Tim Deveaux5-Jul-01 13:40
Tim Deveaux5-Jul-01 13:40 
GeneralRe: Process Directory Pin
Mike Burston5-Jul-01 14:30
Mike Burston5-Jul-01 14:30 
GeneralRe: Process Directory Pin
Kannan Kalyanaraman5-Jul-01 23:10
Kannan Kalyanaraman5-Jul-01 23:10 
GeneralRe: Process Directory Pin
Mike Burston5-Jul-01 13:36
Mike Burston5-Jul-01 13:36 
GeneralRe: Process Directory Pin
Carlos Antollini5-Jul-01 12:36
Carlos Antollini5-Jul-01 12:36 
GeneralRe: Process Directory Pin
#realJSOP6-Jul-01 4:54
professional#realJSOP6-Jul-01 4:54 
Here's a crap-load of filename code I use a lot. The function you're interested in is at the bottom, and it relies on one or two of the other functions. You didn't specify, but I assume you're using VC++ and MFC.

Here's the header file (FILENAME.H) :

#ifndef __FILENAME_H
#define __FILENAME_H

CString justFileName(LPCSTR);
CString justName(LPCSTR);
CString justExtention(LPCSTR);
CString justPath(LPCSTR);
CString justDrive(LPCSTR);
CString forceExtention(CString fn, CString newext);
CString forceFileName(CString fn, CString newname);
CString forcePath(CString fn, CString newpath);
BOOL FileExists(CString fn);
int DeleteFile(CString fname);
int RenameFile(CString oldname, CString newname);
void AddBackSlash(CString& path);
CString RemoveBackSlash(CString path);
CString GetProgramPath(BOOL bAndExeName=FALSE);
#endif


And here's the CPP file (FILENAME.CPP) :

#include "stdafx.h"
#include "filename.h"
#include <io.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//-------------------------------------------------------------------------//
CString justFileName(LPCSTR f)                                               
{
  char drive[_MAX_DRIVE],
        dir[_MAX_DIR],
        file[_MAX_FNAME],
        ext[_MAX_EXT];
  CString st;
  _splitpath((char *)f, drive, dir, file, ext);
  st = file;
  st += ext;
  return st;
}

//-------------------------------------------------------------------------//
CString justName(LPCSTR f)
{
  char drive[_MAX_DRIVE],
        dir[_MAX_DIR],
        file[_MAX_FNAME],
        ext[_MAX_EXT];
  CString st;
  _splitpath((char *)f, drive, dir, file, ext);
  st = file;
  return st;
}


//-------------------------------------------------------------------------//
CString justPath(LPCSTR f)
{
  char drive[_MAX_DRIVE],
        dir[_MAX_DIR],
        file[_MAX_FNAME],
        ext[_MAX_EXT];
  CString st;
  _splitpath((char *)f, drive, dir, file, ext);
  st = drive;
  st += dir;
  return st;
}   

//-------------------------------------------------------------------------//
CString justExtention(LPCSTR f)
{
  char drive[_MAX_DRIVE],
       dir[_MAX_DIR],
       file[_MAX_FNAME],
       ext[_MAX_EXT];
  CString st;
  _splitpath((char *)f,drive,dir,file,ext);
  st = ext;
  return st;
}

//-----------------------------------------------------------------------/
CString forceExtention(CString fn, CString newext)
{
  CString st = justPath(fn);
  st += justName(fn);
  st += newext;
  return st;
}

//-----------------------------------------------------------------------/
CString forceFileName(CString fn, CString newname)
{
  CString st = justPath(fn);
  st += newname;
  st += justExtention(fn);
  return st;
}

//----------------------------------------------------------------------------/
CString forcePath(CString fn, CString newpath)
{
  CString st = newpath;
  AddBackSlash(st);
  st += justFileName(fn);
  return st;
}

//----------------------------------------------------------------------------/
BOOL FileExists(CString fn)
{
	BOOL exists = FALSE;                   // assume failure
	if (!fn.IsEmpty())
	{
		int status = _access(fn,0);
		if (status == 0)
			exists = TRUE;
	}
	return exists;
}

//----------------------------------------------------------------------------/
int RenameFile(CString oldname, CString newname)
{
	int  status    = 0;
	BOOL oldexists = FileExists(oldname);
	BOOL newexists = FileExists(newname);

	if (oldexists && !newexists)
		CFile::Rename(oldname, newname);
	else
		status = -1;
	return status;
}

//----------------------------------------------------------------------------/
int DeleteFile(CString fname)
{
	int  status    = 0;
	if (FileExists(fname))
		CFile::Remove(fname);
	else
		status = -1;
	return status;
}

//----------------------------------------------------------------------------/
void AddBackSlash(CString& path)
{
	int length = path.GetLength();

	if (length > 1)
	{
		if (path.GetAt(length - 1) != '\\')
		{
			path += "\\";
		}
	}
	else if (1 == length)
	{
		CString temp = path;
		temp.MakeUpper();
		TCHAR ch = temp.GetAt(0); 

		if (ch >= 'A' && ch <= 'Z')
		{
			path += ":\\";
		}
		else if (ch != '\\')  // add a back slash anyway
		{
			path += "\\";
		}
	}
	else
	{
		path += "\\";
	}
}

CString RemoveBackSlash(CString path)
{
	if (path.Find('\\') == 0)
		return path;
	int len = path.GetLength();
	path = path.Left(len - 1);
	return path;
}

//-----------------------------------------------------------------------------/
CString GetProgramPath(BOOL bAndExeName/*=FALSE*/)
{
	TCHAR szFullPath[MAX_PATH];
	::GetModuleFileName(NULL, szFullPath, MAX_PATH);
	//CString path = justPath(szFullPath);
	CString path = szFullPath;
	if (!bAndExeName)
	{
		path = justPath(path);
		AddBackSlash(path);
	}
	return path;
}

GeneralRe: Everyone. Pin
John Uhlenbrock9-Jul-01 7:05
John Uhlenbrock9-Jul-01 7:05 
GeneralSimulate a carriage return Pin
RobJones5-Jul-01 10:12
RobJones5-Jul-01 10:12 
GeneralRe: Simulate a carriage return Pin
Christian Graus5-Jul-01 12:16
protectorChristian Graus5-Jul-01 12:16 
GeneralRe: Simulate a carriage return Pin
Julien5-Jul-01 13:34
Julien5-Jul-01 13:34 
GeneralRe: Simulate a carriage return Pin
CMFC6.0VS.NETUser6-Jul-01 4:49
CMFC6.0VS.NETUser6-Jul-01 4:49 
GeneralBeginner Question Pin
Dan Madden5-Jul-01 9:55
Dan Madden5-Jul-01 9:55 
GeneralRe: Beginner Question Pin
Pavlos Touboulidis5-Jul-01 10:29
Pavlos Touboulidis5-Jul-01 10:29 
GeneralRe: Doesn't Work Pin
Dan Madden5-Jul-01 17:38
Dan Madden5-Jul-01 17:38 
GeneralRe: Doesn't Work Pin
Mike Burston5-Jul-01 18:11
Mike Burston5-Jul-01 18:11 
GeneralRe: Doesn't Work Pin
Dan Madden5-Jul-01 18:25
Dan Madden5-Jul-01 18:25 
GeneralRe: Doesn't Work Pin
Mike Burston5-Jul-01 18:42
Mike Burston5-Jul-01 18:42 
GeneralRe: Doesn't Work Pin
Dan Madden5-Jul-01 19:20
Dan Madden5-Jul-01 19:20 
Generalpointer to a function Pin
5-Jul-01 9:13
suss5-Jul-01 9:13 
GeneralRe: pointer to a function Pin
Chris Losinger5-Jul-01 9:26
professionalChris Losinger5-Jul-01 9:26 
GeneralRe: pointer to a function Pin
KingsGambit5-Jul-01 18:00
KingsGambit5-Jul-01 18:00 
GeneralRe: pointer to a function Pin
5-Jul-01 18:28
suss5-Jul-01 18:28 
GeneralRe: pointer to a function Pin
8-Jul-01 11:03
suss8-Jul-01 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.