国家和年份-上次修改日期

时间:2018-07-09 09:32:30

标签: powershell powershell-v2.0

我正在尝试按// GoogleMapComponent.jsx import React, { Component } from 'react' export default class GoogleMap extends Component { shouldComponentUpdate() { return false; } componentDidMount() { this.map = new google.maps.Map( this.refs.map, { center: {lat: this.props.lat, lng: this.props.lng}, zoom: 5 }) } componentWillReceiveProps(nextProps) { this.map.panTo({ lat: nextPros.lat, lng: nextPros.lng }); } render() { return ( <div id="googleMap" ref="map" /> ) } } // App.jsx import GoogleMap from './GoogleMapComponent' import React, { Component } from 'react' export default class App extends Component { constructor(props) { super(props); this.state = {lat: 30.397, lng: 130.876}; } render() { return ( <div style={{ height: '100%'}} > <button onClick={() => this.seState({ lat:50.712, lng: -30.08 }) }> Your Favourite Place </button> <GoogleMap lat={this.state.lat} lng={this.state.lng} /> </div> ) } } 将文件名和国家/地区分开,并希望按年份将剩余文件移动到存档文件夹,但只能过滤年份而不是国家/地区名称 代码:

LastWriteTime

输出为

Mode                LastWriteTime     Length  Name
----                -------------     ------  ----
-a---         6/25/2018  12:08 AM      31744  abc Italy_2016_2 - Copy - Copy.xls
-a---         6/25/2018  12:07 AM      31744  abc Italy_2016_2 - Copy.xls
-a---         6/25/2018  12:06 AM      31744  abc China_2017_1.xls
-a---         6/25/2018  12:07 AM      31744  abc NL_2017_2.xls
-a---         6/25/2018  12:05 AM      31744  abc Finland_2017_3.xls

在上面的数据中,第2行将被归档,因为意大利在2016年的最后写入时间是12:08。 我正在尝试通过$sourcedir = 'C:\Users\garang\Documents\input_files\Advisory_rate' $destdir = 'C:\Users\garang\Documents\input_files\Advisory_rate\Archive' Get-ChildItem -File -Path $sourcedir | Where-Object BaseName -match '_(20\d{2})_\d' | Group-Object $Matches[1] | ForEach-Object { $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 | Move-Item -Destination $destdir -Force } 拆分名称,但无法在上述条件下使用

Group-Object {$_.Basename.Split(' ')[2]}

我认为问题出在组对象$ Matches [1] | ...如果删除此查询没有给出错误...我是否必须定义此$ matches

also tried:Where-Object BaseName -match ' ([a-z]+_20\d{2})_\d' | &  Where-Object BaseName -match '_(20\d{2})_\d' |

$sourcedir = 'C:\Users\garang\Documents\input_files\Advisory_rate'
$destdir   = 'C:\Users\garang\Documents\input_files\Advisory_rate\Archive'
Get-ChildItem -File -Path $sourcedir |
Where-Object BaseName -match ' ([a-z]+_20\d{2})_\d' |
    Group-Object $Matches[1] |
    ForEach-Object {
        $_.Group | Sort-Object LastWriteTime -Descending |
            Select-Object -Skip 1 |
            Move-Item -Destination $destdir -Force 
    }

both conditions are tried separately gives the error message:

Cannot index into a null array.
At C:\Users\garang\Documents\input_files\Script\Tracker.ps1:3 char:1
+ Get-ChildItem -File -Path $sourcedir |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

尝试了新的正则表达式:

--Unable to find country and year when the file name is changed
abc N Refund Maker_Japan_2017_302413 modified date :26/06 10:20
abc N Refund Maker_Japan_2017_Extra  modified date:26/06 10:30

这就是预期的
好的...举一个新的例子...这些是四个文件...在这种情况下...需要按国家和年份分组... 文件将在那里,其他文件将移动该国家的档案 和年份...所以这里第二个和第三个文件将进入存档。...示例中,国家和年份的重复方式很简单,例如两次。和国家...

