Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Not sure if this is bad style, but I'm asking this question here because I couldn't find the answer elsewhere, and then I worked out one solution on my own. I'd be interested to see other people's solutions, but after a few days I'll post my own.

In my specific case, I'm running on Windows 7, but I'd be interested in answers for other/older versions of Windows. I realise that one answer is "install a version of Unix find, and then solve as for Unix", but I wanted a more "native" solution.

EDIT 2012-07-17: Clarification: by "automatically" I ideally mean something I can run as part of a script, rather than a GUI tool which does all the work at the press of a button, because I'll want to do this unattended.

share|improve this question
add comment

2 Answers

Following tools may help you in finding and removing symlink in Windows

  1. Junction Link Magic
  2. Diskjunction
share|improve this answer
    
Those are both handy GUI tools, but I wasn't clear: I want something I can run in a script. I've edited the question to clarify. –  HughG Jul 17 '12 at 10:56
add comment
up vote 0 down vote accepted

somewhat belated, but here's my own answer to my question. It's basically the same approach as the usual one on Unix: find all links, then act on the broken ones; it's just not as concise. The script below deletes broken symlinks, after dumping out some information about them.

@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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.