Cygwin/MSYS2文件夹书签功能函数,调用Windows资源管理器快速打开文件夹;


favorite-dirs函数

如需使用第三方软件或插件打开文件夹路径,只需替换代码中的explorer.exe为第三方软件可执行文件名即可,比如可以替换成total commander等软件的可执行程序;(注意可能需要填写软件程序的完整路径),

favorite-dirs() {
	#使用资源管理器快速打开Windows系统常用文件夹
	local mydirs=$(cat<


二次修改完善版:

增强功能:
1、合并paths函数实现的功能,从/v/bin/dirs.conf文件中载入附加配置路径到代码中预定义的常规路径,统一进行操作;
2、支持传递$1参数对路径进行关键字搜索;
3、交互中支持使用通配符 * 选择所有目录选项;
4、支持使用命令行参数--open xxx指定第三方程序替代explorer.exe打开目录;
5、支持定义多个alias复用函数代码以不同程序打开指定的目录路径;
如:Total CommanderEverythingVSCodeCygwin、自定义bat,vbs脚本,第三方exe程序等...

favorite-dirs() {
	#使用资源管理器快速打开Windows系统常用文件夹
	local mydirs=$(cat<$targetDir"
							print_color 9 "targetDir not exist! ==> $targetDir"
						else
							echo "Open Dir $targetDir ..."
							#cmd /c explorer.exe `cygpath -aw "$targetDir"`
							cmd /c "$openerTool" `cygpath -aw "$targetDir"`
						fi
					else #适配Shell:模式或Windows原生环境变量模式;
						echo "Open Dir $targetDir ..."
						#cmd /c explorer.exe "$targetDir"
						cmd /c "$openerTool" "$targetDir"
					fi
				else
					echo "targetDir is empty!"
				fi
			}
			if [[ "$dirChoose" == "*" ]];then #打开当前列表所有文件夹
				local dirCount=$(echo "$mydirs"|wc -l)
				for dir in `seq 1 1 $dirCount`;
				do 
					open-single-dir $dir
				done
				##以下这行接收到通配符即退出函数,为了方便在命令行执行 `dvscode 'Work\\Company' <<<"*"`这样的命令
				break
			else
				mapfile -t -d $' ' myDirArr<<<"$dirChoose"
			
				for dir in ${myDirArr[@]};
				do 
					#echo "open dir:$dir"
					[[ "$dir" == "0" ]] && { echo "exit...";return; }
					open-single-dir $dir
				done	
			fi					
		else
			echo "Have No Choice...!"
		fi
	done
	set +f #还原通配符拓展
}
alias mydirs='favorite-dirs'
alias d3='favorite-dirs'
alias dtc='favorite-dirs --open tcopen.bat'
alias d4='favorite-dirs --open tcopen.bat'
alias dcr='favorite-dirs --open tcopen.bat'
alias dcl='favorite-dirs --open tcopen-left.bat'
#alias deverything='favorite-dirs --open D:\\Extra\\AcmeKit\\Everything.exe -p'
alias deverything='favorite-dirs --open openeverything.bat'
alias d5='deverything'
alias ds='deverything'
alias dvscode='favorite-dirs --open vscode.bat'
alias dcygwin='favorite-dirs --open cygwin-dir.bat'
alias dmintty='favorite-dirs --open cygwin-dir.bat'

附:老版本的 paths 函数功能代码:

paths() {
	## 存储一些常用的文件夹路径,d命令终端输出
	## 按住Ctrl键,鼠标单击条目即快速打开
	local apppath="/v/bin/dirs.conf"
	if [ -e "${apppath}" ];then
	if [ $# -eq 1 -a -d "$1" ];then
		echo "$1" >>${apppath}
		echo "add $1 To ${apppath} Done..."
		return
	elif [ $# -eq 1 ] && [[ "${1,,}" == "-c" || "${1,,}" == "--check" ]];then
		# $1==-c/--check 查询路径是否存在,以不同颜色显示进行区分
		local paths=$(cat ${apppath}|cygpath -u -f-|sed -r 's/\s/\?/g')
		for pathItem in ${paths[@]}
		do
			OLD_IFS=$IFS
			IFS=$(echo -e "\n")
			local showPath=$(echo $pathItem|sed -r 's/\?/\\\\ /g')
			if [ -e "$showPath" ];
			then
				echo -e "`echo $showPath|sed -r 's/\s/\\\ /g'`\t存在"
				#print_color 3 `echo $showPath|sed -r 's/\s/\\\ /g'`
			else
				#echo "$showPath  不存在"
				print_color 9 "${showPath}\t不存在"
			fi
			IFS=$OLD_IFS
		done	
	elif [ $# -eq 1 ];then
		cat ${apppath}|grep -i "$1"|cygpath -u -f-|sed -r 's/\s/\\\ /g'
	else
		#cat ${apppath}|cygpath -u -f-|sed "s/^/'/;s/$/'/"
		#以下处理为兼容路径中包含空格的情况
		cat ${apppath}|cygpath -u -f-|sed -r 's/\s/\\\ /g'
	fi
	else
		echo -e "dirs configure file not found!\npath:${apppath//\\/\\\\} "
	fi
}
alias d=paths
alias d2='paths --check'

附:获取wshShell SpecialFolder 的VBS代码:

'获取Windows特殊文件夹的真实路径,如,Shell:Desktop、Shell:Appdata等
'See:https://docs.microsoft.com/en-us/previous-versions//0ea7b5xe(v=vs.85)?redirectedfrom=MSDN
Option Explicit
'On Error Resume Next
Dim WshShell,splRegExp,rplRegExp,TargetPathArr
Dim targetPath,suffixTargetPath,targetPathAppend,strShellPath
IF Wscript.Arguments.Count<1 Then
	Wscript.Echo
	Wscript.Quit
End If
targetPath=Wscript.Arguments(0)
targetPathAppend=""
Set WshShell = WScript.CreateObject("WScript.Shell")
Set splRegExp = New RegExp
splRegExp.Pattern = "(\\.*)$"
splRegExp.IgnoreCase = True
splRegExp.Global = True
Set TargetPathArr = splRegExp.Execute(targetPath)
If TargetPathArr.Count>=1 Then
	'Wscript.Echo TargetPathArr(0)
	targetPathAppend=TargetPathArr(0)
End If
Set rplRegExp = New RegExp
rplRegExp.Pattern = "(^shell:|\\.*$)"
rplRegExp.IgnoreCase = True
rplRegExp.Global = True
suffixTargetPath = rplRegExp.Replace(targetPath,"")
strShellPath = WshShell.SpecialFolders(suffixTargetPath)
IF strShellPath="" Then '没有获取到SpecialFoldersFolder结果则直接输出空,不追加路径后缀
	WScript.Echo
Else
	WScript.Echo strShellPath & targetPathAppend
End If