我想从文件名中读取日期,并使用powershell保留最后5个日期文件

时间:2017-04-20 21:41:43

标签: powershell

我在特定文件夹中有一组文件, 我想从文件名中读取日期, 选择最后2个日期文件,并使用powershell将所有其他日期文件移动到另一个位置

下面是我有的文件名样本

Directory: E:\HOLDS\trim
Name                        
----                        
17988000412767900-20170402-T
17988000412770804-20170402-T
17988000412773204-20170402-T
17988000412792005-20170402-T
17988000412794300-20170402-T
17991325988242500-20170403-C
17991325988242800-20170403-C
17991325988243000-20170403-C
17991325988245000-20170403-C
17991325988245200-20170403-C
17992327574130910-20170404-T
17992327574131100-20170404-T
17992327574145005-20170404-T
17992327574145209-20170404-T
17992327574169106-20170404-T
17993057054385600-20170405-T
17993326857390200-20170405-R
17993327575638604-20170405-T
17993327575676304-20170405-T
17993327575835705-20170405-T
17993327575844703-20170405-T
17997018695202606-20170409-T
17998001450000100-20170409-C
17998001450001000-20170409-C
17998057920002100-20170409-R
17998119423714112-20170410-T
17998119423728401-20170410-T
17998282230003400-20170409-R
17998297810002500-20170409-R
17998327575543207-20170410-T
17998327575543708-20170410-T
17998327575546104-20170410-T
17998327575547600-20170410-T
17998327575591805-20170410-T

3 个答案:

答案 0 :(得分:0)

你有什么理由不能按上次写入时间进行过滤吗?

$sourcedir = "c:\scripts\"

$files = get-childitem $sourcedir | sort LastwriteTime

# last files
$files[-1].name
# second from last file
$files[-2].Name

答案 1 :(得分:0)

根据您的要求,如果您的文件名始终遵循相同的样式,我认为您可以使用此流程执行此操作: 1.定义一个Direcroty / Map 2.获取所有文件,然后循环文件&用' - '和&分隔文件名取第二个作为日期,然后将其添加到目录/地图中 3.移动所有不满足条件的文件

$maps = New-Object 'system.collections.generic.dictionary[[string],[system.collections.generic.list[string]]]';
$list = New-Object 'system.collections.generic.list[string]';

$files = ("17988000412767900-20170402-T",
"17988000412770804-20170402-T",
"17988000412773204-20170402-T",
"17988000412792005-20170402-T",
"17988000412794300-20170402-T",
"17991325988242500-20170403-C",
"17991325988242800-20170403-C",
"17991325988243000-20170403-C",
"17991325988245000-20170403-C",
"17991325988245200-20170403-C",
"17992327574130910-20170404-T",
"17992327574131100-20170404-T",
"17992327574145005-20170404-T",
"17992327574145209-20170404-T",
"17992327574169106-20170404-T",
"17993057054385600-20170405-T",
"17993326857390200-20170405-R",
"17993327575638604-20170405-T",
"17993327575676304-20170405-T",
"17993327575835705-20170405-T",
"17993327575844703-20170405-T",
"17997018695202606-20170409-T",
"17998001450000100-20170409-C",
"17998001450001000-20170409-C",
"17998057920002100-20170409-R",
"17998119423714112-20170410-T",
"17998119423728401-20170410-T",
"17998282230003400-20170409-R",
"17998297810002500-20170409-R",
"17998327575543207-20170410-T",
"17998327575543708-20170410-T",
"17998327575546104-20170410-T",
"17998327575547600-20170410-T",
"17998327575591805-20170410-T");

$files | %{
$date = $_.Split('-')[1];
if($maps.Keys.Contains($date)){
    $maps[$date].Add($_);
}
else{
    $maps.Add($date, $list);
    $maps[$date].Add($_);
}
}

输出将是:

Key   : 20170402
Value : {17988000412767900-20170402-T, 17988000412770804-20170402-T, 17988000412773204-20170402-T, 17988000412792005-20170402-T...}

Key   : 20170403
Value : {17988000412767900-20170402-T, 17988000412770804-20170402-T, 17988000412773204-20170402-T, 17988000412792005-20170402-T...}

Key   : 20170404
Value : {17988000412767900-20170402-T, 17988000412770804-20170402-T, 17988000412773204-20170402-T, 17988000412792005-20170402-T...}

Key   : 20170405
Value : {17988000412767900-20170402-T, 17988000412770804-20170402-T, 17988000412773204-20170402-T, 17988000412792005-20170402-T...}

Key   : 20170409
Value : {17988000412767900-20170402-T, 17988000412770804-20170402-T, 17988000412773204-20170402-T, 17988000412792005-20170402-T...}

Key   : 20170410
Value : {17988000412767900-20170402-T, 17988000412770804-20170402-T, 17988000412773204-20170402-T, 17988000412792005-20170402-T...}

然后你可以订购$ map并循环其中的所有文件的价值&搬出去。

答案 2 :(得分:0)

试试这个

 $yourdirpath="C:\Temp\yourfolder"
 $yourmovedir="C:\Temp\movedir"
 $numrow=0
 $currentdate=""

 Get-ChildItem $yourdirpath | 
  select FullName, @{N="DateFile";E={$_.Name.substring(18, 8)}} |
   sort DateFile -Descending | 
    foreach{

         if ($_.DateFile -ne $currentdate)
         {
             $numrow++
             $currentdate=$_.DateFile
         }

         [pscustomobject]@{FullName=$_.FullName;DateFile=$_.DateFile;numrow=$numrow}

    } | where numrow -gt 2 | foreach{move-item $_.FullName -Destination $yourmovedir }