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


11

不知道这是不好的样式,但是我在这里问这个问题,因为我找不到其他答案,然后我自己制定了一个解决方案。我很想看看其他人的解决方案,但是几天后,我会发布自己的解决方案。

在我的特定情况下,我正在Windows 7上运行,但是我对其他Windows /旧版本的答案感兴趣。我意识到一个答案是“安装Unix版本,然后针对Unix进行解决 ”,但是我想要一个更“原生”的解决方案。

EDIT 2012-07-17:澄清:通过“自动”,我的意思是说我可以将其作为脚本的一部分运行,而不是只需按一下按钮即可完成所有工作的GUI工具,因为我要这样做这个无人值守。

Answers:


4

有点迟了,但这是我对问题的自己的回答。它与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



0

使用cygwin64(find,xargs,sh,ls)(在win7上测试了无效的ntfs硬链接)

c:\>   find ./ -type l |  xargs -I % sh -c "ls % > /dev/null 2>&1 || echo %" 

注意:test -e与失效的ntfs硬链接无法正常工作。


1
感谢@mosh,但是,正如我所说,我想要一个不涉及“安装Unix utils版本”的东西-因为我想要一个脚本,其他人无需准备就可以运行该脚本。
休格
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.