13

I learned just now that this is a way to test in a batch file if a file is a link:

dir %filename% |  find "<SYMLINK>" && (
   do stuff
)

How can I do a similar trick for testing if a directory is a symlink. It doesn't work to just replace <SYMLINK> with <SYMLINKD>, because dir %directoryname% lists the contents of the directory, not the directory itself.

It seems like I need some way to ask dir to tell me about the directory in the way that it would if I asked in the parent directory. (Like ls -d does in unix).

Or any other way of testing if a directory is a symlink?

Thanks!

4

8 回答 8

19

你有三种方法

解决方案1:fsutil reparsepoint

使用符号链接/连接fsutil reparsepoint query并检查%errorlevel%是否成功,如下所示:

set tmpfile=%TEMP%\%RANDOM%.tmp

fsutil reparsepoint query "%DIR%" >"%tmpfile%"
if %errorlevel% == 0 echo This is a symlink/junction
if %errorlevel% == 1 echo This is a directory

这行得通,因为fsutil reparsepoint query不能对标准目录执行任何操作并引发错误。但是权限错误%errorlevel%=1也会导致!

解决方案2:目录+查找

使用 列出父目录的链接,使用dir过滤输出find并检查%errorlevel%是否成功,如下所示:

set tmpfile=%TEMP%\%RANDOM%.tmp

dir /AL /B "%PARENT_DIR%" | find "%NAME%" >"%tmpfile%"
if %errorlevel% == 0 echo This is a symlink/junction
if %errorlevel% == 1 echo This is a directory

解决方案3:for(最好的)

获取目录的属性for并从中检查最后一个,因为这表示链接。我认为这是更聪明和最好的解决方案。

for %i in ("%DIR%") do set attribs=%~ai
if "%attribs:~-1%" == "l" echo This is a symlink/junction

仅供参考:此解决方案不依赖于%errorlevel%,因此您也可以检查“有效错误”!

来源

于 2014-03-09T14:05:48.587 回答
11

通用代码:

fsutil reparsepoint query "folder name" | find "Symbolic Link" >nul && echo symbolic link found || echo No symbolic link

弄清楚,如果当前文件夹是符号链接:

fsutil reparsepoint query "." | find "Symbolic Link" >nul && echo symbolic link found || echo No symbolic link

弄清楚,如果父文件夹是符号链接:

fsutil reparsepoint query ".." | find "Symbolic Link" >nul && echo symbolic link found || echo No symbolic link
于 2013-09-19T00:57:27.860 回答
4

实际上,如果您在文件名上附加一个星号,DIR 就可以正常工作,因此:

dir %filename%* |  find "<SYMLINKD>" && (
   do stuff
)

当目录中有另一个条目与 %filename%* 匹配时,GreenAsJade 提醒我注意此解决方案的失败。我相信以下将适用于所有情况:

set MYPATH=D:\testdir1
set FILENAME=mylink
set FULL=%MYPATH%\%FILENAME%
set SP1=0
for /f "tokens=4,5 delims= " %%A IN ('dir /L /N %FULL%*') do (
    if %%B EQU %FILENAME% (
    if "%%A" EQU "<SYMLINKD>" set SP1=1
    )
)
if %sp1% EQU 0 echo It's not there.
if %sp1% EQU 1 echo BINGO!
Pause
于 2014-10-29T22:38:37.697 回答
3

这也有效:

dir /al|find /i "java"|find /i "junction" && (回显目录是一个符号链接)

于 2014-12-02T21:28:36.347 回答
3

更新:这解决了我的问题,但正如评论者指出的那样,dir将同时显示目录符号链接和目录连接。所以如果有路口,那就错了。


简单dir /A:ld的作品很好

dir /?

DIR [drive:][path][filename] [/A[[:]attributes]] …

/A          Displays files with specified attributes.  
attributes   D  Directories                R  Read-only files  
             H  Hidden files               A  Files ready for archiving  
             S  System files               I  Not content indexed files  
             L  Reparse Points             -  Prefix meaning not  

请注意,要仅对非链接文件夹执行命令,可以使用属性否定形式:

for /F "usebackq" %%D in (`dir /A:D-L /B some-folder`) do (
    some-command some-folder\%%D
)
于 2016-11-16T16:32:25.480 回答
1

另一种方法是在以下批处理文件(reparse2.bat) 中分别处理这三种类型的重解析点中的每一种,可以轻松修改该文件以仅搜索您感兴趣的链接类型 -

  ::  Directory to Search
  SET directory=C:\Users\%username%\Desktop\TEST1

  ::  Results Text
  echo SEARCH OF DIRECTORY: %directory% & echo.

  ::  Find FILE SymLinks in directory
  dir "%directory%" | find "<SYMLINK>" && (
    echo This is a SymLink FILE
  ) && ( echo. )

  ::  Find DIRECTORY SymLinks in directory
  dir "%directory%" | find "<SYMLINKD>" && (
    echo This is a SymLink DIRECTORY
  ) && ( echo. )

  ::  Find JUNCTIONS in directory
  dir "%directory%" | find "<JUNCTION>" && (
    echo This is a Directory JUNCTION
  ) && ( echo. ) && ( echo. )
于 2017-01-20T20:25:55.007 回答
1

适当的DIR 命令显示在以下批处理文件(reparse.bat) 中 -

  ::  Specify the Directory to search
  SET directory=C:\Users\%username%\Desktop\TEST1

  ::  Results Text
  echo SEARCH OF DIRECTORY FOR REPARSE POINTS & echo.

  ::  List files with ATTRIBUTE L (Reparse Points)
  DIR "%directory%" /A:L

  echo. && echo  Notes - && echo.
  echo  There are 3 types of Reparse points: && echo.
  echo  ^<JUNCTION^> is a Directory Junction
  echo  ^<SYMLINKD^> is a Directory SymLink
  echo  ^<SYMLINK^>  is a File SymLink
  echo. && echo.
于 2017-01-20T20:20:57.017 回答
1

适用于 Windows 10 的简单示例脚本。

信息:如果它作为SymLink存在于%localappdata%\MegaDownloader文件夹中,请执行MegaDownloader.exe。如果它在 中不作为 SymLink 存在%localappdata%\MegaDownloader,则使用mklink将当前文件夹的 SymLinkPortableData路径创建到%localappdata%\MegaDownloader,然后运行​​MegaDownloader.exe

fsutil reparsepoint query "%localappdata%\MegaDownloader" | find "Substitute" >nul && GOTO MD || mklink /D /J "%localappdata%\MegaDownloader" "%cd%\PortableData" >nul 2>&1
    
:MD
"%~dp0\MegaDownloader.exe"

或者

对于符号和/或目录检查,此命令更健壮:

设置 CheckFolder=D:\ExampleFolder

FOR /f "delims=<> tokens=2" %g in ('dir %CheckFolder%* ^| find "<"') do ( IF %g==DIR (echo %CheckFolder% is real a directory.) ELSE (如果 %g==JUNCTION(回显 %CheckFolder% 是 SymbolicLink/Junction。)))

FOR /F "tokens=3*" %s IN ('fsutil reparsepoint query "%CheckFolder%" ^| findstr /ic:"Print Name:"') do SET "SymLinkDir=%s"

(IF NOT EXIST "%SymLinkDir%" (echo 但结的目标目录 ^("%SymLinkDir%"^) 不存在!) ELSE ( IF EXIST "%SymLinkDir%" (echo 和结的目标目录 ^("% SymLinkDir%"^) 存在。) ) )

于 2020-07-16T11:07:33.510 回答