Search code examples
windowsbatch-filecmdpathsymlink

How do I get my Windows7 symlink to execute from command line?


I have a couple of batch files I want to use regularly, so I decided I would drop them as symlinks into a binaries folder in my path. The idea being that I would use them like I would any other command, without having to change directories. E.g.

> odbimport -u User -f filename 

where odbimport is my symlink to the batch file odbimport.bat.

The process I used to make the symlinks is as follows:

C:\Users\user>mklink C:\utils\odbimport C:\util-files\odbimport.bat
symbolic link created for C:\utils\odbimport <<===>> C:\util-files\odbimport.bat

C:\Users\user>path
Path=C:\....;C:\utils\

C:\Users\user>where odbimport
C:\utils\odbimport

From what I've seen, it looks like I've made the symlink, and the path knows where to find it.

However, after I've made my symlink and attempt to execute, I get:

C:\Users\user> odbimport -u me -f somefile
'odbimport' is not recognized as an internal or external command,
operable program or batch file. "

I've been looking for an answer to this with no success. Everything I find seems to deal more with how to create working symlinks than addressing my issue. The closest thing I found was this. This is essentially my question, except kinda backwards because I don't really want to run the symlinks from Windows Explorer. I have also tried adding .LNK to my PATHEXT variable as in this question.


Solution

  • Add .bat extension to the symlink you create because on Windows .bat extension is necessary to tell the system it's actually an executable batch file. You will still be able to run the file by typing its name only.

    mklink C:\utils\odbimport.bat C:\util-files\odbimport.bat
    

    NTFS hardlinks can be used when source and target are on the same volume, the advantage is that such clones may be executed from Windows Explorer unlike symlinks:

    • mklink /h C:\utils\odbimport.bat C:\util-files\odbimport.bat
    • fsutil hardlink create C:\utils\odbimport.bat C:\util-files\odbimport.bat