Advertisement

Short To Long Filename

Started by August 11, 2000 02:25 PM
3 comments, last by daedalusd 24 years, 5 months ago
Hey all, Quick question, basically i want to convert the filename part of a path+filename (which is a short filename) to a path+long filename. I wrote a piece of code to do this (show below), but is there a better way to do it, or is this good enough ?? (I could only find functions for long to short, not short to long) (Please no flames about how crap this is, I''m relatively new to string manipulation in c++, without using things like the CString class which I can''t use in this case) Thanks in advance
    
	//Convert To A Long Filename

	char CmdPath[2048];
	ZeroMemory(CmdPath,2048);
	strcpy(CmdPath,strCmdLine); 
	while (CmdPath[strlen(CmdPath)-1] !=  ''\\'' )
	{
		ZeroMemory(CmdLine,2048);
		strncpy(CmdLine,CmdPath,strlen(CmdPath)-1);
		ZeroMemory(CmdPath,2048);
		strncpy(CmdPath,CmdLine,strlen(CmdLine));
	}
	ZeroMemory(CmdLine,2048);
	WIN32_FIND_DATA FindFileData;
	ZeroMemory(&FindFileData,sizeof(WIN32_FIND_DATA));
	HANDLE FindHandle = FindFirstFile(strCmdLine, &FindFileData); 
	strcpy(CmdLine,&FindFileData.cFileName[0]); 
	strcpy(&CmdPath[strlen(CmdPath)],CmdLine);
	ZeroMemory(CmdLine,2048);
	strcpy(CmdLine,CmdPath);
	FindClose(FindHandle);
	//Conversion Finished

    
hi

well I'm not sure about the short to long file name bit, but to get just the path information from a string you can just do this:

        int i;char CmdPath[2048];for(i=strlen(strCmdLine)-1; strCmdLine<i> != '\\'; i--){     if(i<0) CmdPath[0] = '\0'; // Or throw exception or whatever}strncpy(CmdPath, strCmdLine, i+1);        


A good bit simpler. All those ZeroMemory calls are a bit unnecessary and expensive. I'd cut them out.

ro

Edited by - rowbot on August 11, 2000 6:17:51 PM
Advertisement
Arghh, thats so damn obvious too =) Thanks for pointing out my stupidity =)

Thanks again man

Adam
Actually you better put this line after the code above

    CmdPath[i+1] = ''\0'';    


just in case the char after the end of your path string isnt 0 already.

What is it youre trying to do exactly?

ro
The Null character should already be on the end of the Filename returned by FindFirstFile so I shouldn''t need to add a Termination Char.

Re: What am i trying to do: Well basically, i need the long filename of a short filename passed.

FindFirstFile returns the Long Filename, but only the filename itself (with no path), so I basically have to remove the old Short filename from the end of the string passed, and append the Filename returned from FindFirstFile onto that path. If there is a simple API call to turn a short path + filename into a long path + filename, then I could use that, but for now, the only thing I have found which does convert short to long is FindFirstFile. (It doesn''t matter if the path name is left in it''s short form (i.e MyDire~1) but it''s vital that the filename is in it''s long form (i.e Testin~1.dat MUST be Testingfile.dat)

The code i posted (and also with you''re modifications) works fine on all Win9X Platforms and Win2k (haven''t tested FindFirstFile on NT platforms, but it should work the same way according to the docs), I was really just wondering if there was a better way to convert a short filename to a long one.

The current code successfully converts something like

c:\temp\mydire~1\testin~4.dat

into

c:\temp\mydire~1\testingfile.dat

which is what i needed to do =)

Thanks again

Adam

This topic is closed to new replies.

Advertisement