Page 1 of 1

Extract file that was added with fileinstall

Posted: 11 Dec 2017, 21:02
by BGM
In my script I have used fileinstall to store a text file.
I know that if that file doesn't exist when the program starts, that it will get extracted.

But is there a way to get the file whilst the script is running? Is there a command to extract it?

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 15:20
by teadrinker
Hi, BGM,
When the compiled script is executed and the same "FileInstall" command is reached, the file is then extracted to Dest.
thus:

Code: Select all

MyFile := "C:\test.txt"
Dest := A_Desktop . "\test.txt"

Loop 10  {
   ToolTip Some work here %A_Index%
   Sleep, 300
}
MsgBox, The file will be extracted now!
FileInstall, C:\test.txt, % Dest
Run, % Dest

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 18:11
by BGM
Hi, teadrinker,
Yes, I know the file is extracted then - I was wondering if there was a way to make the running, compiled script extract it if it doesn't exist. In other words, say the user deletes the file when the script is running. I want the script to check that it exists, and if it doesn't - THEN - extract it again right then so it can be used.

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 18:40
by teadrinker
Maybe, you need something like this:

Code: Select all

myFile := "C:\test.txt"
dest := A_Desktop . "\test.txt"

FileInstall, C:\test.txt, % dest  ; C:\test.txt — myFile
FileDelete, % dest

Loop 10  {
   ToolTip Some work here %A_Index%
   Sleep, 300
}

if A_IsCompiled
   ExtractFile(myFile, dest)  ; myFile — (C:\test.txt) is the name of the resource
Run % dest

ExtractFile(resourceName, dest)  {
   if !hRes := DllCall("FindResource", Ptr, 0, Str, resourceName, Str, RT_RCDATA := "#10", Ptr)  {
      MsgBox, FindResource is failed, error: %A_LastError%
      ExitApp
   }
   hData  := DllCall("LoadResource", Ptr, 0, Ptr, hRes, Ptr)
   szData := DllCall("SizeofResource", Ptr, 0, Ptr, hRes)
   pData  := DllCall("LockResource", Ptr, hData, Ptr)
   oFile := FileOpen(dest, "w")
   oFile.Pos := 0
   oFile.RawWrite(pData + 0, szData)
   oFile.Length := szData
   oFile.Close()
}

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 19:45
by BGM
Strange, this works like a charm (exactly what I was looking for! Thank you very much!) - *if* the resource is not in the same directory as the compiled script.. ?

Here are the lines at the top:

Code: Select all

myFile := a_scriptdir . "\test.txt"
dest := a_scriptdir . "\test.txt"

FileInstall, test.txt, % dest  ; C:\test.txt — myFile
FileDelete, % dest
So, I have test.txt in the same directory as the exe. So I compile the script, then it deletes the file, then I get this:

Code: Select all

FindResource is failed, error: 1814
Now, if I use c:\test.txt for both paths, it works fine.

Any clue here?

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 19:48
by BGM
Ah - if I remove a_scriptdir, then it works.

Thanks, again, teadrinker! You would think this function would be a built-in command to accompany fileinstall.

I'll remember you in my Rosary!

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 20:00
by teadrinker
Glad to help, cheers!

Re: Extract file that was added with fileinstall

Posted: 12 Dec 2017, 21:53
by BGM