AutoHotkey Community

It is currently May 27th, 2012, 7:55 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Drive letter setting
PostPosted: November 29th, 2006, 11:50 pm 
I have a removable disk used on several machines where it will get different drive letters. I would like to set a fix driveletter X: with an autoscript.

subst will not do the trick. what will?

SplitPath,A_WorkingDir, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
if(Outdrive <> "X:"){
run subst X: Outdrive
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 29th, 2006, 11:55 pm 
Use DriveGet to retreive the current drive letter of your removable. Then you can use subst to remap it.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2006, 11:02 am 
I said that subst *doesn't work*, I think I have to go through the API.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2006, 11:09 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Did you tried Run %comspec% /c subst X: %Outdrive%?
It is necessary if subst is a built-in command like type (can't remember if that's an exe or com, or a command).
Anyway, your first code won't work because you didn't enclosed Outdrive between percentage signs.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2006, 7:21 pm 
Offline

Joined: November 30th, 2006, 11:27 am
Posts: 5
Yes your right about that and in the context of the script this will work, but I want also the windows system to see the drive. What I'm actually looking for is reassigning the drive letter like I would trough the configuration panel.
As a work around I could use fsutil to create a hardlink but I resent that because IMHO this is a to big infringement on the running system and it only does NTFS.

_________________
If (Live = OK){
enjoy}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2006, 9:33 pm 
Offline

Joined: August 15th, 2005, 7:15 am
Posts: 107
Location: North Carolina
It sounds like you just want the drive letter to be the same on different machines. This does not involve AHK. You would go into Control Panel > Administrative Tools > Computer Management > Storage, to set the drive letter for this drive, on each computer. That is assuming none of them are less than W2K. Next time you plug in the drive on any of those machines, it will 'remember' the drive letter for it.

Good Luck


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2006, 10:51 pm 
You can use Diskpart (winXP).It is a command line program to manage disks.
More info here http://support.microsoft.com/kb/300415


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 30th, 2006, 11:05 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
I have written yesterday a function Mount() using subst.exe, because I am very often at internet cafes with my USB-Stick (AutoHotkey helped me so much thx Chris and all others here). First I wanted a tool to mount any directory into any other directory, but this is with subst not possible. sorry, now I am in an internet cafe, may be tomorrow I can show u it, but I wanted add some more functionalities.

Here the definition, (currently):
Code:
Mount(SourceDirectory [, MountDrive, Options])
UnMount([MountPath])
IsMount(Path)

Mount() does not need any more explanation, but if MountDrive is not given, it searches for first free drive letter. UnMount() unmounts the given drive created with subst.exe. Without any argument, all subst.exe mounted drives will be unplugged. IsMount() returns the real full path of an subst.exe mounted file path.

Do someone need it? May be in the next days, I'll post it here.

EDIT: Below I posted the code, now it is not the same described here.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Last edited by Tuncay on December 1st, 2006, 7:46 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 1st, 2006, 11:20 am 
Offline

Joined: November 30th, 2006, 11:27 am
Posts: 5
I doubt this can be solved with subst as it is a shell command and will probably not go beyond the current shell. One more time for everyone who didn't understand my gibberish:

I have a removable drive.
It's mounted on a great number of unknown window systems.
When I hook up the device it will autorun AHK.
On first run I want AHK to assign a drive letter of my choice (X) to the drive.
After assignment I should be able to access the drive from windows by this drive letter.

I know I can change this from the control panel but I want to have it done
auto magically. If this is possible AHK would be my number one tool on
my portable drive, and in combination with PHP CLI I got an unbeatable
duo that beats PHP-GTK in easy of use.

I'm sure it can be done but I'm now 1 week on AHK and still learning to find my way arround. But I am very, very, impressed by it.

_________________
If (Live = OK){
enjoy}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 1st, 2006, 7:44 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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.ahk
Code:
/*
    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: Mount
Code:
#Include Mount.lib.ahk
FileSelectFolder, SourcePath
Mount(SourcePath, "x")


Example: UnMount
Code:
#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.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Last edited by Tuncay on March 29th, 2010, 10:01 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 28th, 2007, 9:44 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
I updated the Mount script and started a new thread -> Mount any path as a drive with subst.exe

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject: alternative method..
PostPosted: March 11th, 2007, 1:22 am 
There is another way to remember external drive location of specific external drives.

1. Basically you create a folder on your local hard drive, eg. "external_HDD1"
2. Then Right click my computer and select manage
3. Select disk management (under storage)
4. Right click your external drive and select "change drive letter and paths"
5. Click ADD
6. Browse to your folder your created in step one . i.e. c:\external_HDD1
7. click ok and you are done.

Then whenever you insert your external drive or usb drive into your pc, you will always be able to access it in the same place, c:\external_HDD1, regardless of what drive letter windows decides to assign to it.

Apart of retaining the same path to your drive, the other main benefit of this method is that it will remember any folders you set up as sharing on the drive. If you share a folder on an external drive in windows, when you restart your pc or unplug your drive, you loose your share and have to re-set it up. To share a folder, simply go to c:\external_HDD1\folder_to_share and set it to be shared and whenever you plug the drive into the computer that folder will be shared on the network.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: bobbysoon, just me and 71 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group