最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】帮别人尝试用cygwin中的bash添加右键其中希望获得文件名和后缀名

Windows crifan 2408浏览 0评论

【问题】

别人问到的:

其实我的问题很简单就是右键的时候取不到那个文件的文件名,现在抛开moshell, 我就用echo显示这个文件名

c:\cygwin\bin\bash –login -c "cd ‘%1/..’;echo ‘%~nx1’;exec bash"

%~nx1 据说就是用来显示文件名和扩展名的,但是我用在这里就是不成功。

输出来总是%~nx1,不能输出文件名 不知道问题在哪?

问题转化为:

c:\cygwin\bin\bash –login -c "cd ‘%1/..’;echo ‘%~nx1’;exec bash"

中的,为何用:

echo ‘%~nx1’

获取不到文件名和后缀名。

但是,我现在,需要搞懂其全部的含义,再去找到此问题的原因所在。

 

【解决过程】

1.其中的:

c:\cygwin\bin\bash

不用多说,是cygwin中的bash

2.所以去搜bash的用法,找到:

http://www.gnu.org/software/bash/manual/bash.html

-c string

Read and execute commands from string after processing the options, then exit. Any remaining arguments are assigned to the positional parameters, starting with $0.

--login

Equivalent to -l.

-l

Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell with ‘exec -l bash’. When the shell is not interactive, the login shell startup files will be executed. ‘exec bash -l’ or ‘exec bash –login’ will replace the current shell with a Bash login shell. See Bash Startup Files, for a description of the special behavior of a login shell.

3.对于–login,先不去深究。

对于-c,很明显,就是执行对应的命令。

此处即:

cd ‘%1/..’;echo ‘%~nx1’;exec bash

4.现在就去研究:

cd ‘%1/..’

所以再去找cd的用法。

刚去搜了,linux中的cd的用法。

现在才意识到,此处是用的windows中的cd。

5。后来的后来,发现%1等写法,是windows的batch内置支持的,不是单独针对cd的。

所以找到了,自己之前整理的:

【整理】Windows Batch File test

找到了:

Using batch parameters

此表格解释的很清晰:

Modifier
Description

%~1

Expands %1 and removes any surrounding quotation marks ("").

%~f1

Expands %1 to a fully qualified path name.

%~d1

Expands %1 to a drive letter.

%~p1

Expands %1 to a path.

%~n1

Expands %1 to a file name.

%~x1

Expands %1 to a file extension.

%~s1

Expanded path contains short names only.

%~a1

Expands %1 to file attributes.

%~t1

Expands %1 to date and time of file.

%~z1

Expands %1 to size of file.

%~$PATH:1

Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string.

The following table lists possible combinations of modifiers and qualifiers that you can use to get compound results.

Modifier
Description

%~dp1

Expands %1 to a drive letter and path.

%~nx1

Expands %1 to a file name and extension.

%~dp$PATH:1

Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.

%~ftza1

Expands %1 to a dir-like output line.

 

6.此处,看起来,感觉像是,多写了单引号的缘故。

所以等其试试,去掉单引号:

c:\cygwin\bin\bash –login -c "cd %1/..;echo %~nx1;exec bash"

看看是否可行。

 

7.把后来其自己的折腾,贴出来,供需要的参考:

我试过了,似乎还是不行,因为这个我用鼠标点击一个文件的时候的,这个%1的返回值已经是不完整的了,只有空格前面的部分。所以没有继续操作。

例如:
c:\123\abc def\1.zip

%1的返回值就是:
c:\123\abc

但是%*的返回值是完整的:
c:\123\abc def\1.zip

所以我就把这个值set给一个字符串, 去掉所有空格得到:
c:\123\abcdef\1.zip
然后我再调用一个函数,把这个去掉空格的字符串作为返回参数 (%~nx1)
获得1.zip

具体是这样写的:

@echo off
set _T=%*/..
set str1=%*
set "str1=%str1: =%"
call :Print "%str1%"
pause
goto :eof

:Print
    set _D=%~nx1
goto :eof

这样就可以实现了!

感觉这个windows下太不完善了啊!!

谢谢!!!

在 2013年6月22日下午2:56, <[email protected]>写道:

    “增加一个if判断语句 当所取的%1不为空的时候,进行其他的操作,例如 cd, echo等等”

    然后传递的时候,把获得的%1的值,加上双引号,然后cd之类的操作,就可以正常了

    在 2013-06-22 12:39:19,"WAN CHANG" <[email protected]> 写道:

        Hi , 现在发现了一个问题,就是批处理如果路径里面带空格的话,这个%1 和 %~nx1的输入不正常,只会输出空格前面的部分:

        @echo off
        echo %1
        echo %~nx1
        echo %*
        pause

        这个%*是可以正常输出的,但是似乎没有~nx的扩展功能。。

        不知道你有没有什么好的想法?

        谢谢!!!


        在 2013年6月22日下午12:15, <[email protected]>写道:

            恩,实现就好。

            或许以后,等更加熟悉,才知道原因的。

            在 2013-06-22 01:45:30,"WAN CHANG" <[email protected]> 写道:

                Hi ,

                这个我试过了,是不行的,还是只能输出:

                %~nx1

                [~]$

                我觉得还是这个%~nx1之类的%1 引申用法不能再这种语句里面使用的问题。

                我已经放弃了这个方法,现在我做的方法是:

                1. 右键调用c:\cygwin\Cygwin.bat自带的文件

                2. 在缺省的Cygwin.bat文件里面增加了2个变量,来记录文件所在路径(%1)和文件名及扩展名(%~nx1).

                3. 修改c:\cygwin\home\USER\.bash_profile文件,增加一个if判断语句 当所取的%1不为空的时候,进行其他的操作,例如 cd, echo等等

                %~nx1在.bat文件里面是可以正常输出的。。

                这样一来就有点复杂了。但是功能还是可以实现的。

                thanks!

 

【总结】

cmd下面,cygwin下面,bash下面等等,关于空格的问题,一致是个恼人的问题。

不过,相信,等有空自己去折腾,还是可以找到更好的解决办法的。

只是暂时没环境供折腾,就此作罢。

转载请注明:在路上 » 【记录】帮别人尝试用cygwin中的bash添加右键其中希望获得文件名和后缀名

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.182 seconds, using 22.11MB memory