0

Sorry if the question title is hard to understand, it was difficult to phrase.

Regarding the following question and answer:

open-cygwin-at-a-specific-folder

In LindseyD's answer it tells you how to setup a windows "sendto" shortcut to open a folder. using:

C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico  C:\cygwin\bin\bash.exe  -l -c "cd \"$0\" ; exec bash

This is really cool, but often I am in a folder which contains only files, so to use this I have to go up a level and then right-click the folder and do sendto...

This is because it does not work on files since the parameter passed in to the shortcut is a file path not a folder, e.g.: c:...\test.txt. So I am trying to figure out how to make this work by right-clicking a file --> sendto --> bash.

But I have come up short, my idea was to find a way to extract just the path from whatever is in $0. But I can't find a valid syntax to do this. I have been looking at the following:

how-to-open-a-cygwin-shell-at-a-specific-directory-from-netbeans

how-to-get-folder-path-from-file-path-with-cmd

batch-extract-path-and-filename-from-a-variable

And many others, but none of these seem to be applicable to the variable $0. For example I can't seem to use ~dp&0 this is obviously wrong. So either it can't be done like this, or my understanding of the syntax is wrong :o

Community
  • 1
  • 1
code_fodder
  • 15,263
  • 17
  • 90
  • 167

2 Answers2

1

You are probably looking for the chere package

https://sourceware.org/ml/cygwin-announce/2014-02/msg00024.html

It can create a "bash prompt here" menu for explorer. (left mouse key)

matzeri
  • 8,062
  • 2
  • 15
  • 16
  • Thanks for that, it is a valid answer (so +1 for that), but I would rather stick with plain dos/windows/bash tools if possible. This is because its very hard for me to get new software packages approved :( – code_fodder Feb 08 '17 at 11:53
  • chere is a bash script. It creates the menu entry for explorer – matzeri Feb 08 '17 at 12:04
0

You could try a command (value of -c arg) like

cd $( basedir $( cygpath -ua "$0" ) )

Working outward, convert target path to absolute unix style, get the base directory, change directory.

You will need to escape the quotes and put quotes around the whole string.

This should be similar to chere, but opens in file's parent. I'm not able to test just now, but it may open in selected dir's parent as well.

Doug Henderson
  • 785
  • 8
  • 17
  • This did not work for me. I tried this in dos: `C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico C:\cygwin\bin\bash.exe -l -c "cd $( basedir $( cygpath -ua \"c:\dev\test\test.txt\" ) )" ; exec bash` NOTE: I used a complete path as a test so I can run it from DOS before using it from within the shortcut, I get the minty open with the reply: basedir command not found – code_fodder Feb 09 '17 at 13:04
  • Sorry. I mixed up the names of `dirname` and `basename` which return the parts of the path to left and right (respectively) to the rightmost / in the path. – Doug Henderson Feb 10 '17 at 02:48
  • Ah, ok np. I will try this out on Monday when I get back thankyou – code_fodder Feb 10 '17 at 17:17