如何使用命令提示符或power shell为多个文件夹中的多个文件创建符号链接? [英] How to create symbolic links for multiple files in multiple folders using command prompt or power shell?

查看:173
本文介绍了如何使用命令提示符或power shell为多个文件夹中的多个文件创建符号链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主要文件夹,在不同的驱动器中有很多子文件夹。必须为第二个文件夹中的所有文件创建符号链接。

I have two main folders which have a lot of sub-folders in different drives. Have to create symbolic link for all files in the second folder into the first one.

C:\folderC>tree /f
C:.
├───folder1
│       file1.txt
│       file3.txt
│
└───folder2
        file1.txt
        file3.txt

D:\folderD>tree /f
D:.
├───folder1
│       file2.txt
│
└───folder2
        file2.txt

使用两个命令的结果:

C:\>mklink C:\folderC\folder1\file2.txt D:\folderD\folder1\file2.txt
symbolic link created for C:\folderC\folder1\file2.txt <<===>> D:\folderD\folder1\file2.txt

C:\>mklink C:\folderC\folder2\file2.txt D:\folderD\folder2\file2.txt
symbolic link created for C:\folderC\folder2\file2.txt <<===>> D:\folderD\folder2\file2.txt

C:.
├───folder1
│       file1.txt
│       file2.txt
│       file3.txt
│
└───folder2
        file1.txt
        file2.txt
        file3.txt

它对于所有文件用几个命令,而不是手动为每个文件编写代码?

How to make it for all files with a few commands instead of writing the code manually for each file?

PS:首先,我想使用硬链接,但似乎是不可能的。

PS: Firstly I wanted to use hard links but it seems it is not possible.

C:\>mklink /h C:\folderC\folder2\file2.txt D:\folderD\folder2\file2.txt
The system cannot move the file to a different disk drive.


推荐答案



You can try something link this:

function createSymbolicLinks ($source, $destination, [switch]$recurse) {
    Get-ChildItem $source -Recurse:$recurse | ? { !$_.PSISContainer } | % {
        $destpath = $_.Fullname -replace [regex]::Escape($source), $destination
        if(!(Test-Path (Split-Path $destpath))) {
            #Create missing subfolders
            New-Item (Split-Path $destpath) -ItemType Directory -Force | Out-Null
        }
        cmd /c mklink $destpath $($_.FullName) | Out-Null
    }
}

#Create symbolic links in c:\folderC for all files in d:\folderD(with recursive search)
createSymbolicLinks -source d:\folderD -destination c:\folderC -recurse

如果 c:\folderc 中已存在相同的文件名。所以如果你需要用 d:\folderd 的符号链接替换 c:\folderc ,您需要扩展它以删除现有文件。

I believe this will fail if the same filename already exists in c:\folderc. So if you need to replace a file in c:\folderc with a symbolic link from d:\folderd, you need to extend it to remove the existing file.

更新:这只会下降一级与 recurse 选项。这不是最漂亮的解决方案,但它应该工作。

UPDATE: This will only go down one level with the recurseoption. It's not the prettiest solution, but it should work.

function createSymbolicLinks ($source, $destination, [switch]$recurse) {
    Get-ChildItem $source | % { 
        if($_.PSIsContainer -and $recurse) { 
            Get-ChildItem $_.FullName
        } else { 
            $_
        }
    } | ? { !$_.PSIsContainer } | % { 
        $destpath = $_.Fullname -replace [regex]::Escape($source), $destination
        if(!(Test-Path (Split-Path $destpath))) {
            #Create missing subfolders
            New-Item (Split-Path $destpath) -ItemType Directory -Force | Out-Null
        }
        cmd /c mklink $destpath $($_.FullName) | Out-Null
    }
}

#Create symbolic links in c:\folderC for all files in d:\folderD(with recursive search)
createSymbolicLinks -source d:\folderD -destination c:\folderC -recurse

这篇关于如何使用命令提示符或power shell为多个文件夹中的多个文件创建符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