Open corresponding remote directory with bat file

erwi1313

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.
davidmneedham

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Recursive directory processing in a BAT file with a twist

분류에서Dev

Create file at Remote directory in sftp

분류에서Dev

Excel VBA How to Execute BAT file in folders in current directory

분류에서Dev

Python create file in certain directory with open()

분류에서Dev

awk: fatal: cannot open file `' for reading (No such file or directory)

분류에서Dev

react-native-sqlite-storage How to open the specified directory file?

분류에서Dev

Open a window in a remote machine

분류에서Dev

ImportError: libavcodec.so.56: cannot open shared object file: No such file or directory

분류에서Dev

"No such file or directory" error when attempting to copy (using scp) from remote host to local machine

분류에서Dev

Is a symlink to a directory a file or a directory?

분류에서Dev

Launching a .reg within a .bat file

분류에서Dev

WMIC command not working in .bat file

분류에서Dev

How to save file and corresponding url?

분류에서Dev

Check the existence of remote directory with R

분류에서Dev

kubectl exec -it <POD> getting sh : ca n't open 'export': No such file or directory

분류에서Dev

Yosemite + Fusion 7.0.1 now gets "Could not open /dev/vmmon: No such file or directory." and will not start any VM

분류에서Dev

Remote File Access

분류에서Dev

user cannot open a specific directory

분류에서Dev

`open` command to open a file in an application

분류에서Dev

How to sign a Windows batch (.bat) file?

분류에서Dev

Bat file returns error but works correctly

분류에서Dev

What is wrong with my if parameter syntax of this .bat file?

분류에서Dev

How to start an application Maximized using a .bat file?

분류에서Dev

Find corresponding .INF file for device driver

분류에서Dev

12.04LTS에서 "wodim no such file or directory.cannot open scsi driver"를 해결하는 방법

분류에서Dev

12.04LTS에서 "wodim no such file or directory.cannot open scsi driver"를 해결하는 방법

분류에서Dev

lspci에서 "Cannot open / sys / bus / pci / devices / xxxxx / resource : No such file or directory"를 반환합니다.

분류에서Dev

Create a shortcut to easily open corresponding hpp or cpp files in sublime text?

분류에서Dev

Github Strategy/Remote Setup for Open Source Project

Related 관련 기사

  1. 1

    Recursive directory processing in a BAT file with a twist

  2. 2

    Create file at Remote directory in sftp

  3. 3

    Excel VBA How to Execute BAT file in folders in current directory

  4. 4

    Python create file in certain directory with open()

  5. 5

    awk: fatal: cannot open file `' for reading (No such file or directory)

  6. 6

    react-native-sqlite-storage How to open the specified directory file?

  7. 7

    Open a window in a remote machine

  8. 8

    ImportError: libavcodec.so.56: cannot open shared object file: No such file or directory

  9. 9

    "No such file or directory" error when attempting to copy (using scp) from remote host to local machine

  10. 10

    Is a symlink to a directory a file or a directory?

  11. 11

    Launching a .reg within a .bat file

  12. 12

    WMIC command not working in .bat file

  13. 13

    How to save file and corresponding url?

  14. 14

    Check the existence of remote directory with R

  15. 15

    kubectl exec -it <POD> getting sh : ca n't open 'export': No such file or directory

  16. 16

    Yosemite + Fusion 7.0.1 now gets "Could not open /dev/vmmon: No such file or directory." and will not start any VM

  17. 17

    Remote File Access

  18. 18

    user cannot open a specific directory

  19. 19

    `open` command to open a file in an application

  20. 20

    How to sign a Windows batch (.bat) file?

  21. 21

    Bat file returns error but works correctly

  22. 22

    What is wrong with my if parameter syntax of this .bat file?

  23. 23

    How to start an application Maximized using a .bat file?

  24. 24

    Find corresponding .INF file for device driver

  25. 25

    12.04LTS에서 "wodim no such file or directory.cannot open scsi driver"를 해결하는 방법

  26. 26

    12.04LTS에서 "wodim no such file or directory.cannot open scsi driver"를 해결하는 방법

  27. 27

    lspci에서 "Cannot open / sys / bus / pci / devices / xxxxx / resource : No such file or directory"를 반환합니다.

  28. 28

    Create a shortcut to easily open corresponding hpp or cpp files in sublime text?

  29. 29

    Github Strategy/Remote Setup for Open Source Project

뜨겁다태그

보관