Batch-style variables to detect change in Cygdrive

Jeremy Bopp
Mon Aug 9 19:53:00 GMT 2010

On 8/8/2010 10:52 PM, Monte Cabet wrote:
> Is there anyway I can get batch-style relative drive paths in Cygwin? An
> example of what I mean is "%~d0". The reason for is is that I would like
> to mount folders from a USB, so the drive (the variable I showed does
> drive) will change everytime I plug the USB in. After abit of playing
> around I found how to mount folders, but it needs be ran from inside
> Cygwin via a file ran on start-up or written manually. I am attempting
> to set the mounts from a batch script or atleast get a variable/function
> that will find out what drive to use. Below is some examples I've cooked
> up.
> # batch-style
> set "mono1=cygpath --type windows "%~d0\mono\""
> set "mono2=cygpath --type mixed "%~d0/mono/""
> set "mono3=cygpath --type unix "/cygdrive/%~d0/mono/""
> mount -o binary '%mono1%' /mnt
> # Cygwin-style
> mount -o binary "$(cygpath --type windows "%~d0\mono\)" /mnt
> mount -o binary "$(cygpath --type mixed "%~d0/mono/)" /mnt
> mount -o binary "$(cygpath --type unix /cygdrive/%~d0/mono/)" /mnt

Assuming you'll only ever have a single drive with a directory named
mono in the root and assuming that your cygdrive prefix is /cygdrive,
you could simply do the following from within a Cygwin shell:

mount "$(cygpath -m /cygdrive/*/mono)" /mnt

The cygdrive prefix is trivial to change to suit your needs, but if you
have more than a single drive with a directory named mono in the root,
you'll have to get more creative to find a way to select the correct
one.  In any case, this will only remain until all your Cygwin processes
are stopped.

Another option is to create a shell script with the following content:

mount "$(cygpath -m "$1")/mono" /mnt

Say you named the script /home/monte/bin/  Create a
shortcut that starts in C:\cygwin\bin and has the following target:

C:\cygwin\bin\bash.exe /home/monte/bin/

Now you can drag the drive you find in your file browser for the USB
drive and drop that onto the shortcut to have it mounted within Cygwin.
 This solution assumes that you installed Cygwin into C:\cygwin.  Feel
free to change that to suit your needs.  It also assumes that you have
at least 1 Cygwin process already running so that the mount is preserved
once the script exits.

BTW, it would be a good idea to make the script more robust so that it
can cleanly handle being run more than once or run with a drive that
does not have the folder you wish to mount.


Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list