简体   繁体   中英

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

How can I test if a file is a symbolic link from a batch file (windows cmd.exe)?

(I would have thought that this was a well phrased question, but stackoverflow appears to want me to write some more, so here it is!)

    dir %filename% | find "<SYMLINK>" && (
      Do something
    )
for /f "tokens=2delims=[]" %%a in ('dir /ad ^|find "<SYMLINKD>"') do echo Symlink: "%%a"

这不适用于Symlink名称中的[]

Here is an even faster solution as it doesn't require a pipe and also allows access to the original file name either as a relative or absolute path:

@echo off
setlocal EnableDelayedExpansion

set ScriptPath=%~dp0
cd /d "%ScriptPath%"

set "r=%__CD__%"

set SearchTarget=SomePath

for /f "tokens=*" %%a in ('dir /s /b /a:l %SearchTarget% 2^>nul') do (
   set "FullPath=%%a"
   set "LocalPath=!:%r%=!"

   rem local -> [absolute]
   echo(!FullPath:%r%=! -^> [%%a]
)

If you don't want to search recursively then remove the /s option for the dir command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM