![](/img/trans.png)
[英]batch file (windows cmd.exe) test if a directory is a link (symlink)
[英]batch file (windows cmd.exe) test if a file is a link
如何測試文件是否是批處理文件(windows cmd.exe)中的符號鏈接?
(我本以為這是一個措辭很好的問題,但stackoverflow似乎要我寫更多,所以這里是!)
dir %filename% | find "<SYMLINK>" && (
Do something
)
for /f "tokens=2delims=[]" %%a in ('dir /ad ^|find "<SYMLINKD>"') do echo Symlink: "%%a"
這不適用於Symlink名稱中的[]
。
這是一個更快的解決方案,因為它不需要管道,並且還允許以相對路徑或絕對路徑訪問原始文件名:
@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]
)
如果您不想以遞歸方式搜索,請刪除dir
命令的/s
選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.