pushd() Function - Map drive if UNC detected

Post your working scripts, libraries and tools for AHK v1.1 and older
Dragonslayr
Posts: 17
Joined: 24 Feb 2014, 10:12

pushd() Function - Map drive if UNC detected

14 Apr 2014, 20:40

Simple function to map a drive letter on the fly if the script is running from a unc path.
Then close the map afterwards..
Tested on XP 32bit and win7 64bit

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; Pushd() By DragonSlayr
; Function to create temporary drive letter for unc path.

; If the script is  running from unc, it creates the temporary mapped drive.
; Returns 1 if running from a unc path
; Returns 0 when you are not running from a unc path.

; Example:
	currentscriptdir = %A_scriptDir% ; Make a common path, so I only have to have one run command regardless if on unc or not.

	if pushd("open") ; Test if on unc path, map the drive if I am.
	{
		currentscriptdir = %PushdDrive%\%PushdScriptDir%
	}
; Just a reminder. When using %comspec% if the path or name of the executable contains spaces, the entire string should be enclosed in quotes.
	runwait %comspec% /c "dir "%currentscriptdir%" > test.txt", %currentscriptdir%, hide
	runwait notepad.exe %currentscriptdir%\test.txt

    Pushd("close") ; Close the temporary mapped drive if still open.

; End Example
   
pushd(openclose)
{
global PushdDrive
global PushdScriptDir
global PushdPid

if openclose not in open,Close
	{
		msgbox, Command Failed!`npushd() supports two options. "Open" or "Close"
		Return
	}
	

  if openclose = open
  {
    SplitPath, A_ScriptFullPath, ProgramExe, ProgramDir, ProgramExtension, ProgramNameNoExtention , ProgramDrive
    ;msgbox %ProgramExe% `n %ProgramDir% `n %ProgramExtension% `n %ProgramNameNoExtention% `n %ProgramDrive%

    IfInString, ProgramDrive, \\
      {
	    StringGetPos, backslash, ProgramDir, \, L4
        backslash := backslash +1
        StringTrimLeft, PushdScriptDir, ProgramDir, %backslash%
        DriveGet, beforepushd, list, ;Network
        run, %comspec% /k "pushd %A_scriptDir%",%A_Desktop% ,UseErrorLevel hide, PushdPid ; Pushd maps the share, Hold open with /k
        sleep 500
        DriveGet, afterpushd, list, ;Network
        PushdDrive := RegExReplace(afterpushd, "[" beforepushd "]")
        PushdDrive = %PushdDrive%:
        Return 1
        }  
        
    else
      {
		Return 0
      }
      
  }
  
  if openclose = Close
  {
    DetectHiddenWindows, on
    sleep 500
    ControlSend, ,exit{enter}, ahk_pid %PushdPid%
    DetectHiddenWindows, off
  }
  
}


Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 259 guests