[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

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

在指定文件夹及其子文件夹下对相同文件名的文件保留最新创建时间的文件(相同文件名的文件在不同的子文件夹中,但是创建时间是不一样)

本帖最后由 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
复制代码
1

评分人数

提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

so easy  让后面的大神给你写吧!
QQ: 己阵亡
脚本优先 [PowerShell win10]

TOP

回复 2# zaqmlp


运行代码,出现无限循环

TOP

回复 4# 星空独子

已修改
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

可试一下 replace /u /s参数

TOP

本帖最后由 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. }
复制代码
微信:flashercs
QQ:49908356

TOP

返回列表