Explorer To Cygwin – Open In Folder

replace the Cygwin.bay under c:\cygwin\ with this content,

@echo off
::force UTF-8 support
chcp 65001

if "%1"=="" (
  goto START_STANDARD
) else (
  goto START_IN_FOLDER
)


:START_STANDARD
  call C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -
  goto EXIT
  

:START_IN_FOLDER
  rem  remove [\'\"] from first arg
  set CURRENT=%~1
  rem  backward2forward slash
  set CURRENT=%CURRENT:\=/%
  rem  remove ":" after the drive letter
  set CURRENT=%CURRENT::/=/%
  rem  adds prefix (used in cygwin as dummy drive-mounts)
  set CURRENT=/cygdrive/%CURRENT%
  rem  ------------------------------------------------------------
  call C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico c:\cygwin\bin\bash.exe -l -c "cd \"%CURRENT%\" ; exec bash" -
  goto EXIT


:EXIT

or download the following file instead: Cygwin.bat (here is the original file: Cygwin.bat.bak)

if you’ll pass a folder path as argument to the batch (no checks.. q&d..) it will open cygwin in this path, in any other case, just open cygwin in home-directory (standard run..)

2015-12-05_034648

place a shortcut in your SENDTO folder,
and then right-click a folder and select the shortcut from the sendto-menu

or better yet, download the following reg file: open_in_cygwin.reg and double click it,

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\open_in_cygwin]
@="Open In Cy&gwin"

[HKEY_CLASSES_ROOT\Directory\shell\open_in_cygwin\command]
@="\"C:\\cygwin\\Cygwin.bat\" \"%1\""

it will add a new context-menu item for each folder:
2015-12-05_040001

download and run the following file to uninstall the context-menu item: open_in_cygwin_uninstall.reg

Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\shell\open_in_cygwin]

July 29th, 2017 – Update.

Here is a slightly modified version of the “start cygwin from a folder”, that runs few checks before the actual execute stage, it wraps things with ", and very carefully handles long and short paths, passing paths correctly to cygwin terminal application, also, it does not uses hard-coded paths, so if your cygwin folder placed somewhere else it will work just fine for you :]

@echo off
::              set code-page for UTF-8 charset.
chcp 65001

::              normalise to fully qualified path. short path (8.3) is used for testing the path.
set FOLDER=%~s1
for /f %%a in ("%FOLDER%") do ( set "FOLDER=%%~fsa" )

::              verify existing folder.
if ["%FOLDER%"]==[""]         ( goto RUN_STANDARD   )
if not exist %FOLDER%         ( goto RUN_STANDARD   )
if not exist %FOLDER%\NUL     ( goto RUN_STANDARD   )

goto RUN_FOLDER


::--------------------------------------------------------


:RUN_FOLDER
::             prefer long-path.
  set FOLDER=%~1
  echo.
  echo Starting Cygwin From ^[%FOLDER%^] ...
::             convert to cygwin-compatible path (forward-slash, removing drive's ":" and /cygdrive/ as root)
  set FOLDER=%FOLDER:\=/%
  set FOLDER=%FOLDER::/=/%
  set FOLDER=/cygdrive/%FOLDER%
  call "%~dp0bin\mintty.exe" "-i" "/Cygwin-Terminal.ico" %~dp0bin\bash.exe -l -c "cd \"%FOLDER%\"; exec bash" - 2>nul >nul
  goto EXIT


:RUN_STANDARD
  echo.
  echo Starting Cygwin From Home...
  call "%~dp0bin\mintty.exe" "-i" "/Cygwin-Terminal.ico" "-"   2>nul >nul
  goto EXIT


:EXIT