FileURL() Converts filepath to file URI

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

FileURL() Converts filepath to file URI

09 Dec 2018, 14:26

Code: Select all

FileURL( File ) {                                                ; By SKAN on D1CA @ goo.gl/UqXL6n
Local v, INTERNET_MAX_URL_LENGTH := 2048   
  VarSetCapacity(v,4200,0)
  DllCall( "Shlwapi.dll" ( SubStr(File,1,5)="file:" ? "\PathCreateFromUrl" : "\UrlCreateFromPath" )
         , "Str",File, "Str",v, "UIntP",INTERNET_MAX_URL_LENGTH, "UInt",0 )
Return v
}
Usage eg. MsgBox % FileURL( A_ScriptFullPath )
Note: Will also invert a file URI back to Filepath
Wikipedia: file URI scheme
My Scripts and Functions: V1  V2
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: FileURL() Converts filepath to file URI scheme

09 Dec 2018, 14:42

@SKAN, another gem. Thank you.
Tested file->URI and URI->file, both work perfectly, AHK_L 1.1.30.01, Win7 Pro 64-bit.
Regards,
burque505
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileURL() Converts filepath to file URI scheme

09 Dec 2018, 15:00

Thanks for testing burque505 :)

Quickly written/urgent requirement for ActiveX control. When doing document.write(html) and html links to a local javascript file,
an error is thrown unless file URI is used. eg: <script src='file:///myJavascript.js'></script>
Seemingly, fullpath is not required when local file is in A_WorkingDir
My Scripts and Functions: V1  V2
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: FileURL() Converts filepath to file URI scheme

09 Dec 2018, 15:03

Nice :-)

I have a fixuri() function in Lintalist (it does various things incl. parsing all fileurls in a block of html code for example and can work with relative urls ("..\") @ https://github.com/lintalist/lintalist/ ... FixURI.ahk (hard to follow perhaps out of context)

I think I would improve your FileUrl() function by adding support for relative paths and just filenames:

FileURL("test.txt") -> your result "file:test.txt" -> My suggested result "file://A_ScriptDir-or-WorkDir/test.txt"
FileURL("..\data\test.txt") -> your result "file:../test.txt" -> My suggested result "file://full-path-to/data/test.txt"

I know, garbage in garbage out, but it would avoid the need to pass on A_ScriptDir all the time for example.
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: FileURL() Converts filepath to file URI scheme

09 Dec 2018, 15:20

@SKAN, thank you
earlier I used stringreplace , with your script it's easier to convert back
example

Code: Select all

;-  TEST
f1=%a_scriptdir%\test55.txt
fx:=A_ScriptFullPath

;- earlier I made this :
stringreplace,c3,fx,\,/,all
Fn:="file:///" . c3

aa:=FileURL(fx)
fileappend,%aa%,%f1%
fileread,ec,%f1%

ab:=FileURL(ec)
filedelete,%f1%
msgbox,fn=%fn%`naa=%aa%`nab=%ab%
return

FileURL( File ) {                                                ; By SKAN on D1CA @ goo.gl/UqXL6n
Local v, INTERNET_MAX_URL_LENGTH := 2048   
  VarSetCapacity(v,4200,0)
  DllCall( "Shlwapi.dll" ( SubStr(File,1,5)="file:" ? "\PathCreateFromUrl" : "\UrlCreateFromPath" )
         , "Str",File, "Str",v, "UIntP",INTERNET_MAX_URL_LENGTH, "UInt",0 )
Return v
}
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileURL() Converts filepath to file URI scheme

09 Dec 2018, 15:34

Thanks garry :)

@list
To convert a relative path to full path + make sure file exists, I would do as follows:

Code: Select all

uglyPath := A_AhkPath "\..\license.txt"

Loop, Files, %uglypath%
 prettyPath := A_LoopFileLongPath

MsgBox % uglyPath "`n" prettyPath
My Scripts and Functions: V1  V2

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Lamron750 and 110 guests