// JP opened flex table

Click to See Complete Forum and Search --> : Filenames from Path string with multiselect


deck42
July 15th, 2008, 07:28 PM
Guys short question:

If I get a path string by an OpenFileDialog with multiselect on, is there any standard function that will read out each filename from the string?
I know that they are "\0" separated with OFN_EXPLORER on, but I thought I would rather not do it "by hand".

_shubhi
July 16th, 2008, 03:08 AM
Try the solution mentioned here Get filenames from "multiselect" openFileDialog (http://www.codeguru.com/forum/archive/index.php/t-391612.html)

deck42
July 18th, 2008, 03:54 AM
Well thanks but I am not using openFileDialog (since it is an MFC or .NET class if I get it right) but GetOpenFilenName which will return you just one big string and no array.

I really would appreciate some help here, since I just don't get how I am meant to handle that string. My problems:

-How do I know which "0" is the really last one in the Path string, if all file names are separated by "0"
-How do I separate them by those "0"s if all the string/char functions think that a "0" is the "terminator".
-What happens if I got a "0" in a file name or directory name - that's not the same as zero value right?

How does this work?

deck42
July 18th, 2008, 02:52 PM
If anyone should ever have the same problem, I solved it like this:


OPENFILENAME ofn;
char Path[MY_MAX_PATH]; //Buffer for the ofn structure - after GetOpenFileName(&ofn) the ofn structure will store the result in here.
ZeroMemory(&ofn, sizeof(ofn)); //Zero fill the memory, otherwise some functions (like GetOpenFileName) will be unhappy
ZeroMemory(Path, MY_MAX_PATH);

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hDlg;
ofn.lpstrFilter = "All Files (*.*)\0*.*\0";
ofn.lpstrFile = Path; //Pointer of the ofn structure is set to our buffer.
ofn.nMaxFile = MY_MAX_PATH;
ofn.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

if(GetOpenFileName(&ofn))
{
char* NextFileName = Path;
char tempFile[MAX_PATH];
if(*(Path + strlen(Path) + 1) == 0) //If multiselect was used, Path is a 0 seperated list of dir and filenames. "strlen" assumes that Path is terminated by 0. So "strlen(Path)+1" is the first char after the first 0 in Path. If this is 0 again, there was no multiselect. Otherwise there comes the next filename.
{ //Just one file was selected, so handle Path like one connected path string:
//Add code to handle "Path" here
}
else
{//Multiple files were selected so extract them. Again "strlen"'s behaviour to assume string is 0 terminated will be used here.
NextFileName = NextFileName + strlen(NextFileName) + 1; //Do first iteration already to jump to first filename and skip dir
while(*NextFileName!=0)
{
strcpy_s(tempFile, sizeof(tempFile), Path);
StringCbCat(tempFile, sizeof(tempFile), "\\\0");
StringCbCat(tempFile, sizeof(tempFile), NextFileName);

//Add code to handle "tempFile" here

NextFileName = NextFileName + strlen(NextFileName) + 1;
}
}
}

MrViggy
July 18th, 2008, 04:31 PM
-How do I know which "0" is the really last one in the Path string, if all file names are separated by "0"
You've already solved the mystery, but the MSDN docs for the OPENFILENAME (http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx) structure state:
If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the file names of the selected files. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name.
So, the end of the string has two terminating NULL characters.

Viggy

//JP added flex table