Jump to content

Help with code (newbie)


Go to solution Solved by Nine,

Recommended Posts

Hi.

First of all I'm not a programmer, when I need I try to do it my self and so far so good.

So after  search a lot I manage to creat a script that detects a specific removable device by label, creats a combo with drive letter and copy files by selection.

The first problem is if I have more than one device scrpt will show only one.

The second problem is that script will only detect removeble media that is already connected and would be a must if I insert removeble media and it was detected.

 

Can someone help?

 

 

Bootpack Installer.au3

Link to comment
Share on other sites

Hello and welcome to the forums :)

Quote

The first problem is if I have more than one device script will show only one.

So this is the code that you use for detecting devices (i cleaned it up a bit):

Global $ourDrive = ""
Global $aArray = DriveGetDrive("REMOVABLE")
If ($aArray = "") Then
    $Combo1 = GUICtrlCreateCombo("Despositivo BOOTPACK não encontrado", 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $aArray[0] To 1 Step -1
        If DriveGetLabel($aArray[$i]) = "BOOTPACK_V4" then
            $ourDrive = $aArray[$i]
            $Combo2 = GUICtrlCreateCombo(StringUpper($ourDrive), 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
            $letter = StringUpper($aArray[$i])
            GUICtrlSetData($Combo2, $letter)
            GUICtrlRead ($letter)
         EndIf
    Next
Endif

The problem comes from the fact that in your loop you replace $combo2 with a new combo instead of updating the existing combo with the new data. You need to check if one drive has already been detected or not. If not then you create the combo, if yes then you update the existing combo with the new letter. Here is an example of how you can do it:

Global $ourDrive = ""
Global $number_of_bootpack_drives = 0
Global $aArray = DriveGetDrive("REMOVABLE")
If ($aArray = "") Then
    $Combo1 = GUICtrlCreateCombo("Despositivo BOOTPACK não encontrado", 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $aArray[0] To 1 Step -1
        If DriveGetLabel($aArray[$i]) = "BOOTPACK_V4" then
            $letter = StringUpper($aArray[$i])
            if $number_of_bootpack_drives = 0 then
                ;we create the combo since it doesn't exists
                $Combo1 = GUICtrlCreateCombo(StringUpper($letter), 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            else
                ;if the combo already exists we update it to add the other drives letters
                GUICtrlSetData($Combo1, $letter) ; this will ADD $letter to the existing list of letters in the combo (it wont delete the existing letters in the list)
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            Endif
         EndIf
    Next
Endif

 

Quote

The second problem is that script will only detect removeble media that is already connected and would be a must if I insert removeble media and it was detected.

The easiest is to add a button called "scan drives again" (button4) and if  you click on that button it will do the above code again.

Edited by Neutro
typo
Link to comment
Share on other sites

As @Neutro showed you, you need to update the combo box properly.  I would suggest that you put all this code into a Func to make your code more modular.  But I must warn you that not all removable devices has a disk drive assigned to it.  For example, if you connect a smart phone, it will not be shown in the list of drives (DriveGetDrive).  Fortunately, you can query WMI to verify if a specific item is connected.

It is very easy to get notified when a removable device has been connected or removed.  The WM_DEVICECHANGE notification is exactly what you need to be informed of a change in the devices connected to your computer.

Link to comment
Share on other sites

46 minutes ago, Neutro said:

Hello and welcome to the forums :)

So this is the code that you use for detecting devices (i cleaned it up a bit):

Global $ourDrive = ""
Global $aArray = DriveGetDrive("REMOVABLE")
If ($aArray = "") Then
    $Combo1 = GUICtrlCreateCombo("Despositivo BOOTPACK não encontrado", 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $aArray[0] To 1 Step -1
        If DriveGetLabel($aArray[$i]) = "BOOTPACK_V4" then
            $ourDrive = $aArray[$i]
            $Combo2 = GUICtrlCreateCombo(StringUpper($ourDrive), 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
            $letter = StringUpper($aArray[$i])
            GUICtrlSetData($Combo2, $letter)
            GUICtrlRead ($letter)
         EndIf
    Next
Endif

The problem comes from the fact that in your loop your replace $combo2 with a new combo instead of updating the existing combo with the new data. You need to check if one drive has already been detected or not. If not then you create the combo, if yes then you update the existing combo with the new letter. Here is an example of how you can do it:

Global $ourDrive = ""
Global $number_of_bootpack_drives = 0
Global $aArray = DriveGetDrive("REMOVABLE")
If ($aArray = "") Then
    $Combo1 = GUICtrlCreateCombo("Despositivo BOOTPACK não encontrado", 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $aArray[0] To 1 Step -1
        If DriveGetLabel($aArray[$i]) = "BOOTPACK_V4" then
            $letter = StringUpper($aArray[$i])
            if $number_of_bootpack_drives = 0 then
                ;we create the combo since it doesn't exists
                $Combo1 = GUICtrlCreateCombo(StringUpper($letter), 16, 64, 289, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            else
                ;if the combo already exists we update it to add the other drives letters
                GUICtrlSetData($Combo1, $letter) ; this will ADD $letter to the existing list of letters in the combo (it wont delete the existing letters in the list)
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            Endif
         EndIf
    Next
Endif

 

The easiest is to add a button called "scan drives again" (button4) and if  you click on that button it will do the above code again.

Thank for help @Neutro, now is correctly detectig drives but still a small problem: variable $letter is always assigned to the first device found. In my test cas I've e J: And K: and if I selcet K: will always update J:

Link to comment
Share on other sites

You're welcome :)

You need to use the GUICtrlRead() function to read the current letter selected in your combo when you click on your buttons and assign it to $letter

Edited by Neutro
Link to comment
Share on other sites

35 minutes ago, Neutro said:

You're welcome :)

You need to use the GUICtrlRead() function to read the current letter selected in your combo when you click on your buttons and assign it to $letter

And how do I do that?

Link to comment
Share on other sites

  • Developers
15 minutes ago, FGAIFEN said:

I've tryed to implement but my knolege is limited

show what you tried that isn't working so we can help. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

27 minutes ago, Jos said:

show what you tried that isn't working so we can help. ;) 

Since I cound't understand how tou use GUICtrlRead to $letter i've tryed all possible combinations:

GUICtrlRead($aArray[$i], $letter)

GUICtrlRead($Combo1, $letter) , etc... 

 

Link to comment
Share on other sites

6 minutes ago, Jos said:

ah ...so you didn't try as stated. ;) 

As I said I'm not a programmer and I try to find a way to solve my needs. I understand part of the logic for what I need but now I'm confused and don't know ho to reach the objective. So if you can help I will be most thankfull.

Link to comment
Share on other sites

  • Solution

I am going to give you a trick.  After you have tried every single possible things and you are still failing, you open help file and you start reading...

As stated in help file for GUICtrlRead, it says there is 2 parameters : first is ControlId (in your case you want to read from a combobox, right ?).  Second parameter is advanced and there is only 2 possible choices (0 or 1).  Nothing else.  Then it says the result depends on the control (as already mentioned, you want to read from a combobox).  So it says for combo : the value selected (isn't that what you want ?)

So now what do you think, your statement should be ?

 

Link to comment
Share on other sites

29 minutes ago, Nine said:

I am going to give you a trick.  After you have tried every single possible things and you are still failing, you open help file and you start reading...

As stated in help file for GUICtrlRead, it says there is 2 parameters : first is ControlId (in your case you want to read from a combobox, right ?).  Second parameter is advanced and there is only 2 possible choices (0 or 1).  Nothing else.  Then it says the result depends on the control (as already mentioned, you want to read from a combobox).  So it says for combo : the value selected (isn't that what you want ?)

So now what do you think, your statement should be ?

 

I've tryed GUICtrlRead ($letter, 1) and  GUICtrlRead ($Combo1,1) as the last was what I understand from your explanation and still not working.

Link to comment
Share on other sites

38 minutes ago, FGAIFEN said:

I've tryed GUICtrlRead ($letter, 1) and  GUICtrlRead ($Combo1,1) as the last was what I understand from your explanation and still not working.

I'm so dumm.... So solution is "$letter = GUICtrlRead($combo1,1)" in every $BUTTON1 .....

Link to comment
Share on other sites

4 hours ago, FGAIFEN said:

As I said I'm not a programmer and I try to find a way to solve my needs. I understand part of the logic for what I need but now I'm confused and don't know ho to reach the objective. So if you can help I will be most thankfull.

You think that trying to find a way to solve your needs without understanding how to code properly will save you time but it wont.

It's like if you try to speak spanish just using a dictionnary and not trying to understand the syntax of the spanish language :)

Dont be afraid to learn "how to be a programmer". We were all at your place some time ago and we learned how to do it, it's not that hard :)

So i suggest when you have a bit of free time you go to https://www.autoitscript.com/wiki where in the "tutorials" section there are enough informations on learning how to code properly. 

It will take you less time to learn how to code properly this way rather than trying to solve your problems without understanding how things work :) 

Edited by Neutro
Link to comment
Share on other sites

On 4/23/2022 at 9:48 PM, Neutro said:

You think that trying to find a way to solve your needs without understanding how to code properly will save you time but it wont.

It's like if you try to speak spanish just using a dictionnary and not trying to understand the syntax of the spanish language :)

Dont be afraid to learn "how to be a programmer". We were all at your place some time ago and we learned how to do it, it's not that hard :)

So i suggest when you have a bit of free time you go to https://www.autoitscript.com/wiki where in the "tutorials" section there are enough informations on learning how to code properly. 

It will take you less time to learn how to code properly this way rather than trying to solve your problems without understanding how things work :) 

Yes I know that :) .

Sometimes the easy way is the hard way.

 

I've manage to complete both scripts I was working but I've to ask again for help.

The question is:  and about adding label?

I manage to "insert" label on it but the variable will always include it and copy process will be "drive letter" + "label" instead of "drive letter".

I'll upload code.

 

 

 

Bootpack Updater Administrador.au3

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <GuiButton.au3>
#include <MsgBoxConstants.au3>

#Region ### START Koda GUI section ### Form=E:\AutoIT\Bootpack Installer.kxf
$Form1 = GUICreate("BOOTPACK UPDATER v1.0 Administrador", 433, 285, 571, 339, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$Label1 = GUICtrlCreateLabel("Dispositivo BOOTPACK", 15, 40, 324, 24)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

Global $ourDrive = ""
Global $number_of_bootpack_drives = 0
Global $aArray = DriveGetDrive("REMOVABLE")
If ($aArray = "") Then
    $Combo1 = GUICtrlCreateCombo("Dispositivo BOOTPACK não encontrado", 16, 64, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $aArray[0] To 1 Step -1
        If DriveGetLabel($aArray[$i]) = "BOOTPACK_V4" then
            $letter = StringUpper($aArray[$i])
            if $number_of_bootpack_drives = 0 then
                ;we create the combo since it doesn't exists
                $Combo1 = GUICtrlCreateCombo(StringUpper($letter), 16, 64, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            else
                ;if the combo already exists we update it to add the other drives letters
                GUICtrlSetData($Combo1, $letter) ; this will ADD $letter to the existing list of letters in the combo (it wont delete the existing letters in the list)
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            Endif
         EndIf
      Next
Endif

$Label2 = GUICtrlCreateLabel("Disco de Destino", 15, 128, 324, 24)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$BUTTON1 = GUICtrlCreateButton("Começar", 16, 224, 129, 25)
$Button2 = GUICtrlCreateButton("Atualizar", 282, 62, 129, 25)
$Button3 = GUICtrlCreateButton("Sair", 282, 224, 129, 25)


Global $ourDrive = ""
Global $number_drives = 0
Global $bArray = DriveGetDrive("ALL")
If ($bArray = "") Then
    $Combo2 = GUICtrlCreateCombo("", 16, 152, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $bArray[0] To 1 Step -1
        $letter1 = StringUpper($bArray[$i])
        $label = DriveGetLabel($bArray[$i])
            if $number_drives = 0 then
                ;we create the combo since it doesn't exists
                $Combo2 = GUICtrlCreateCombo(StringUpper($letter1) & Consolewrite($label), 16, 152, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
                $number_drives = $number_drives + 1
            else
                ;if the combo already exists we update it to add the other drives letters
                GUICtrlSetData($Combo2, $letter1 & Consolewrite($label)) ; this will ADD $letter to the existing list of letters in the combo (it wont delete the existing letters in the list)
                $number_drives = $number_drives + 1
            Endif
      Next
Endif

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###


Dim $options, $dest, $source, $what, $prog, $runner
$options = '/R:1 /W:1'
$what = '/COPY:DAT /XJ /V /TEE /ETA /E /MIR'
$prog = 'RoboCopy.exe'


WHILE 1
$MSG=GUIGETMSG()
SWITCH $MSG
CASE $GUI_EVENT_CLOSE
EXIT


CASE $BUTTON1

$letter = GUICtrlRead($combo1,1)
$letter1 = GUICtrlRead($combo2,1)

$source = $letter & "\" & "boot"
$dest = $letter1 & "\" & "BOOTPACK_V4\Bootpack Base\boot"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "\" & "BootPack"
$dest = $letter1 & "\" & "BOOTPACK_V4\Bootpack Base\BootPack"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "\" & "EFI"
$dest = $letter1 & "\" & "BOOTPACK_V4\Bootpack Base\EFI"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

FileCopy($letter & "\" & "g2ldr.*", $letter1 & "BOOTPACK_V4\Bootpack Base", $FC_OVERWRITE)


$source = $letter & "\" & "Winsetup\DRVS"
$dest = $letter1 & "BOOTPACK_V4\SO's Base\DRVS"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "\" & "Winsetup\OEM BRAND"
$dest = $letter1 & "BOOTPACK_V4\SO's Base\OEM BRAND"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "\" & "Winsetup\PROGS"
$dest = $letter1 & "BOOTPACK_V4\SO's Base\PROGS"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "\" & "Winsetup\UPDTS"
$dest = $letter1 & "BOOTPACK_V4\SO's Base\UPDTS"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "Winsetup\Vista"
$dest = $letter1 & "BOOTPACK_V4\Vista"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "Winsetup\Vista64"
$dest = $letter1 & "BOOTPACK_V4\Vista64"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "Winsetup\WIN7"
$dest = $letter1 & "BOOTPACK_V4\WIN7"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "Winsetup\WIN81"
$dest = $letter1 & "BOOTPACK_V4\WIN81"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "Winsetup\W10"
$dest = $letter1 & "BOOTPACK_V4\W10"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)

$source = $letter & "Winsetup\W11"
$dest = $letter1 & "BOOTPACK_V4\W11"
$runner = $prog & ' "' & $source & '" "' & $dest & '" ' & $what & ' ' & $options
RunWait(@ComSpec & " /c" & $runner, "", @SW_MINIMIZE)


MsgBox($MB_SYSTEMMODAL, "", "Atualização concluida")


CASE $BUTTON2
GUIDelete ($Form1)
#Region ### START Koda GUI section ### Form=E:\AutoIT\Bootpack Installer.kxf
$Form1 = GUICreate("BOOTPACK UPDATER v1.0 Administrador", 433, 285, 571, 339, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$Label1 = GUICtrlCreateLabel("Dispositivo BOOTPACK", 15, 40, 324, 24)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

Global $ourDrive = ""
Global $number_of_bootpack_drives = 0
Global $aArray = DriveGetDrive("REMOVABLE")
If ($aArray = "") Then
    $Combo1 = GUICtrlCreateCombo("Dispositivo BOOTPACK não encontrado", 16, 64, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $aArray[0] To 1 Step -1
        If DriveGetLabel($aArray[$i]) = "BOOTPACK_V4" then
            $letter = StringUpper($aArray[$i])
            if $number_of_bootpack_drives = 0 then
                ;we create the combo since it doesn't exists
                $Combo1 = GUICtrlCreateCombo(StringUpper($letter), 16, 64, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            else
                ;if the combo already exists we update it to add the other drives letters
                GUICtrlSetData($Combo1, $letter) ; this will ADD $letter to the existing list of letters in the combo (it wont delete the existing letters in the list)
                $number_of_bootpack_drives = $number_of_bootpack_drives + 1
            Endif
         EndIf
      Next
Endif

$Label2 = GUICtrlCreateLabel("Disco de Destino", 15, 128, 324, 24)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$BUTTON1 = GUICtrlCreateButton("Começar", 16, 224, 129, 25)
$Button2 = GUICtrlCreateButton("Atualizar", 282, 62, 129, 25)
$Button3 = GUICtrlCreateButton("Sair", 282, 224, 129, 25)


Global $ourDrive = ""
Global $number_drives = 0
Global $bArray = DriveGetDrive("ALL")
If ($bArray = "") Then
    $Combo2 = GUICtrlCreateCombo("", 16, 152, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
Else
    For $i = $bArray[0] To 1 Step -1
        $letter1 = StringUpper($bArray[$i])
            if $number_drives = 0 then
                ;we create the combo since it doesn't exists
                $Combo2 = GUICtrlCreateCombo(StringUpper($letter1), 16, 152, 249, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
                $number_drives = $number_drives + 1
            else
                ;if the combo already exists we update it to add the other drives letters
                GUICtrlSetData($Combo2, $letter1) ; this will ADD $letter to the existing list of letters in the combo (it wont delete the existing letters in the list)
                $number_drives = $number_drives + 1
            Endif
      Next
Endif

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###


CASE $BUTTON3
Exit

ENDSWITCH


WEnd
Exit

 

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...