Windows下的快捷目录跳转


上一篇文章介绍了Ubuntu环境下的快捷目录跳转功能,还觉得不过瘾,因为除了Linux下有命令行外,在Windows上我也经常使用CMD进行某些操作以提高效率或是必须在命令行下才能进行的操作,在GitHub上搜索 jeroenhjanssens 这个关键字(他是Linux下那个工具的发明者)的时候,碰巧搜到了dictoon/marks他的项目介绍为:A port of Jeroen Janssens’ marks tools to Windows.

即Windows版的快捷目录跳转实现。

安装:

在GitHub上下载该项目的所有文件,可以只将 j.bat / m.bat / marks.bat / unmark.bat 这4个批处理文件放在一个你觉得合适的地方,然后记得将该路径添加至系统路径中,在你使用的时候,它会在你存放的目录下新建一个叫做mark的子目录用于存放后缀为.mark的文件,文件内容其实就是标记的路径。

使用方法:
m <name>    # to create a named shortcut to the current directory. Example: m docs
j <name>    # to follow a given shortcut. Example: j docs
unmark <name>  # to delete a given shortcut. Example: unmark docs
marks       # to print all available shortcuts.
使用示例:
C:Windows> mark win

C:Windows> marks
win -> C:Windows

C:Windows> cd ..

C:> jump win

C:Windows>
最后,我一般是用的快捷方式有:
  • hosts -> C:WindowsSystem32driversetc
  • php55 -> C:php-5.5.9-Win32-VC11-x64
  • py27 -> C:Python27Scripts
  • py34 -> C:Python34Scripts

———-

这里面涉及到的4个批处理脚本:
m.bat(为当前目录创建shortcut)
@echo off
if ("%~1") == ("") echo Please specify mark name.&&exit /b 1
for /f "delims=*+,/:;<=>?[]|" %%a in ("%~1") do if not ("%%~a") == ("%~1") echo Please do not use special characters in the mark name.&&exit /b 1
if not exist "%~dp0marks" mkdir "%~dp0marks"
echo %CD%>"%~dp0marks%~1.mark"
j.bat(跳转至指定的shortcut代表的目录)
@echo off
if ("%~1") == ("") echo Please specify mark name.&&exit /b 1
for /f "delims=*+,/:;<=>?[]|" %%a in ("%~1") do if not ("%%~a") == ("%~1") echo Please do not use special characters in the mark name.&&exit /b 1
if not exist "%~dp0marks%~1.mark" echo Mark does not exist: %~1&&exit /b 1
for /F "usebackq delims=" %%i in ("%~dp0marks%~1.mark") do if exist "%%i" (%%~di && cd "%%i") else (echo Jump destination does not exist: %%i)
marks.bat(显示所有已创建的shortcuts)
@echo off
if not exist "%~dp0marks*.mark" echo No marks exist.&&exit /b 1
for /f "delims=" %%f in ('dir /b "%~dp0marks*.mark"') do (for /F "usebackq delims=" %%i in ("%~dp0marks%%f") do echo %%~nf -^> %%i)
umark.bat(解除指定shortcut)
@echo off
if ("%~1") == ("") echo Please specify mark name.&&exit /b 1
for /f "delims=*+,/:;<=>?[]|" %%a in ("%~1") do if not ("%%~a") == ("%~1") echo Please do not use special characters in the mark name.&&exit /b 1
if not exist "%~dp0marks%~1.mark" echo No mark exists with name: %~1&&exit /b 1
del "%~dp0marks%~1.mark"

其实可以从这几个batch脚本中学到很多实用的Windows命令行下的字符串处理技巧。

,

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注