Windows

如何在 Windows 上自動找到損壞的符號連結?

  • December 6, 2018

不確定這是否是不好的風格,但我在這裡問這個問題是因為我在其他地方找不到答案,然後我自己制定了一個解決方案。我很想看看其他人的解決方案,但幾天后我會發布我自己的解決方案。

在我的具體情況下,我在 Windows 7 上執行,但我會對其他/舊版本 Windows 的答案感興趣。我意識到一個答案是“安裝一個版本的 Unix 查找,然後像 Unix 一樣解決”,但我想要一個更“原生”的解決方案。

**編輯 2012-07-17:**澄清:“自動”我理想的意思是我可以作為腳本的一部分執行的東西,而不是按下按鈕即可完成所有工作的 GUI 工具,因為我想做這個無人看管。

有點晚了,但這是我自己對我的問題的回答。它與 Unix 上的常用方法基本相同:查找所有連結,然後處理損壞的連結;它只是不那麼簡潔。下面的腳本在轉儲一些有關它們的資訊後刪除損壞的符號連結。

@echo off

rem Grab the list of directories to scan, before we "pushd to dir containing this script",
rem so that we can have the default be "dir from which we ran this script".
setlocal
if x%CD%==x (echo Command Extensions must be enabled. && goto :eof)
set ORIGINAL_DIR=%CD%

pushd %~dp0

set DIRS_TO_CHECK=%*
if x%DIRS_TO_CHECK%==x (
   set DIRS_TO_CHECK=.
)

rem Find all the files which are both links (/al) and directories (/ad).
rem (We use "delims=" in case any paths have a space; space is a delim by default.)
rem If not, delete it (and assume something else will fix it later :-).
echo Deleting broken symlinks ...
echo.
for %%D in (%ORIGINAL_DIR%\%DIRS_TO_CHECK%) do (
   echo Checking %%D
   echo.
   pushd %%D
   if errorlevel 1 (
       echo Cannot find "%%D"
       echo.
       goto :Next_Dir
   )
   rem Clean up broken directory links.
   for /f "usebackq delims=" %%L in (`dir /adl /b`) do (
       rem Check the directory link works.
       rem Redirecting to nul just to hide "The system cannot find the file specified." message.
       pushd "%%L" >nul 2>&1
       if errorlevel 1 (
           echo Deleting broken directory symlink "%%L".
           rem First dump out some info on the link, before we delete it.
           rem I'd rather a more human-readable form, but don't know how to get that.
           fsutil reparsepoint query "%%L"
           rmdir "%%L"
           echo.
       ) else (
           popd
       )
   )
   rem Clean up broken file (non-directory) links.
   for /f "usebackq delims=" %%L in (`dir /a-dl /b`) do (
       rem Check the file link works.
       rem Redirecting to nul just to hide "The system cannot find the file specified." message.
       copy "%%L" nul >nul 2>&1
       if errorlevel 1 (
           echo Deleting broken file symlink "%%L".
           rem First dump out some info on the link, before we delete it.
           rem I'd rather a more human-readable form, but don't know how to get that.
           fsutil reparsepoint query "%%L"
           rm "%%L"
           echo.
       ) else (
           popd
       )
   )
   popd
   :Next_Dir
   rem Putting a label on the line immediately before a ')' causes a batch file parse error, hence this comment.
)
echo Deleting broken symlinks ... done.

:Finally
popd

引用自:https://serverfault.com/questions/408192