Jump to content

locate and use selected part of a string with an variable.


Recommended Posts

The function below adds mapped drives, i would like to ping the device before mapping to check its exists first.

So what I would like to do is somehow get just the first part of the string e.g. the \\device_name\ how can this be done I been messing with various string functions but in this I am baffled.

as an example: _mapdrive("q:",\\server1\quickbooks) so I would want just the "\\server1" to be put into a variable say $var for use with the ping($var,25) function.

Func _mapdrive($DriveLtr, $DrivePath)
DriveMapAdd($DriveLtr, $DrivePath, 1)
Switch @error
  Case 1
   MsgBox(16, "O.M.G Error", "An unknown error occured on " & $DrivePath & " trying to be mapped as local drive " & $DriveLtr & " maybe end device is not avilable for mapping")
  Case 2
   MsgBox(16, "Access Error", "Access to the remote share " & $DrivePath & " was denied")
  Case 3
   MsgBox(64, "Map Drive Error", "The device/drive " & $DriveLtr & " is already assigned and will be deleted")
  Case 4
   MsgBox(16, "Device Error", "Invalid device " & $DriveLtr & " name")
  Case 5
   MsgBox(16, "Connect to Remote Share Error", "Invalid remote share :" & $DrivePath)
  Case 6
   MsgBox(16, "Password Error", "Invalid password")
  Case Else
   ;MsgBox(64, "Completed!", "Mapped " & $DriveLtr & " to share " & $DrivePath)
   Sleep(500)
EndSwitch
EndFunc   ;==>_mapdrive
Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Link to comment
Share on other sites

Hi,

!! Beware of Firewall. Check ping visibility beforehand !!

Otherwise you could check on an opened port with TCPConnect.

!! You may also need Full Qualified Domain Name to ping server or map server share !!

1) Add: #include <string.au3> at beginning of script.

2) New Function _mapdrive

