Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file (windows cmd.exe) test if a directory is a link (symlink)

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!

like image 703
GreenAsJade Avatar asked Sep 18 '13 23:09

GreenAsJade


Video Answer


2 Answers

This also works:

dir /al|find /i "java"|find /i "junction" && ( echo directory is a symlink )

like image 142
Hames Avatar answered Sep 29 '22 18:09

Hames


Update: this solved my problem, but as commenters noted, dir will show both directory symlinks and directory junctions. So it's wrong answer if junctions are there.


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  

Note that to execute a command only for non-link folders, you can use the attribute negation form:

for /F "usebackq" %%D in (`dir /A:D-L /B some-folder`) do (
    some-command some-folder\%%D
)
like image 22
zxcat Avatar answered Sep 29 '22 19:09

zxcat