Windows native alternative for ln -s
is mklink
.
Is there any native alternative for readlink
? Or how to natively identify if the file is symlink?
Windows native alternative for ln -s
is mklink
.
Is there any native alternative for readlink
? Or how to natively identify if the file is symlink?
No need to redirect the stdout to a temp.txt file. You can simply do this in one line:
Also note that the above method by Lars only applies to "file" symlinks. That method has to be adjusted for directory symlinks since passing a directory to the "dir" command will list out that directories actual contents (which won't have itself in there)
So you would use:
Note the inclusion of the * in the dir call and the addition of the "D" in the FIND
The asterisk will prevent dir from listing the contents of that directory.
To further simplify the process to work for either file or directory symlinks, you would simply always include the asterisk and for the FIND argument you would change it to a partial match so that it matches with or without "D" on the end
And finally, since that method actually returns the file inside the brackets [].. you would be better off delimiting those, leaving you with the final method:
I don't believe there's any tool directly equivalent to
readlink
. But you can see in the output ofdir
whether an item is a symlink or not, so you can still determine this from the command line or in a script.Test whether it's a symlink or not
You can pretty easily test whether a given file is a symlink using syntax like:
Errorlevel will be 0 in the case that "
<SYMLINK>
" is found, and will be 1 otherwise.Determine the target of a symlink
Determining the actual target of the symlink is another matter, and in my opinion not as straight-forward. The output of
dir mysymlink | find "<SYMLINK>"
might look likeYou can parse this, but some of the characters make it difficult to deal with all in one variable, so I found it easier to deal with a temporary file:
This yields output like
[C:\Windows\Temp\somefile.txt]
. (Remember to use%%i
within any batch scripts; the above is from the command prompt.)To get the result without the brackets, you can do something like:
The variable
%answer%
contains the result with the brackets, so%answer:~1,-1%
is the result without the first or the last character.