【问题标题】:PowerShell Iterate file contents matching filenames recursively in directoryPowerShell在目录中递归迭代匹配文件名的文件内容
【发布时间】:2017-05-17 21:34:18
【问题描述】:

我有一个这样组织的文件名列表:(search.txt)

2KJ34DS
JSD92NS
1JNKD8A
KO1N231
ASJDOA9

我有一个这样组织的目录:\\logs\

20161201
  .\
  K2J3N4K.xml
  SDJNFK2.wav
  ASDN1JE.html
20161202
20161203
20161204

目录中日期文件夹的内容是我需要根据 search.txt 文件中提供的列表匹配的文件名。

我需要一个 powershell 脚本,它可以遍历过时的文件夹并将找到的匹配文件复制到目标“\\logs\matches\”。它应该匹配任何文件扩展名。如果我可以指定“开始日期”和“结束日期”并且它只在开始日期和结束日期内的日期文件夹之间迭代,那将是一个额外的好处。

我不知道从哪里开始...有人可以帮忙吗?

【问题讨论】:

    标签: powershell match file-copying


    【解决方案1】:

    你好试试这样的东西(PowerShell v3 最低)

    $listfiles=get-content "c:\temp\search.txt"
    $destination="c:\logs\matches"
    $startdate=get-date -Year 2016 -Month 01 -Day 01 -Hour 0 -Minute 0 -Second 0
    $enddate=get-date -Year 2017 -Month 06 -Day 24 -Hour 0 -Minute 0 -Second 0
    
    Get-ChildItem -file -Recurse "c:\logs" | 
        where {$_.BaseName -in $listfiles -and $_.FullName -ne "$destination\$($_.Name)" -and $_.LastWriteTime -ge $startdate -and $_.LastWriteTime -le $enddate} | 
            Copy-Item  -Destination $destination -Force
    

    【讨论】:

    • 为什么选择 v5?您没有使用 v3 中没有的单一功能?
    • 你是对的;我错误地认为“-file 选项”从 v5 开始就处于活动状态,因为很少有人在这个网站上使用它,我的错。谢谢你的信息,我修改了我的出版物:)
    猜你喜欢
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2016-11-13
    • 2020-01-14
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多