使用副本保留/重命名重复的项目 [英] Keep/Rename Duplicate Items with Copy

查看:90
本文介绍了使用副本保留/重命名重复的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PowerShell脚本,该脚本可以展平目录结构并复制其中包含的所有文件.

I have a PowerShell script that flattens a directory structure and copies all files contained within.

ls $srcdir -Recurse | Where-Object {
    $_.PSIsContainer -eq $false
} | foreach($_) {
    cp $_.Fullname $destdir -Force -Verbose
}

然后我将其放入函数中.

I then took this and put it into a function.

function mcopy($srcdir, $destdir) {
    ls $srcdir -Recurse | Where-Object {
        $_.PSIsContainer -eq $false
    } | foreach($_) {
        cp $_.Fullname $destdir -Force -Verbose
    }
}

我现在正在寻找一种处理函数中重复文件名的好方法. 处理"是指如果目标目录中已经存在具有该文件名的文件,则通过在文件名中添加数字来逐步更改文件名.

I am now looking for a good way to handle duplicate file names within the function. By "handle" I mean incrementally altering the filename by adding a number to the file name if a file with that file name already exists in the destination directory.

我在搜索中遇到了这个问题

I came across this in my search:

$SourceFile = "C:\Temp\File.txt"
$DestinationFile = "C:\Temp\NonexistentDirectory\File.txt"

if (Test-Path $DestinationFile) {
    $i = 0
    while (Test-Path $DestinationFile) {
        $i += 1
        $DestinationFile = "C:\Temp\NonexistentDirectory\File$i.txt"
    }
} else {
    New-Item -ItemType File -Path $DestinationFile -Force
}

Copy-Item -Path $SourceFile -Destination $DestinationFile -Force

我正在尝试统一这两个脚本中的概念,但是到目前为止,我一直没有成功.

I am trying to unify the concepts in both of these scripts but I have been unsuccessful so far.

function mcopy($srcdir, $destdir) {
    ls $srcdir -Recurse | Where-Object {
        $_.PSIsContainer -eq $false
    } | foreach($_) {
if (Test-Path $_.fullname) {
    $i = 0
    while (Test-Path $_.fullname) {
        $i += 1
        $DestinationFile = "C:\Temp\NonexistentDirectory\File$i.txt"
    }
} else {
    New-Item -ItemType File -Path $_.fullname -Force
}

Copy-Item -Path $SourceFile -Destination $_.fullname -Force
    }
}    

我认为我遗漏了一些明显的东西,或者由于某处的误解而做错了一些明显的错误.

I think I am missing something obvious or I've done something obviously wrong due to a misunderstanding somewhere.

工作替代解决方案(robocopy或其他解决方案)也是可以接受的,只要它既可以平滑目录结构又可以很好地处理重复的文件名(请参见上面的"well"的定义).

Working substitute solutions (robocopy or other) are also acceptable as long it both flattens the directory structure and handles duplicate file names well (please see definition of "well" further above).

推荐答案

由于我提出这个问题时收到的建议,我发现自己犯了一些简单的错误,只需要进一步完善脚本即可.

Thanks to the advice I received when I asked this question I found I was making a few simple errors and I just needed to refine the scripting a little bit more.

更正后的脚本如下.到目前为止,它已经对较小的数据集进行了处理.我将很快在更大的目录上对其进行测试!

The corrected script is below. Thus far it has worked a treat on smaller data sets. I will be testing it on some larger directories soon!

function mcopy($srcdir,$destdir)
{
ls $srcdir -recurse | Where-Object { $_.PSIsContainer -eq $false } | foreach($_) {
$SourceFile = $_.FullName
$DestinationFile = $destdir+$_
If (Test-Path $DestinationFile) {
    $i = 0
    While (Test-Path $DestinationFile) {
        $i += 1
        $DestinationFile = $destdir+$_+$i
    }
} Else {
    New-Item -ItemType File -Path $DestinationFile -Force
}
Copy-Item -Path $SourceFile -Destination $DestinationFile -verbose -Force
}
}

mcopy -srcdir C:\copyme\ -destdir C:\copyharder\

这篇关于使用副本保留/重命名重复的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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