Windows上的readlink的本机替代品 [英] Native alternative for readlink on Windows
问题描述
Windows 本地的替代版本是mklink
.
Windows native alternative for ln -s
is mklink
.
readlink
是否有任何本机替代品?还是如何原生识别文件是否为符号链接?
Is there any native alternative for readlink
? Or how to natively identify if the file is symlink?
推荐答案
我不相信有任何直接与readlink
等效的工具.但是,您可以在dir
的输出中看到某个项目是否为符号链接,因此仍可以从命令行或脚本中确定该项目.
I don't believe there's any tool directly equivalent to readlink
. But you can see in the output of dir
whether an item is a symlink or not, so you can still determine this from the command line or in a script.
您可以使用以下语法轻松测试给定文件是否为符号链接:
You can pretty easily test whether a given file is a symlink using syntax like:
dir mysymlink | find "<SYMLINK>" >NUL
if not errorlevel 1 echo. It's a symlink!
在找到"<SYMLINK>
"的情况下,错误级别将为0,否则将为1.
Errorlevel will be 0 in the case that "<SYMLINK>
" is found, and will be 1 otherwise.
确定符号链接的实际目标是另一回事,在我看来并不是那么简单. dir mysymlink | find "<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 like
11/13/2012 12:53 AM <SYMLINK> mysymlink [C:\Windows\Temp\somefile.txt]
您可以对此进行解析,但是某些字符使得很难在一个变量中处理所有字符,因此我发现处理临时文件更容易:
You 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:
dir mysymlink | find "<SYMLINK>" > temp.txt
for /f "tokens=6" %i in (temp.txt) do @echo %i
这将产生类似于[C:\Windows\Temp\somefile.txt]
的输出. (请记住在任何批处理脚本中都使用%%i
;以上内容来自命令提示符.)
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:
dir mysymlink | find "<SYMLINK>" > temp.txt
for /f "tokens=6" %i in (temp.txt) do set answer=%i
echo %answer:~1,-1%
变量%answer%
包含带括号的结果,因此%answer:~1,-1%
是不带第一个或最后一个字符的结果.
The variable %answer%
contains the result with the brackets, so %answer:~1,-1%
is the result without the first or the last character.
这篇关于Windows上的readlink的本机替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!