Page 1 of 1

Convert filenames DOS <-> WIN

Posted: Fri Oct 17, 2003 2:31 pm
by dige
.. maybe its useful for someone:

Windows longfile name to DOS 8.3:

Code: Select all

Procedure.s GetShortFileName ( Long.s )
  Short.s = Long
  getshortpathname_ ( @Long, @Short, Len(Short) )
  ProcedureReturn Short
EndProcedure
Get long windows filename from existing DOS filename:

Code: Select all

Procedure.s GetLongFileName ( Short.s )
  Res = FindFirstFile_ ( @Short, FD.WIN32_FIND_DATA )
  If Res
    ProcedureReturn PeekS( @FD\cFileName[0], 255)
  EndIf
  ProcedureReturn ""
EndProcedure
Example:

Code: Select all

 Debug GetShortFileName ( "C:\Temp\very long file name.txt" )
 Debug GetLongFileName ( "C:\Temp\VERYLO~1.TXT" )
cya dige

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 11:48 am
by Kwai chang caine
Thanks for this tips, even six years later...it's usefull :lol:
I know the "getshortpathname_" API, but i don't know "GetLongFileName" 8)

But i have again a problem.
The API "getshortpathname" works only if the file or the path exist :cry:

Have you another tips for convert in shortname a longname, even if the file is not here ???? :roll:

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 12:53 pm
by Fangbeast
Try this hint from pb (coder, not compiler)

short$=Space(999)
GetShortPathName_("C:\Program Files\Internet Explorer\",@short$,999)
Debug short$

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 1:58 pm
by Kwai chang caine
Thanks FANGBEAT to your answer 8)

But my problem is if the path and/or the file not exist and i want to convert the futur path to shortname :roll:
Your code don't works in this case, try this code...it return a empty line :(
This API only works if the path exist :(

Code: Select all

short$=Space(999)
GetShortPathName_("C:\Path not exist\File Not Exist.exe",@short$,999)
Debug short$

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 2:56 pm
by UserOfPure
Why would you need to convert a long name to a short name, if the long name doesn't exist? If it doesn't exist, you can't work with it anyway...

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 4:07 pm
by Kwai chang caine
Hello UserOfPure :D

Yes because i run sometimes application with line command, like the compiler of pure..and i want to be sure that the path is good understanding by the "DOS" program

Like this the path of the futur EXE not exist because it's PB compiler who create it :roll:

Code: Select all

ShortPathRepExeAInstaller$ = Space(#MAX_PATH) 
GetShortPathName_(PathExe, @ShortPathRepExeAInstaller$, #MAX_PATH)

ShortPathSource$ = Space(#MAX_PATH) 
GetShortPathName_(PathSource$, @ShortPathSource$, #MAX_PATH)
 
ShortPathCompilateur$ = Space(#MAX_PATH) 
GetShortPathName_(PathOfPb$ + "Compilers\PBCompiler.exe", @ShortPathCompilateur$, #MAX_PATH)
  
RunProgram(ShortPathCompilateur$, ShortPathSource$ + " /EXE " + Chr(34) + ShortPathRepExeAInstaller$ + NomExe + ".exe" + Chr(34), "", #PB_Program_Wait)

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 4:59 pm
by Fred
I don't understand your shortpath thinggie. All apps on Windows works with long filename as the API accepts them. Commandline programs are differents than DOS programs.

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 5:24 pm
by Kwai chang caine
Hello FRED, i'm very happy to call to you.

I have two problem with the command line.
This one, because i give long names with [, and space and other specials characters, and your compiler don't understand it.
So i say to me...it's not important, i convert it in short name, like this your nice compiler understand my path.
But the problem is with the futur name of the exe, because he don't exist again.

I ensure you that i have several problem with my long long name, and i have no problem when i convert it in shortname :shock:

And the second problem, is linked, and the same subject, is on the other post, with the constant #PB_Editor_CreateExecutable
I have loose two hours for found that the compiler not want this constant :shock:
http://www.purebasic.fr/english/viewtop ... 00#p309900

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 9:39 pm
by Fangbeast
Kwaï chang caïne wrote:Thanks FANGBEAT to your answer 8)

But my problem is if the path and/or the file not exist and i want to convert the futur path to shortname :roll:
Your code don't works in this case, try this code...it return a empty line :(
This API only works if the path exist :(

Code: Select all

short$=Space(999)
GetShortPathName_("C:\Path not exist\File Not Exist.exe",@short$,999)
Debug short$
Sorry KCC, wasn't thinking. The API only was for the path without a filename.

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 11:22 pm
by Kwai chang caine
So it's a pity, i must create this function myself :wink:
Thanks for your answer 8)

Re: Convert filenames DOS <-> WIN

Posted: Wed Dec 23, 2009 11:53 pm
by ts-soft
Don't use shortpath's, this will not work on all PCs, this feature is disabled on many PCs!
Use #DQUOTE$ for long paths with spaces!

greetings
Thomas

Re: Convert filenames DOS <-> WIN

Posted: Thu Dec 24, 2009 9:53 am
by Kwai chang caine
Thanks TsSoft

You say this format "MESDOC~1\PROGRA~1" is not use now, in the new PC :shock:
Have i good understand ???? :roll:

#DQUOTE$ i don't know that :oops:
If i good understand two, when i put #DQUOTE$ in the beginning and endig of a sentence...the compiler take the spaces and specials characters like this ??

Code: Select all

#DQUOTE$ + "E:\MES DOCUMENTS\Program Files\Programme v1.0 du 091223\[Sources]" + #DQUOTE$
I try to convert because, some characters is difficult to understand for any program, like the '[]" or other :(
So with your tips of #DQUOTE$, you think that all the specials characters is recognized in line command ?? :roll: