Jump to content
  • Advertisement
Sign in to follow this  

remove path from filename

This topic is 672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All


using the openfile dialog I get the full path to my file which is ok but I need to strip it down and remove the drive and initial dir folders from it the code I have so far


OPENFILENAME ofn;       // common dialog box structure
   char szFile[260];       // buffer for file name
   HWND hwnd = hwnd;// owner window
   HANDLE hf;              // file handle

   // Initialize OPENFILENAME
   ZeroMemory(&ofn, sizeof(ofn));
   ofn.lStructSize = sizeof(ofn);
   ofn.hwndOwner = hwnd;
   ofn.lpstrFile = szFile;
   // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
   // use the contents of szFile to initialize itself.
   ofn.lpstrFile[0] = '\0';
   ofn.nMaxFile = sizeof(szFile);
   ofn.lpstrFilter = "*.dds";
   ofn.nFilterIndex = 1;
   ofn.lpstrFileTitle = NULL;
   ofn.nMaxFileTitle = 0;
   ofn.lpstrInitialDir = MyArtDirectory;

   // Display the Open dialog box. 

   if (GetOpenFileName(&ofn) == TRUE)
        hf = CreateFile(ofn.lpstrFile,
        char real_filename[MAX_PATH] = { 0 };
        strncpy(real_filename, szFile + ofn.nFileOffset, ofn.nFileExtension - 1 - ofn.nFileOffset);
        MessageBox(NULL, real_filename, "File Chosen",  MB_OK);  //check we have got the string for the path 

I have tried some version the above will give me just the filename cool but I need also just some parts of the path ie everything except for the ofn.lpstrInitialDir as it point  to  the initial dir  I need to strip that from the szFile string is this possible ?


so for example instaed of d:\apps\myapp\art\dir1\dir2\my.dds I would like just to have dir1\dir2\my.dds


any help greatly appreciated

Edited by maori

Share this post

Link to post
Share on other sites

It looks like you are searching for PathRelativePathTo().


Be aware that it will fail under many circumstances.  The paths may not have anything in common.  There are also many ways to make paths so it may not have ever had a drive letter, such as the case of a UNC path or web addresses used as a path.

Share this post

Link to post
Share on other sites

Go backwards through the path until you hit a separator - everything after that is the file name.


char *fileNameWithoutPath;


for (int i = strlen (szFile) - 1; i; i--)


if (szFile == '\\')


fileNameWithoutPath = &szFile[i + 1];




Share this post

Link to post
Share on other sites
if (szFile == '\\')


Forward slash and backslash can be used interchangeably.  There are also other delimiters possible on Windows, including colons for drives, colons for dividers in service provider names, question marks, and even dots before a UNC device.


The days of a simple drive letter and backslashes have been dead for decades.


The function PathRelativePathTo() is going to give the best results since it adjusts for all the various path styles Windows supports.

Share this post

Link to post
Share on other sites
PathRelativePathTo is cool...thanks for that.
But I think the OP is looking for something like
#include "tchar.h"
#include <string>
int _tmain(int argc, _TCHAR* argv[])
std::string myArtDir = "d:/apps/myapp/art/";
std::string fullPath = "d:/apps/myapp/art/dir1/dir2/my.dds";
// pull the file path past the base directory
std::string subPath = fullPath.substr(myArtDir.length()/*start read position*/, 100/*max length of substring*/);
// and if you want just the extension for a hint of file type
std::string ext = fullPath.substr(fullPath.find_last_of('.') + 1);
return 0;

Using Frob's suggestion is interesting...
#include "tchar.h"
#include "Shlwapi.h"

#pragma comment(lib, "Shlwapi.lib")

int _tmain(int argc, _TCHAR* argv[])
	char path[_MAX_PATH] = "";
	char myArtDir[] = "d:\\apps\\myapp\\art"; // double backslash required format or fails
	char fullPath[] = "d:\\apps\\myapp\\art\\dir1\\dir2\\my.dds";
	if (!PathRelativePathTo(path, myArtDir, FILE_ATTRIBUTE_DIRECTORY, fullPath, FILE_ATTRIBUTE_NORMAL))
		MessageBox(0, "whoops", "", 0);
		MessageBox(0, path, "", 0);

	return 0;

// output : .\dir1\dir2\my.dds
but the output is slightly different with the leading ".\"

Share this post

Link to post
Share on other sites

Hi all


thanks for all the replies


I now have


std::string MyArtDir = ofn.lpstrInitialDir ;
         // need to remove the \\ from the above path as   well
         std::string del = "\\";
         // this is the full path to the file 
         std::string fullPath = szFile;

         // pull the file path past the base directory
         std::string subPath = fullPath.substr(MyArtDir.length() + del.length()/*start read position*/, 100/*max length of substring*/);

         const char *cstr = subPath.c_str();
         MessageBox(NULL ,cstr, "File Chosen",  MB_OK);  //check we have got the string for the path 



?very much appreciated :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!