steve
steve

Reputation: 235

batch create symlinks of all files/folders in a directory

I have a directory with many files and folders and I'd like to make symlinks of all files and folders in that directory to another folder but exclude one folder

Any suggestions?

Upvotes: 2

Views: 8838

Answers (1)

MC ND
MC ND

Reputation: 70961

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

EDIT - Updated to allow "easily" add of multiple excludes, using /G:file if findstr command filter the file/folder list

@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

Upvotes: 6

Related Questions