Board logo

标题: [文件操作] 【已解决】批处理如何对相同文件名的文件保留最新修改时间的文件? [打印本页]

作者: 星空独子    时间: 2019-11-20 17:25     标题: 【已解决】批处理如何对相同文件名的文件保留最新修改时间的文件?

本帖最后由 星空独子 于 2019-11-22 15:10 编辑

在指定文件夹及其子文件夹下对相同文件名的文件保留最新创建时间的文件(相同文件名的文件在不同的子文件夹中,但是创建时间是不一样)
作者: zaqmlp    时间: 2019-11-20 17:54

本帖最后由 zaqmlp 于 2019-11-20 23:41 编辑
  1. @echo off
  2. mode con lines=3000
  3. set info=互助互利,支付宝扫码头像,感谢打赏
  4. rem 有问题,可加QQ956535081及时沟通
  5. title %info%
  6. set "folder=%~dp0"
  7. cd /d "%folder%"
  8. powershell -NoProfile -ExecutionPolicy bypass ^
  9.     $dic=New-Object 'System.Collections.Generic.Dictionary[string,Object]';^
  10.     $files=@(dir -liter '%folder%' -recurse^|?{($_.Name -ne '%~nx0') -and ($_ -is [System.IO.FileInfo])});^
  11.     for($i=0;$i -lt $files.length;$i++){^
  12.         $fname=$files[$i].Name.toLower();^
  13.         if(-not $dic.ContainsKey($fname)){^
  14.             [System.Collections.ArrayList]$arr=@();^
  15.             $dic.add($fname, $arr);^
  16.         };^
  17.         [void]$dic[$fname].add($files[$i]);^
  18.     };^
  19.     foreach($i in $dic.keys){^
  20.         $list=@($dic[$i]^|sort {$_.LastWriteTime} -des);^
  21.         write-host $list[0].FullName -ForegroundColor green;^
  22.         for($i=1;$i -lt $list.length;$i++){^
  23.             write-host $list[$i].FullName -ForegroundColor red;^
  24.             del -liter $list[$i].FullName -force;^
  25.         };^
  26.         write-host '';^
  27.     };
  28. :end
  29. echo;%info%
  30. pause
  31. exit
复制代码

作者: xczxczxcz    时间: 2019-11-20 17:55

so easy  让后面的大神给你写吧!
作者: 星空独子    时间: 2019-11-20 19:53

回复 2# zaqmlp


运行代码,出现无限循环
作者: zaqmlp    时间: 2019-11-20 23:41

回复 4# 星空独子

已修改
作者: terse    时间: 2019-11-21 11:21

可试一下 replace /u /s参数
作者: flashercs    时间: 2019-11-21 12:48

本帖最后由 flashercs 于 2019-11-21 12:50 编辑

当前目录和子目录
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. Powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. pause
  7. exit /b
  8. #>
  9. Get-ChildItem -Recurse -Force | Where-Object { -not $_.PSIsContainer } | Sort-Object -Descending -Property CreationTime |`
  10.   Group-Object -Property PSChildName | ForEach-Object {
  11.   for ($i = 1; $i -lt $_.Count; $i++) {
  12.     Remove-Item -LiteralPath $_.Group[$i].PSPath -Force -Verbose
  13.   }
  14. }
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2