Batch file (windows cmd.exe) check if the directory is a link (symbolic link)

I just found out that this is a way to test in a batch file if the file is a link:

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

How can I do a similar trick for testing if the directory is a symbolic link. This does not work to simply replace <SYMLINK> with <SYMLINKD> , because dir %directoryname% lists the contents of the directory, not the directory itself.

It seems that I need to somehow ask the director to tell me about the directory as if I asked it in the parent directory. (Like ls -d on unix).

Or any other testing method if the directory is a symbolic link?

Thanks!

+6
source share
7 answers

common code:

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

find out if the current folder is a symbolic link:

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

find out if the parent folder is a symbolic link:

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

You have three methods

Solution 1: fsutil reprocessing point

Use symlink / connection to fsutil reparsepoint query and check %errorlevel% for success, for example:

 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 

This works because the fsutil reparsepoint query cannot do anything in the standard directory and throws an error. But a permission error also causes %errorlevel%=1 !

Solution 2: dir + find

List the links of the parent directory with dir , output the filter with find and check %errorlevel% for success, for example:

 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 

Solution 3: for (best)

Get the attributes of a directory with for and check the last of it because it points to links. I think this is the smarter and best solution.

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

FYI: this solution is not dependent on %errorlevel% , so you can also check for the correct errors!

Sources

+13
source

In fact, DIR works great if you add an asterisk to the file name, this way:

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

GreenAsJade drew my attention to this decision failure when there is another entry in the directory corresponding to% filename% *. I believe wiull works in all cases:

 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 not there. if %sp1% EQU 1 echo BINGO! Pause 
+4
source

This also works:

dir / al | find / i "java" | find / i "connection" && & (echo directory - symlink)

+2
source

Update: this solved my excellent task, but, as commentators noted, dir display both the symbolic links of the directory and the directory nodes. So this is the wrong answer if there are connections.


Simple dir /A:ld works fine

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 
+1
source

The corresponding DIR command is shown in the following batch file (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. 
0
source

An alternative approach is to consider each of the three types of reprocessing points separately, in the following batch file (reparse2.bat), which can be easily changed to search only for the type of link you are interested in -

  :: 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. ) 
0
source

Source: https://habr.com/ru/post/954115/


All Articles