AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q:How to get the UNC names for mapped network drives?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
marker99
Guest





PostPosted: Mon Apr 18, 2005 1:08 pm    Post subject: Q:How to get the UNC names for mapped network drives? Reply with quote

Hi,
I'm trying to get the UNC names for files in mapped network drives.

Example:
Drive N: is mapped to \\pcbackup\user
File is: N:\dir\file.txt in a variable
Wanted: \\pcbackup\user\dir\file.txt

How can I do this within a script??

splitpath didn't help, I still get N:\dir\file.txt

thanks for any pointers
Back to top
BoBo
Guest





PostPosted: Mon Apr 18, 2005 1:22 pm    Post subject: Reply with quote

Code:
Run, %COMSPEC% /c net use,,Hide > UNCNames.txt
Run, notepad UNCNames.txt
Back to top
marker99
Guest





PostPosted: Mon Apr 18, 2005 5:27 pm    Post subject: Reply with quote

Hi Bobo,
many thanks. It does work.

Now I just need the results in a hash array or similar as variable in the script ;-(

Ok, one could write a function to parse the output of the UNCNames.txt ...

another potential alternative, use the Wscript.Network object to do the job.
Is that possible?

here is a draft of the needed code in vbs:
Code:
'VBS code'
my_drive = "N:"
GetUNCName(my_drive)

Private Function GetUNCName(DriveLetter)

  Dim netobj
  Dim i
  Dim AllDrives

  Set netObj = CreateObject("WScript.Network")
  Set AllDrives = netobj.EnumNetworkDrives()

  For i = 0 To AllDrives.Count - 1 Step 2
    If AllDrives.Item(i) = DriveLetter Then
      MsgBox "Drive letter " & DriveLetter & "maps to " & AllDrives.Item(i+1), vbInformation + vbOkOnly
    End If
  Next

End Function


Many thanks for your support
/marker99
Back to top
BoBo
Guest





PostPosted: Mon Apr 18, 2005 6:52 pm    Post subject: Reply with quote

Code:
Loop, Read, UNCNames.txt
{
    IfInString, A_LoopReadLine, \\
    {
       StringReplace, Line, A_LoopReadLine, Microsoft Windows Network,, All
       StringSplit, Field, Line, :
       UNC%A_Index% := %Field2%
    }
}
Of course, untested Rolling Eyes Smile
Back to top
marker99
Guest





PostPosted: Wed Apr 20, 2005 3:06 pm    Post subject: Reply with quote

Hi,
Bobo, many thanks for your hints.
Here is my first little autohotkey script Laughing

Hope that is is useful for someone.
Code:

; Conversion of full path to UNC format
; 2005-04-19, marker99
;
; This is an example script to convert a full path into the UNC format
;  based on hints from Bobo in the autohotkey support forum, many thanks
;

; global vars:
;  unc_drive_max   integer holding the max index of the below array (starting with 1)
;  unc_drive      array variable holding the drive letter, example: N:
;  unc_UNCname      array variable holding the UNC name,     example: \\pcbackup\marker99


;----------------------------------------------------------------
; initialize first to get all UNC names from the mapped drives
initialize_UNC()

; test function to show all mapped network drives
show_mapped_drives()

; test the conversion from full path to UNC format
test()

return

;----------------------------------------------------------------



;;================================================================
;; functions and subroutines
;;================================================================

;;================
; initialize_UNC
;  function to extract the UNC names from the system,
;  input   : none
;  output   : none
;  side effects : stores UNC names and drives in global variables:
;               unc_drive_max, unc_drive1..n, unc_UNCname1..n
;  Note:
;    the trick is, to store the output of the command "net use" to a temporary file
;     and to read that back in. Thanks to Bobo from the autohotkey support forum!
;;================
initialize_UNC()
{
global   ; needed to create a global array of drives and UNC names
    ; unc_drive_max, unc_drive1..n, unc_UNCname1..n

local Line
local TempFile
   TempFile = %TEMP%\UNCNames.txt

   filedelete %TempFile%
   sleep 100
   Run, %COMSPEC% /c net use > %TempFile% ,, HIDE
   sleep 100
   
   unc_drive_max = 0
   Loop, Read, %TempFile%
   {
      
       IfInString, A_LoopReadLine, \\
       {
      ; now trim the result line a bit
      StringTrimLeft Line, A_LoopReadLine, 13   ; remove obsolete leading info of net use
           StringReplace, Line, Line, Microsoft Windows Network,, All   ; remove some obsolete drive info
      StringReplace, Line, Line, Clearcase Dynamic Views,, ALL

      StringSplit, Field, Line, :
   
      if Field1
      {
         unc_drive_max := unc_drive_max + 1
         unc_drive%unc_drive_max% = %Field1%:     ;some magic, we remove leading and trailing spaces here
                unc_UNCname%unc_drive_max% = %Field2%      
      }
       }
   }
   filedelete %TempFile%
   return 1
}

;;================
; show_mapped_drives()
;   shows the mapped network drives in a messagebox
;;================
show_mapped_drives()
{
global
local str
   ; show the results
   loop, %unc_drive_max%
   str = % str "`n" unc_drive%A_Index% " -> " unc_UNCname%A_Index%

   msgbox These are all %unc_drive_max% mapped drives:`n%str%
   return 1
}

;;===============
; test()
;   tests the conversion of full path to UNC formats
;    and shows a message box
;;===============
test()
{

   ;; Now test the whole thing
   full_path1 = "N:\dir1\dar\dur\file.txt"
   full_path2 = "Z:\dir\dar\dur\file.txt"
   full_path3 = "O:\dir\dar\dur\file.txt"
   full_path4 = "C:\dir\dar\dur\file.txt"

   loop, 4
      str = % str "`n" full_path%A_Index% " converts to: " convert_to_UNC(full_path%A_Index%)
   msgbox These are all converted path:`n%str%

}

;;================
; convert full path into UNC path
; uses the global vars: unc_drive_max, unc_drive1...n and unc_UNCname1..n as global vars
; returns the converted path or unchanged path
;;================
convert_to_UNC(my_path)
{
global             ; we need to have unc_drive_max, unc_drive1...n and unc_UNCname1..n as global vars

        loop, %unc_drive_max%
        {
      IfInString, my_path, % unc_drive%A_Index%   ; did we find the right drive??
      {
         StringReplace, my_path, my_path,% unc_drive%A_Index%,% unc_UNCname%A_Index%   
         return %my_path%
      }
        }
   return %my_path%
}

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group