Open corresponding remote directory with bat file

2

I need a regular shortcut or a .bat located in C:\abc\00001\ It should link to C:\xyz\00001\ , where the 00001 is treated as a relative expression, in this case "current directory name".

The aim is to quickly accessing a "sister folder", whether the folder name is 00001 or 12734 or 96185 etc etc. The real paths will be far away from each other in the folder tree.

Ideally, it would not be a bat file but a regular windows shortcut, but I couldn't get any kind of %CurrDirName% to work.

I tried searching and came up with some code that maybe could be adjusted for the purpose, but I have little experience with this type of syntax..

Get the current directory name (where the bat file is located; C:\abc\00001\ should give 00001)

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

Open the corresponding remote directory (C:\xyz\00001)

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

Any takes? :)

EDIT: Thanks to @davidmneedham I ended up using a VBscript. Here is my final code:

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.

erwi1313

Posted 2018-04-10T14:47:36.437

Reputation: 23

Answers

0

CMD Batch File Method

Create this batch file and place it inside your C:\abc\00001\ directory:

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

If you run this batch file, it will open C:\xyz\00001\ in a new window. The same batch file placed in C:\xyz\00023\ will open C:\xyz\00023\ etc.

%CD% is an environmental variable that represents the current directory. %cd:\abc\=\xyz\% replaces \abc\ with \xyz\ within the string that represents %cd%. See SS64's page on cmd variable replacement for more details.

VBScript Method

The following is the same solution using 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

davidmneedham

Posted 2018-04-10T14:47:36.437

Reputation: 1 724

Hi David, thank you- that's a great start. :) It works perfectly when the folder structure is simple SET newpath=%cd:\ERWI\=\ERWI2\% start %newpath% , but I run into problems when trying to adjust to a more advanced pathway, as I just added as a comment to my original question. To begin with, my attempt on simple double quotes messes it up.. SET newpath=%cd:"\ERWI\"="\ERWI2\%" as well as SET newpath=%cd:"\ERWI\"="\ERWI2\"% – erwi1313 – 2018-04-11T08:58:07.697

Hi again - sorry I removed my first comment just before you posted. You're correct - I had mistakingly forgotten to remove quotation marks, trying to fix my new problem. :) – erwi1313 – 2018-04-11T08:59:23.117

The VBScript executed wonderfully on the simple folder paths, so I will try to adapt it for the complex paths as well. I have a bit more experience with VBScript than command prompts, so I'll try to figure it out. Thank you so much!! I will be loved for this at work!! :) – erwi1313 – 2018-04-11T09:26:41.477

1Yes!! I've gotten everything to work perfectly with the VBscript and it so smoooooth! Danish characters made a fuzz, but was solved by changing from UTF-8 to ANSI. Also, I used a "if FolderExists" to see if the sister folder had been created yet. If not, you're just sent to the "overview folder". Thank you so much for the backbone to my script!! – erwi1313 – 2018-04-11T13:49:25.240