Jump to content

Add character to string if character does not exist


Recommended Posts

I'm trying to do a check if a \ exist at the end of a string, and if not then add the \ at the end of that string.

To do this, i'm using to folowing code:

If GUICtrlRead($dFolder) <> "" And StringRight(GUICtrlRead($dFolder), 1) <> "\" Then
$dFolder = GUICtrlRead($dFolder) & "\"
EndIf

$dFolder (destination folder) is a GUICtrlCreateInput control.

The weird thing is, that when $dFolder is empty, $dFolder becomes 11 and.

Also, when you enter the destination folder the first time, the script works like a charm.

But when you cancel your action and change the folder, the new value is not show. It still uses the old value.

Anyone an idea what I'm doing wrong? Beneath you'll see the complete script (the text in this script is dutch):

#include <GUIConstantsEx.au3>
#include <Process.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <ComboConstants.au3>

$Admin = GUICreate("Admininistrator", 380, 280, -1, -1, $WS_CAPTION)
GUICtrlCreatePic("image001.jpg",0,0,528,100)
$doImage = GUICtrlCreateButton("Maak image", 5, 250, 100)
$restartbtn = GUICtrlCreateButton("Afsluiten", 275, 250, 100)
$cCMD = GUICtrlCreateButton("Open Command Prompt", 115, 250, 150)
GUICtrlSetState($doImage, $GUI_DISABLE)
GUICtrlSetState($restartbtn, $GUI_FOCUS) ;zet de focus op Exit de knop
GUICtrlCreateLabel("Hieronder kun aangeven welke schijf gebruikt moet" & @CRLF & "worden voor de image en waar deze naartoe weg-" & @CRLF & "geschreven moet worden.", 5, 110)
GUICtrlCreateLabel("Bron schijf:", 5, 160)
GUICtrlCreateLabel("Doel schijf:", 115, 160)
GUICtrlCreateLabel("Map (optioneel):", 5, 205)
$dFolder = GUICtrlCreateInput("", 5, 222, 210)

Global $ip = "localhost"

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")
$colItems = $objWMIService.ExecQuery("SELECT Name, VolumeName FROM Win32_LogicalDisk", "WQL", 0x30)

If IsObj($colItems) Then
    $label = ""
    $dropList = ""
    For $objItem In $colItems
        $Label = $Label & $objItem.Name & " -> " & $objItem.VolumeName & @CRLF
        $dropList = $dropList & $objItem.Name & "|"
    Next
EndIf

GUICtrlCreateLabel("Beschikbare schijven:" & @CRLF & "-----------------------------------", 264, 110)
GUICtrlCreateLabel($Label, 265, 140)
$sList = GUICtrlCreateCombo("", 5, 177, 100, 0, BitOR($WS_VSCROLL, $CBS_DROPDOWNLIST))
GUICtrlSetData($sList, $dropList)
$dList = GUICtrlCreateCombo("", 115, 177, 100, 0, BitOR($WS_VSCROLL, $CBS_DROPDOWNLIST))
GUICtrlSetData($dList, $dropList)
GUICtrlSetState($dList, $GUI_DISABLE)
GUISetState(@SW_SHOW)

While 1
$msg = GUIGetMsg()  

Select
    Case $msg = $GUI_EVENT_CLOSE
        GUIDelete()
        ExitLoop
    Case $Msg = $sList
        If GUICtrlRead($Msg) <> "" Then GUICtrlSetState($dList, BitOR($GUI_ENABLE, $GUI_FOCUS))
    Case $Msg = $dList
        If GUICtrlRead($Msg) <> "" Then GUICtrlSetState($doImage, $GUI_ENABLE)
    Case $msg = $doImage
        If GUICtrlRead($dFolder) <> "" And StringRight(GUICtrlRead($dFolder), 1) <> "\" Then
            $dFolder = GUICtrlRead($dFolder) & "\"
        EndIf
        $doCont = MsgBox(289, "Te verzenden opdracht:", "imagex /compress fast /check flags ""Professional"" /capture " & GUICtrlRead($sList) & " " & GUICtrlRead($dList) & "\" & $dFolder & "install.wim ""Windows 7 Professional"" ""Windows 7 Professional Custom""")
        Select
            Case $doCont = 1
                ;Run("imagex /compress fast /check flags ""Professional"" /capture " & GUICtrlRead($sList) & " " & GUICtrlRead($dList) & "\" & $dFolder & "install.wim ""Windows 7 Professional"" ""Windows 7 Professional Custom""", @SCRIPTDIR)
                GUIDelete()
                ExitLoop
        EndSelect
    Case $msg = $restartbtn
        GUIDelete()
        ExitLoop
    Case $msg = $cCMD
        Run("cmd", @ScriptDir)
EndSelect
WEnd
Edited by Maikey81
Link to comment
Share on other sites

Hi Maikey,

try:

GuiCtrlSetData($dFolder, GUICtrlRead($dFolder) & "\")

With assigning the string to the $dFolder variable, you're destroying the handle to the input field.

Hi again Hannes,

Your script didn't work. It doesn't change the value of $dFolder at all.

The one's keep comming and now the $dFolder value stays 11, no matter what is entered in the input field.

Link to comment
Share on other sites

Already fixed it :mellow:

Your tip (that i was destroying the handle) made me think.

I did a:

Local $dxFolder

Since the script runs in a function.

Then I changed the following:

If GUICtrlRead($dFolder) <> "" And StringRight(GUICtrlRead($dFolder), 1) <> "\" Then
            $dxFolder = GUICtrlRead($dFolder) & "\"

And changed the the following $dFolder attributes to $dxFolder

That solved the problem with the 1's and the issue that folder name could be entered just ones.

Thanks again!

Link to comment
Share on other sites

If GUICtrlRead($dFolder) <> "" And StringRight(GUICtrlRead($dFolder), 1) <> "\" Then
    GUICtrlSetData($dFolder, GUICtrlRead($dFolder) & "\")
EndIf
$doCont = MsgBox(289, "Te verzenden opdracht:", "imagex /compress fast /check flags ""Professional"" /capture " & GUICtrlRead($sList) & " " & GUICtrlRead($dList) & "\" & GUICtrlRead($dFolder) & "install.wim ""Windows 7 Professional"" ""Windows 7 Professional Custom""")

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

If GUICtrlRead($dFolder) <> "" And StringRight(GUICtrlRead($dFolder), 1) <> "\" Then
    GUICtrlSetData($dFolder, GUICtrlRead($dFolder) & "\")
EndIf
$doCont = MsgBox(289, "Te verzenden opdracht:", "imagex /compress fast /check flags ""Professional"" /capture " & GUICtrlRead($sList) & " " & GUICtrlRead($dList) & "\" & GUICtrlRead($dFolder) & "install.wim ""Windows 7 Professional"" ""Windows 7 Professional Custom""")

That works even better :mellow:

Thanks Hannes!

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