Jump to content

Fileinstall Helper


 Share

Recommended Posts

Hi,

Here's my FileInstall helper. The _SplitPath function was posted by ezzetabi. I just took out the array return bit.

One problem I have is with having to click on the Save button twice for the _SaveList function to be called. :D

AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("GUICoordMode", 1)
AutoItSetOption("GUINotifyMode", 1)

; Listbox constants
Global $LB_GETCURSEL = 0x188
Global $LB_DELETESTRING = 0x182
Global $LB_FINDSTRING = 0x18F
Global $LB_ADDSTRING = 0x180
Global $LB_GETCOUNT = 0x18B
Global $LB_GETTEXT = 0x189

; GUI controls
Global $ctllist, $cmdadd, $cmdremove, $cmdsave, $txtdestdir, $cmdbrowse

Local $msg, $filelist, $index

; Create dialog box and controls
GuiCreate("FileInstall Helper", 650, 380, -1, -1)
$ctllist = GUISetControl("list", "File List", 20, 20, 520, 320)
$cmdadd = GUISetControl("button", "Add", 550, 20, 80, 30)
$cmdremove = GUISetControl("button", "Remove", 550, 60, 80, 30)
$cmdsave = GUISetControl("button", "Save", 550, 100, 80, 30)
GuiSetControl("label", "Destination Directory: ", 20, 340, 120, 20)
$txtdestdir = GuiSetControl("input", "", 130, 335, 360, 25)
$cmdbrowse = GuiSetControl("button", "...", 500, 335, 40, 25)
GuiShow()

; Main message loop
While 1
    Sleep(100)
    $msg = GuiMsg(0)
    Select
    case $msg = $cmdadd
  $filelist = FileOpenDialog("Select Source File(s):", @ScriptDir, "All (*.*)", 7)
  _AddToList($filelist)
    case $msg = $cmdremove
  $index = GuiSendMsg($ctllist, $LB_GETCURSEL, 0, 0)
  If $index > -1 Then GuiSendMsg($ctllist, $LB_DELETESTRING, $index, 0)
    case $msg = $cmdbrowse
  GuiWrite($txtdestdir, 0, FileSelectFolder("Select Destination Directory:", "", 0))
    case $msg = $cmdsave
  _SaveList()
    Case $msg = -3
  ExitLoop
   EndSelect
WEnd
Exit

; Add all files to list
Func _AddToList($fileList)
    Local $files
    Local $n
    
    $files = StringSplit($filelist, "|")
    If $files[0] > 1 Then
  For $n = 2 To $files[0]
    _AddFileToList($files[1] & "\" & $files[$n])
  Next
    Else
  If $files[1] <> "" Then _AddFileToList($files[1])
    EndIf
EndFunc

; Add one file to list
Func _AddFileToList($file)
    Local $index

    $index = GuiSendMsg($ctllist, $LB_FINDSTRING, -1, $file)
    If $index = -1 Then GuiSendMsg($ctllist, $LB_ADDSTRING, 0, $file)
EndFunc

; Save FileInstall code to clipboard
Func _SaveList()
    Local $listcnt
    Local $file = "", $drive, $dir, $fname, $ext
    Local $code
    Local $destdir = GuiRead($txtdestdir)
    Local $n
    
    $listcnt = GuiSendMsg($ctllist, $LB_GETCOUNT, 0, 0)
    If $listcnt > 0 and $destdir <> "" Then
  For $n = 0 To $listcnt-1
    $file = GuiRecvMsg($ctllist, $LB_GETTEXT, $n, 1)
    $fname = ""
    _SplitPath($file, $drive, $dir, $fname, $ext)
    If $fname <> "" Then $code = $code & "FileInstall(""" & $file & """, """ & $destdir & "\" & $fname & $ext & """)" & @CRLF
  Next
  If $code <> "" Then 
    ClipPut($code)
    MsgBox(0, "Save To Clipboard", "FileInstall code saved to clipboard!")
  EndIf
    EndIf
EndFunc

; ===================================================================
; _SplitPath($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)
;
; Splits a path into the drive, directory, file name and file extension parts.  An empty string is set if a
;    part is missing.
; Parameters:        
;    $szPath - IN - The path to be split (Can contain a UNC server or drive letter)
;    $szDrive - OUT - String to hold the drive
;     $szDir - OUT - String to hold the directory
;     $szFName - OUT - String to hold the file name
;     $szExt - OUT - String to hold the file extension
; ===================================================================
Func _SplitPath($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)
; Set local strings to null (We use local strings in case one of the arguments is the same variable)
    Local $drive = ""
    Local $dir = ""
    Local $fname = ""
    Local $ext = ""
    Local $i, $pos ; For Opt("MustDeclareVars", 1)

