Extract file that was added with fileinstall

Get help with using AutoHotkey and its commands and hotkeys
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Extract file that was added with fileinstall

11 Dec 2017, 21:02

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?
teadrinker
Posts: 909
Joined: 29 Mar 2015, 09:41
Contact:

Re: Extract file that was added with fileinstall

12 Dec 2017, 15:20

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
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Extract file that was added with fileinstall

12 Dec 2017, 18:11

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.
teadrinker
Posts: 909
Joined: 29 Mar 2015, 09:41
Contact:

Re: Extract file that was added with fileinstall

12 Dec 2017, 18:40

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()
}
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Extract file that was added with fileinstall

12 Dec 2017, 19:45

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?
User avatar
BGM
Posts: 464
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: Extract file that was added with fileinstall

12 Dec 2017, 19:48

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!

Return to “Ask For Help”

Who is online

Users browsing this forum: deten and 98 guests