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!
This also works:
dir /al|find /i "java"|find /i "junction" && ( echo directory is a symlink )
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With