remove path from filename

Started by
4 comments, last by maori 7 years, 2 months ago

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;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box.

if (GetOpenFileName(&ofn) == TRUE)
{
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL);
//TO DO STRIP THE baseDIR FROM THE PATH SO WE CAN APPEND THE THEATERS.1ST FILE
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

Advertisement

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.

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];

break;

}

}

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.

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);
	else
		MessageBox(0, path, "", 0);

	return 0;
}

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

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 :)

This topic is closed to new replies.

Advertisement