Func _mapdrive($DriveLtr, $DrivePath)
    If Ping (_Stringbetween ($DrivePath, "\\", "\")) Then
        DriveMapAdd($DriveLtr, $DrivePath, 1)
        Switch @error
          Case 1
           MsgBox(16, "O.M.G Error", "An unknown error occured on " & $DrivePath & " trying to be mapped as local drive " & $DriveLtr & " maybe end device is not avilable for mapping")
          Case 2
           MsgBox(16, "Access Error", "Access to the remote share " & $DrivePath & " was denied")
          Case 3
           MsgBox(64, "Map Drive Error", "The device/drive " & $DriveLtr & " is already assigned and will be deleted")
          Case 4
           MsgBox(16, "Device Error", "Invalid device " & $DriveLtr & " name")
          Case 5
           MsgBox(16, "Connect to Remote Share Error", "Invalid remote share :" & $DrivePath)
          Case 6
           MsgBox(16, "Password Error", "Invalid password")
          Case Else
           ;MsgBox(64, "Completed!", "Mapped " & $DriveLtr & " to share " & $DrivePath)
           Sleep(500)
        EndSwitch
    Else
        MsgBox (16, "Server Error", "Server not pingable!")
    EndIf
EndFunc   ;==>_mapdrive

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

you are officially my hero.. Posted Image, that alone has shown me allot, unfortunately seems this has not worked, looks like for some reason _Stringbetween is not seeing anything? I done a check by doing this: the variables are correct, I've had a look at the _StringBetween and cannot see anything wrong not that that means anything, so maybe I have misunderstood something.

Func _mapdrive($DriveLtr, $DrivePath)
$var_DrivePath = _StringBetween($DrivePath, "\\", "\")
MsgBox(16, "Device " & $var_DrivePath, $DriveLtr & $DrivePath)
Exit
If Ping($var_DrivePath, 25) Then
  DriveMapAdd($DriveLtr, $DrivePath, 1)
  Switch @error
   Case 1
    MsgBox(16, "O.M.G Error", "An unknown error occured on " & $DrivePath & " trying to be mapped as local drive " & $DriveLtr & " maybe end device is not avilable for mapping")
   Case 2
    MsgBox(16, "Access Error", "Access to the remote share " & $DrivePath & " was denied")
   Case 3
    MsgBox(64, "Map Drive Error", "The device/drive " & $DriveLtr & " is already assigned and will be deleted")
   Case 4
    MsgBox(16, "Device Error", "Invalid device " & $DriveLtr & " name")
   Case 5
    MsgBox(16, "Connect to Remote Share Error", "Invalid remote share :" & $DrivePath)
   Case 6
    MsgBox(16, "Password Error", "Invalid password")
   Case Else
    ;MsgBox(64, "Completed!", "Mapped " & $DriveLtr & " to share " & $DrivePath)
    Sleep(500)
  EndSwitch
Else
  $var = _StringBetween($DrivePath, "\\", "\")
  MsgBox(16, "Device Error", $var & " Device not present or pingable!")
EndIf
EndFunc   ;==>_mapdrive
Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Link to comment
Share on other sites

_Stringbetween is not seeing anything?

It does seem to be missing something.

Try StringInString() and StringLeft()

Example

$variable = '\\host\c:\windows'

$count = StringInStr($variable, '\', Default, 3); 3 is third instance of \
$result = StringLeft($variable, $count)
MsgBox(0, '', _
        '$variable = ' & $variable & @CRLF & _
        '$count = ' & $count & @CRLF & _
        '$result = ' & $result)

MsgBox(0, '', 'Between \\ \ = ' & StringReplace($result, '\', ''))
Link to comment
Share on other sites

Hi,

sorry my fault. Missing, as sometimes, the return from _StringBetween (see helpfile):

Success: A 0 based $array[0] contains the first found string.

So just change as follows:

If Ping($var_DrivePath [0], 25) Then

and everythings o.k

;-))

Stefan

@Edit: grabbing the idea from Mhz. You don't need then #include <string.au3> and makes youe exe smaller:

If Ping (StringMid ($DrivePath, 3, StringInStr ($DrivePath, "\", Default, 3) - 3 )) Then
Edited by 99ojo
Link to comment
Share on other sites

thanks everyone worked a treat here the finished function just in case anyone else would be interested..

Func _mapdrive($DriveLtr, $DrivePath)
 $var_DrivePath = _StringBetween($DrivePath, "\\", "\")
 If Ping($var_DrivePath[0], 25) Then ; so now the function will not try to connect to a device that is not present, make sure your firewall allow ICBM echo requests.
  DriveMapAdd($DriveLtr, $DrivePath, 1) ; thanks to 99ojo for his help, see [url="http://www.autoitscript.com/forum/index.php?showtopic=110567&st=0&gopid=776497&#entry776497"]http://www.autoitscript.com/forum/index.php?showtopic=110567&st=0&gopid=776497&#entry776497[/url]
  Switch @error
   Case 1
    MsgBox(16, "O.M.G Error", "An unknown error occured on " & $DrivePath & " trying to be mapped as local drive " & $DriveLtr & " maybe end device is not avilable for mapping")
   Case 2
    MsgBox(16, "Access Error", "Access to the remote share " & $DrivePath & " was denied")
   Case 3
    MsgBox(64, "Map Drive Error", "The device/drive " & $DriveLtr & " is already assigned and will be deleted")
   Case 4
    MsgBox(16, "Device Error", "Invalid device " & $DriveLtr & " name")
   Case 5
    MsgBox(16, "Connect to Remote Share Error", "Invalid remote share :" & $DrivePath)
   Case 6
    MsgBox(16, "Password Error", "Invalid password")
   Case Else
    ;MsgBox(64, "Completed!", "Mapped " & $DriveLtr & " to share " & $DrivePath)
    Sleep(500)
  EndSwitch
 Else
  MsgBox(16, "Device Error", $var_DrivePath[0] & " Device not present or pingable!")
 EndIf
EndFunc   ;==>_mapdrive

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

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