Try this, its not full tested, but may be it does what you mean. I am also a portable user.
The main file:
mount.lib.ahkCode:
/*
Mount
Revision: 0.1
Status: EXPERIMENTAL BETA
Date: Fri, 1 December 2006 11:09:02 GMT
Author: Tuncay
License: GNU GPL, Version 2
Type: Library
Standalone: Yes
Polymorph: No
Tested AHK Version: 1.0.46
Tested WIN Version: XP Pro SP2
Public Functions: (Any parameter enclosed in '[' and ']' are optional.)
Mount([SourcePath], [Mountpoint], [Options])
Returns on success mounted drive with ending backslash and on failure
a string without content.
If Mountpoint is not given, then it looks for first free drive otherwise
existing SourcePath will be mounted to given Mountpoint. If SourcePath
is not given, it defaults to WorkingDir.
Currently there is only one option used to unmount the drive.
UnMount([Mountpoint], [Options])
Like Mount(), but without the need of SourcePath. Also the unmount
option is here given over to Mount() always.
GetMount([Path])
If the specified Path is a drive, the full real path is returned. If
the Path is not given, first virtual drive will be get.
*/
#NoEnv
SendMode Input
#NoTrayIcon
/*
Public Function Mount
Mounts with subst.exe any path to a Windows drive.
2006 by Tuncay
*/
Mount(SourcePath = "", Mountpoint = "", Options = "")
{
GoSub, SetOptions@Mount
GoSub, SetMountPath@Mount
If Option?UnMount
Command = subst %MountPath% /d
Else
{
If (SourcePath = "")
SourcePath := A_WorkingDir
Command = subst %MountPath% "%SourcePath%"
}
If (NOT Option?UnMount AND NOT FileExist(MountPath . "\") AND FileExist(SourcePath))
OR (Option?UnMount AND FileExist(MountPath . "\"))
{
RunWait, "%comspec%" /c %Command%,, Hide UseErrorLevel
Sleep, 200
If ErrorLevel = "ERROR"
{
MountPath := ""
ErrorLevel := 2 ; Failed to launch subst.exe
}
Else
{
MountPath := MountPath . "\"
ErrorLevel := 0
}
}
Else
{
MountPath := ""
ErrorLevel := 1
}
Return MountPath
SetMountPath@Mount:
If (Mountpoint = "") ; Search drive.
{
DriveGet, ActualDrives, List
If NOT Option?UnMount ; Get first free drive.
{
FreeDriveLetters := "CDEFGHIJKLMNOPQRSTUVWXYZ"
Loop, Parse, ActualDrives
StringReplace, FreeDriveLetters, FreeDriveLetters, %A_LoopField%
Loop, Parse, FreeDriveLetters
{
MountPath := A_LoopField . ":"
Break
}
}
Else If Option?UnMount ; Get first subst.exe mounted drive.
{
Loop, Parse, ActualDrives
{
If GetMount(A_LoopField . ":")
{
MountPath := A_LoopField . ":"
Break
}
}
}
}
Else If Mountpoint Is Alpha ; Add double colon on drive letter.
MountPath := Mountpoint . ":"
Else If Mountpoint ; Drive will be extracted from any path.
SplitPath, MountPath,,,,, Mountpoint
Return
SetOptions@Mount:
; Default settings
Option?UnMount := False
CurrentStringCaseSense := A_StringCaseSense
StringCaseSense On
If Options Contains --
{
StringReplace, Options, Options,--unmount,-u
}
StringReplace, Options, Options,/,-, All
; Overwriting default settings.
OptionsFoundList := "" ; For performance, not to loop if already founded
; and avoid dublicates.
Loop, Parse, Options,-,%A_SPACE%
{
If A_LoopField In ,%A_SPACE%
Continue
; Inner Loop for enabling the short style for grouping of options.
Loop, Parse, A_LoopField
{
IfInString, OptionsFoundList, %A_LoopField%
Continue
; Option?UnMount
; 0=creates a virtual drive from given path (default)
; 1=deletes the given virtual drive mounted by subst.exe
If InStr(A_LoopField, "u", 1)
{
Option?UnMount := True
OptionsFoundList := OptionsFoundList . A_LoopField
Continue
}
}
}
StringCaseSense %CurrentStringCaseSense%
Return
}
/*
Public Function UnMount
UnMounts a virtual drive mapped with subst.exe.
2006 by Tuncay
*/
UnMount(Mountpoint = "", Options = "")
{
Return Mount("", Mountpoint, "-u " . Options)
}
/*
Public Function GetMount
Converts virtual path mapped with subst.exe to real physical full path.
2006 by Tuncay
*/
GetMount(pPath = "")
{
Path := pPath
If (Path = "") ; Search first mounted drive. (very slow!)
{
DriveGet, ActualDrives, List
Loop, Parse, ActualDrives
{
;! Recursive call for every existing drive.
If GetMount(A_LoopField . ":")
{
MountPath := A_LoopField . ":"
Break
}
}
}
Else
{
If Path Is Alpha ; Add double colon on drive letter.
Path := Path . ":"
Else
SplitPath, Path,,,,, Path
If FileExist(Path . "\")
{
TempFile = %A_Temp%\{D74BA6E8-2728-4FC6-8185-623EA7DAD412}_%A_Now%.~tmp
Command = subst >"%TempFile%"
RunWait, "%comspec%" /c %Command%,, Hide UseErrorLevel
Sleep, 200
If ErrorLevel = "ERROR"
ErrorLevel := 2 ; Failed to launch subst.exe
Else
{
ErrorLevel := 0
Loop, Read, %TempFile%
{
StringMid, MountDrive, A_LoopReadLine, 1, 2
If (Path = MountDrive)
{
StringMid, MountPath, A_LoopReadLine, 9
Break
}
}
Sleep, 200
FileDelete, %TempFile%
MountPath := MountPath . SubStr(pPath, 3)
}
}
Else
{
MountPath := ""
ErrorLevel := 1
}
}
Return MountPath
}
Example: MountCode:
#Include Mount.lib.ahk
FileSelectFolder, SourcePath
Mount(SourcePath, "x")
Example: UnMountCode:
#Include Mount.lib.ahk
UnMount()
Feel free to modify, but please comment it. I have not made any gui or something, because for an automation script this is not needed.