$sourcedir = 'C:\Users\garang\Documents\input_files\Advisory_rate'
$destdir   = 'C:\Users\garang\Documents\input_files\Advisory_rate\Archive'

Get-ChildItem -File -Path $sourcedir |
  Where-Object BaseName -match '_([a-z]+_20\d{2})_\d'|
    Group-Object {$Matches[1]} |
      ForEach-Object {
        $_.Group | Sort-Object LastWriteTime -Descending |
          Select-Object -Skip 1 |
            Move-Item -Destination $destdir -Force 
      }
but file is not moving
abc N Refund Maker_Japan_2017_302413 modified date :26/06 10:20
abc N Refund Maker_Japan_2017_Extra  modified date:26/06 10:30
abc N Refund Macker_Italy_2017_302413 modified date :26/06 10:20 
abc N Refund Macker_Italy_2017_extra modified date :26/06 10:30


i got new  logic  into it and getting output also how fix this in the query

   $Match = "[^_]+_[0-9]{4}"
$Lines = get-clipboard
$Results = $Lines | %{$_ -Match $Match | %{$Matches[0]}}

output:

Japan_2017
Japan_2017
Italy_2017
Italy_2017

2 个答案:

答案 0 :(得分:0)

对不起,我的错。您必须在组对象

中的$Matches[1]上加上花括号
$sourcedir = 'C:\Users\garang\Documents\input_files\Advisory_rate'
$destdir   = 'C:\Users\garang\Documents\input_files\Advisory_rate\Archive'

Get-ChildItem -File -Path $sourcedir |
  Where-Object BaseName -match ' ([a-z]+_20\d{2})_\d' |
    Group-Object {$Matches[1]} |
      ForEach-Object {
        $_.Group | Sort-Object LastWriteTime -Descending |
          Select-Object -Skip 1 |
            Move-Item -Destination $destdir -Force 
      }

经过不同路径测试

> ls
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2018-07-09     23:04                Archive
-a----       2018-06-25     12:06              6 abc China_2017_1.xls
-a----       2018-06-25     12:05              6 abc Finland_2017_3.xls
-a----       2018-06-25     12:08              6 abc Italy_2016_2 - Copy - Copy.xls
-a----       2018-06-25     12:07              6 abc NL_2017_2.xls


PoSh 23:04:43 A:\test_______________________________________________________________
> gci .\Archive\
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-06-25     12:07              6 abc Italy_2016_2 - Copy.xls

答案 1 :(得分:0)

另一个答案仅更改了细节(不明白为什么会有问题)

这些文件

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-06-26     10:20              6 abc N Refund Macker_Italy_2017_302413
-a----       2018-06-26     10:30              6 abc N Refund Macker_Italy_2017_xyz
-a----       2018-06-26     10:20              6 abc N Refund Maker_Japan_2017_302413
-a----       2018-06-26     10:30              6 abc N Refund Maker_Japan_2017_Extra

和此脚本:

## Q:\Test\2018\07\09\SO_51242305.ps1
$sourcedir = 'C:\Users\garang\Documents\input_files\Advisory_rate'
$destdir   = 'C:\Users\garang\Documents\input_files\Advisory_rate\Archive'

$sourcedir = 'A:\Test'
$destdir   = 'A:\Test\Archive'

Get-ChildItem -File -Path $sourcedir |
  Where-Object BaseName -match '_([a-z]+_20\d{2})_' |
    Group-Object {$Matches[1]} |
      ForEach-Object {
        $_.Group | Sort-Object LastWriteTime -Descending |
          Select-Object -Skip 1 |
            Move-Item -Destination $destdir -Force
      }

获取此输出:

> gci $sourcedir -r
    Verzeichnis: A:\Test

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2018-07-12     20:15                Archive
-a----       2018-06-26     10:30              6 abc N Refund Macker_Italy_2017_xyz
-a----       2018-06-26     10:30              6 abc N Refund Maker_Japan_2017_Extra

    Verzeichnis: A:\Test\Archive

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-06-26     10:20              6 abc N Refund Macker_Italy_2017_302413
-a----       2018-06-26     10:20              6 abc N Refund Maker_Japan_2017_302413