如何在 Windows 上自动查找损坏的符号链接?

如何在 Windows 上自动查找损坏的符号链接?

不知道这种做法是否不好,但我在这里问这个问题是因为我在别处找不到答案,然后我自己想出了一个解决方案。我很想看看其他人的解决方案,但几天后我会发布自己的解决方案。

在我的具体情况下,我使用的是 Windows 7,但我对其他/旧版本的 Windows 的答案感兴趣。我意识到一个答案是“安装一个版本的 Unix find,然后与 Unix 一样解决“,但我想要一个更“本土”的解决方案。

编辑2012-07-17:澄清:我所说的“自动”理想情况下是指我可以作为脚本的一部分运行的东西,而不是只需按一下按钮即可完成所有工作的 GUI 工具,因为我想无人值守地完成这项工作。

答案1

虽然有些迟了,但这是我自己对我的问题的回答。它基本上与 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

答案2

以下工具可能有助于您查找和删除 Windows 中的符号链接

  1. 盘状连接

答案3

您还可以尝试一个名为智联,它显示并检查 Windows NTFS 连接、符号链接甚至快捷方式。

答案4

czkawka 可以查找断开的链接。它有一个 GUI 和 CLI 界面。请参阅https://qarmin.github.io/czkawka/

相关内容