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.
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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다