2

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

有什么建议么?

4

1 回答 1

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 )"

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

@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
于 2013-11-06T07:53:34.930 回答