批量创建目录中所有文件/文件夹的符号链接

时间:2013-11-06 03:36:15

标签: batch-file symlink

我有一个包含许多文件和文件夹的目录,我想将该目录中所有文件和文件夹的符号链接添加到另一个文件夹但排除一个文件夹

有什么建议吗?

1 个答案:

答案 0 :(得分:6)

@echo off

    set source=c:\source\directory
    set target=c:\target\directory
    set exclude=DoNotLinkThisDirectory

    forfiles /P "%source%" /C "cmd /c if @isdir==TRUE (if not @file==\"%exclude%\" mklink /d \"%target%\@file\" @path ) else ( mklink \"%target%\@file\" @path )"

编辑 - 更新为允许“轻松”添加多个排除项,使用/ G:file如果findstr命令过滤文件/文件夹列表

@echo off

    set "source=c:\source\directory"
    set "target=c:\target\directory"
    set "exclude=%temp%\exclude.txt"

    (
        rem exclude files/dires with these strings into full path
        echo .txt
        echo pipe.cmd

        rem escaped backslash and initial and final quotes to avoid partial matches
        echo "c:\\source\\directory\\something.txt"

        rem exclude thisNot file/directory from source directory
        echo "%source:\=\\%\\thisNot"

    )> "%exclude%"

    forfiles /P "%source%" /C "cmd /c (echo @path|findstr /i /v /g:"%exclude%" >nul) && if @isdir==TRUE (mklink /d \"%target%\\\"@file @path) else (mklink \"%target%\\\"@file @path)"

    del "%exclude%" > nul