用bat文件打开相应的远程目录

用bat文件打开相应的远程目录

我需要一个常规快捷方式或位于 C:\abc\00001\ 的 .bat 它应该链接到 C:\xyz\00001\ ,其中 00001 被视为相对表达式,在本例中为“当前目录名称”。

目的是快速访问“姊妹文件夹”,无论文件夹名称是 00001 还是 12734 还是 96185 等等。在文件夹树中,真实路径将彼此相距甚远。

理想情况下,它不是一个 bat 文件,而是一个常规的 Windows 快捷方式,但我无法让任何类型的 %CurrDirName% 工作。

我尝试搜索并找到了一些可以针对此目的进行调整的代码,但我对这种语法几乎没有经验。

获取当前目录名(bat 文件所在的位置;C:\abc\00001\ 应该为 00001)

for %%* in (.) do set CurrDirName=%%~nx*

打开相应的远程目录(C:\xyz\00001)

%SystemRoot%\explorer.exe "c:\xyz\%CurrDirName%"

有什么收获吗?:)

编辑:感谢@davidmneedham,我最终使用了 VBscript。这是我的最终代码:

Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSOexists = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strExchangeThis = "Y:\Organization\...\" 'shortened path!
strToThis  = "Y:\Labspace\...\" 'shortened path!
strRelFolder = Replace(strFolder, strExchangeThis, strToThis)
' if strRelFolder does not exist yet, we should instead be lead to the basic strToThis folder
exists = objFSOexists.FolderExists(strRelFolder)
if Not (exists) then 
    strRelFolder = strToThis
end if
strPath = "explorer.exe /e," & strRelFolder
objShell.Run strPath
' Encoding changed from UTF-8 to ANSI to allow danish characters in strings.

答案1

CMD 批处理文件方法

创建此批处理文件并将其放在您的C:\abc\00001\目录中:

SET newpath=%cd:\abc\=\xyz\%
start %newpath%

如果您运行此批处理文件,它将C:\xyz\00001\在新窗口中打开。放置在中的相同批处理文件C:\xyz\00023\将打开C:\xyz\00023\等。

%CD%是一个代表当前目录的环境变量。在代表的字符串中用替换%cd:\abc\=\xyz\%。请参阅\abc\\xyz\%cd%SS64 关于 cmd 变量替换的页面更多细节。

VBScript 方法

以下是使用 VBScript 的相同解决方案:

Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strRelFolder = Replace(strFolder, "\abc\", "\xyz\")
strPath = "explorer.exe /e," & strRelFolder
objShell.Run strPath

相关内容