; Get drive letter if present (Can be a UNC server)
    If StringMid($szPath, 2, 1) = ":" Then 
  $drive = StringLeft($szPath, 2)
      $szPath = StringTrimLeft($szPath, 2)
    ElseIf StringLeft($szPath, 2) = "\\" Then
      $szPath = StringTrimLeft($szPath, 2); Trim the \\
  $pos = StringInStr($szPath, "\")
      If $pos = 0 Then $pos = StringInStr($szPath, "/")
      If $pos = 0 Then
    $drive = "\\" & $szPath; Prepend the \\ we stripped earlier
    $szPath = ""; Set to null because the whole path was just the UNC server name
      Else
    $drive = "\\" & StringLeft($szPath, $pos - 1); Prepend the \\ we stripped earlier
    $szPath = StringTrimLeft($szPath, $pos - 1)
      EndIf
    EndIf
  
; Set the directory and file name if present
    For $i = StringLen($szPath) To 0 Step -1
      If StringMid($szPath, $i, 1) = "\" OR StringMid($szPath, $i, 1) = "/" Then
    $dir = StringLeft($szPath, $i)
    $fname = StringRight($szPath, StringLen($szPath) - $i)
    ExitLoop
      EndIf
    Next
  
; If $szDir wasn't set, then the whole path must just be a file, so set the filename
    If StringLen($dir) = 0 Then $fname = $szPath
  
; Check the filename for an extension and set it
    For $i = StringLen($fname) To 0 Step -1
      If StringMid($fname, $i, 1) = "." Then
    $ext = StringRight($fname, StringLen($fname) - ($i -1))
    $fname = StringLeft($fname, $i - 1)
    ExitLoop
      EndIf
    Next
  
; Set the strings to what we found
    $szDrive = $drive
    $szDir = $dir
    $szFName = $fname
    $szExt = $ext
    Return
EndFunc; _SplitPath()
Edited by pacman
Link to comment
Share on other sites

Oops...I thought I had played it safe by saying ezzetabi posted it rather wrote it. Sorry about that.

The reason why I removed the return array code is because I wasn't using it. I will happily put it back if you want your code to remain as you had originally written it.

Link to comment
Share on other sites

It can be useful if your script uses multiple FileInstalls, especially on sources from different locations. One reason why you would want to do this is if you are building a portable installer and you want to include all the source files with your script.

Link to comment
Share on other sites

oh, cool, so it builds the FileInstall("source","dest") part of a script... I can see how that'd save some time.... too bad it requires the GUI stuff and I'm staying away from the unstable builds... once the GUI's in a stable build, I'll definately use this...

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

oh, cool, so it builds the FileInstall("source","dest") part of a script...  I can see how that'd save some time....  too bad it requires the GUI stuff and I'm staying away from the unstable builds...  once the GUI's in a stable build, I'll definately use this...

Why do you avoid the unstable build so much? The way I see it, you're shooting yourself in the foot already. Maybe it isn't a good idea to use an unstable script in a work environment, but that doesn't mean you can't go ahead and write scripts now. Then when it is "stable", it's only a matter of making any minor changes and compiling it and you're almost immediately done with a the project and it can go into application as soon as AutoIt is stable, not two weeks later.

I'll never understand people who are afraid of "unstable" or "beta" software. I can spot just as many bugs in "final" software as I can in beta software. On top of that, being in a continual state of development, by using and finding bugs, they will be fixed a hell of a lot quicker than those bugs in a "stable" version. Besides, I can't count the number of times I've seen "stable" releases actually have more bugs than the previous beta's simply because the author "fixed" all the bugs and rushed the release out of the door in a worse state.

So, avoiding the unstable like the plague is not only NOT protecting you from bugs and quirks, it's actually making it take you that much longer to actually get something up and running when it finally does go "stable" since you could of used the unstable time to test and learn along the way.

I think that all of us test our code before we send it off to Jon. Depending on what it is and how easy it is to test determines how much I test something, but I usually try to think of ways to break my code as well. So, even though it's called "unstable", rarely is something actually put in there that hasn't been run a few times at the very least.

Link to comment
Share on other sites

I was just thinking about that myself, questioning my avoidance of the 'unstable' versions. I think it really came from Jon's suggestion to not use it in a production environment. I don't have much time for coding, really been spending too much time on it recently... that's part of it, I want to be able to immediately use what I do create. I could just avoid the gui parts and probably benifit from fixes to the 'stable' version... I would just avoid the more actively updated GUI stuff for the most part, at least for things that I want to push out to the users... I guess it's part of working at a power company, anything less than stable tends to be looked down at, but that's also why we're always behind on updating to new products... it's a double edged sword.

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

  • Administrators

I think that all of us test our code before we send it off to Jon.  Depending on what it is and how easy it is to test determines how much I test something, but I usually try to think of ways to break my code as well.  So, even though it's called "unstable", rarely is something actually put in there that hasn't been run a few times at the very least.

Except for the code I write of course :D
Link to comment
Share on other sites

so, I'm now trying out your nifty tool, running it now... got the following error:

---------------------------

AutoIt Error

---------------------------

Line 126 (File "S:\ITProcedures\autoit\fileinstall helper.au3"):

$pos = StringInStr($szPath, "\")

^ ERROR

Error: Variable used without being declared.

---------------------------

OK

---------------------------

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...