我有数十万个文件分布在许多外部磁盘和计算机磁盘中,并且许多文件是重复的。这个混乱是由我为安全建议创建副本造成的。有时我会更改组织的目录结构,但不会在其他有副本的地方进行复制。
现在,我有一个巨大的磁盘,几乎所有我真正需要的备份和镜像都在云中。
我想要一种从大磁盘中已经存在的所有分布式磁盘中删除所有内容的方法。
让我展示一下场景:
旧磁盘 1:
/code/{manystructures}/{manyfiles}
/docs/{manystructures}/{manyfiles}
旧磁盘 2:
/dev/{another_structures}/{same_files_different_names}
/documents/{another_structures}/{same_files_different_names}
新的巨大磁盘:
/home/username/code/{new_strutucture}/{new_files}
/home/username/documents/{new_strutucture}/{new_files}
任何人都知道一种工具或一种方法来执行诸如“查找 OldDisk1 上已经在 NewHugeDisk 中的所有文件并删除”之类的操作?
我查看了许多免费和付费的工具(Windows、Mac 和 Linux,因为我在两者上都有这个问题),但没有运气。
ideia 将创建一个代码来做到这一点,但我不是开发人员。我可以做小而简单的代码,但是这种代码,我认为对我来说会很复杂。
我将不胜感激任何帮助或任何想法。
假设您可以使用 Windows 作为整个过程的操作系统,并且您不喜欢Free Duplicate File Finder(从未尝试过,但发现这里提到了它),您可以使用 PowerShell 以相对较少的努力实现您想要的。注意:我不是 PowerShell 的真正专家,所以我很确定有人可以改进我的代码。
只需打开 Powershell ISE(或者,如果您没有,请使用记事本),将以下代码复制并粘贴到其中,并将生成的文件保存为 *.ps1。您还必须将$oldpath
's 和$newpath
' 值更改为您的目录 - 只需将您的路径放在引号之间。
# Search-and-Destroy-script
# Get all files of both code-directories:
$oldpath = "Disk1:\code"
$newpath = "DiskNew:\code"
$files_old = Get-ChildItem -Path $oldpath -Recurse -File
$files_new = Get-ChildItem -Path $newpath -Recurse -File
for($i=0; $i -lt $files_old.length; $i++){
$j=0
while($true){
# if last edit time is the same and file-size is the same...
if($($files_old[$i]).length -eq $($files_new[$j]).length -and $($files_old[$i]).lastWriteTime -eq $($files_new[$j]).lastWriteTime){
# Get File-Hashes for those files (SHA1 should be enough)
$files_old_hash = Get-FileHash -Path $($files_old[$i]).FullName -Algorithm SHA1 | ForEach-Object {$_.Hash}
$files_new_hash = Get-FileHash -Path $($files_new[$j]).FullName -Algorithm SHA1 | ForEach-Object {$_.Hash}
# if hashes also are the same...
if($files_old_hash -eq $files_new_hash){
# remove the old file (-Confirm can be removed so you don't have to approve for every file)
# if you want to check the files before deletion, you could also just rename them (here we're adding the suffix ".DUPLICATE"
# Rename-Item -Path $($files_old[$i]).FullName -NewName "$($files_old[$i]).Name.DUPLICATE"
Remove-Item -Path $($files_old[$i]).FullName -Confirm
Write-Host "DELETING`t$($files_old[$i]).FullName" -ForegroundColor Red
break
}
# if files aren't the same...
}else{
# if old_file is compared to all new_files, check next old file
if($j -ge $files_new.length){
break
}
}
$j++
}
}
然后启动脚本(例如通过右键单击) - 如果失败,请确保您ExecutionPolicy
已设置(https://superuser.com/a/106363/703240)。
我使用几乎相同的脚本来检查已复制的文件(但可能更改了名称)。这段代码假设只有文件名不同,内容不同。即使将文件复制到新路径后,上次编辑时间通常也保持不变 - 与创建时间不同。如果内容不同,我的解决方案会严重失败 - 您可以使用文件的不同独特属性(但哪个?)或声明例如只有文件 tat 比新文件更小或更旧(再次考虑编辑时间)应该是删除。
脚本的作用:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句