Convert filenames DOS <-> WIN

Share your advanced PureBasic knowledge/code with the community.
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Convert filenames DOS <-> WIN

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert filenames DOS <-> WIN

Post 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:
ImageThe happiness is a road...
Not a destination
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert filenames DOS <-> WIN

Post by Fangbeast »

Try this hint from pb (coder, not compiler)

short$=Space(999)
GetShortPathName_("C:\Program Files\Internet Explorer\",@short$,999)
Debug short$
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert filenames DOS <-> WIN

Post 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$
ImageThe happiness is a road...
Not a destination
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Convert filenames DOS <-> WIN

Post 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...
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert filenames DOS <-> WIN

Post 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)
ImageThe happiness is a road...
Not a destination
Fred
Administrator
Administrator
Posts: 18249
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Convert filenames DOS <-> WIN

Post 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.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert filenames DOS <-> WIN

Post 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
ImageThe happiness is a road...
Not a destination
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert filenames DOS <-> WIN

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert filenames DOS <-> WIN

Post by Kwai chang caine »

So it's a pity, i must create this function myself :wink:
Thanks for your answer 8)
ImageThe happiness is a road...
Not a destination
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Convert filenames DOS <-> WIN

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Convert filenames DOS <-> WIN

Post 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:
ImageThe happiness is a road...
Not a destination
Post Reply