首页 > 解决方案 > 将具有相同文件名(“前缀”+日期)的多个文件中的最新文件移至文件夹

问题描述

我有一个持续导出文件的程序。另一个程序需要导入这些文件。

但它只需要导入“文件名/日期范围”内的最新文件。

文件名包含一个 6 位数字(案例编号)和一个date+timestamp.

文件名示例:

732087_20190903114454043000

图片为下面的颜色解释。

在此处输入图像描述

红色数字是案件编号。
蓝色数字是日期(年/月/日)。
黄色数字是时间(小时/分钟/秒/十分之一或百分之一秒。

仅使用相同案例编号和日期戳创建的文件示例。(对于这个例子,我将范围隔离为一个 Casenumber)

732087_20190903114454043000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114452887000.xml 18.979 03-09-2019 11:44--
732087_20190903114451713000.xml 18.979 03-09-2019 11:44-a--
732087_20190903114446353000.xml 18.979 03-09-2019 11:44--
732087_20190903114446161000.xml 18.979 03-09-2019 11:44-a--
732087_20190903114445013000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114442719000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114439141000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114433660000.xml 18.979 03-09-2019 11:44--
732087_20190903114431784000.xml 18.979 03-09-2019 11:44--
732087_20190903114424999000.xml 18.979 03-09-2019 11:44--
732087_20190903114423845000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114422669000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114421406000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114416004000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114414820000.xml 18.979 03-09-2019 11:44--
732087_20190903114410392000.xml 18.979 03-09-2019 11:44-a--
732087_20190903114405990000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114405784000.xml 18.979 03-09-2019 11:44--
732087_20190903114404612000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114357807000.xml 18.979 03-09-2019 11:43 -a--
732087_20190903114353325000.xml 18.979 03-09-2019 11:43 -a--
732087_20190903114346777000.xml 18.979 03-09-2019 11:43--
732087_20190903114248766000.xml 18.979 03-09-2019 11:42 -a--
732087_20190903114245183000.xml 18.979 03-09-2019 11:42-a--

如果我只想将创建的最后一个文件(最新)移动: 732087_20190903114454043000.xml到某个目标文件夹 ex:n:\Outbox\Projectdata\ 并且相同 Casenumber 范围内的其余文件应移动到 ex 中的子文件夹:n:\Outbox\Projectdata\old

在同一日期内创建的几个案例编号的真实示例,我用粗体标记的文件应移至n:\Outbox\Projectdata\其余文件n:\Outbox\Projectdata\old.

731218_20190903131544122000.xml 17.224 03-09-2019 13:15 -a--
731218_20190903131108622000.xml 17.224 03-09-2019 13:11 -a--
731218_20190903130833778000.xml 17.224 03-09-2019 13:08 -a--
731218_20190903130819889000.xml 17.224 03-09-2019 13:08 -a--
731218_20190903130332272000.xml 17.224 03-09-2019 13:03 -a--
731218_20190903130329390000.xml 17.224 03-09-2019 13:03 -a--
734107_20190903125649782000.xml 1.833 03-09-2019 12:56-a-- 732087_20190903114454043000.xml 
18.979 03-09-2019 11:44 -a--
732087_20190903114452887000.xml 18.979 03-09-2019 11:44--
732087_20190903114451713000.xml 18.979 03-09-2019 11:44-a--
732087_20190903114446353000.xml 18.979 03-09-2019 11:44--
732087_20190903114446161000.xml 18.979 03-09-2019 11:44-a--
732087_20190903114445013000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114442719000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114439141000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114433660000.xml 18.979 03-09-2019 11:44--
732087_20190903114431784000.xml 18.979 03-09-2019 11:44--
732087_20190903114424999000.xml 18.979 03-09-2019 11:44--
732087_20190903114423845000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114422669000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114421406000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114416004000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114414820000.xml 18.979 03-09-2019 11:44--
732087_20190903114410392000.xml 18.979 03-09-2019 11:44-a--
732087_20190903114405990000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114405784000.xml 18.979 03-09-2019 11:44--
732087_20190903114404612000.xml 18.979 03-09-2019 11:44 -a--
732087_20190903114357807000.xml 18.979 03-09-2019 11:43 -a--
732087_20190903114353325000.xml 18.979 03-09-2019 11:43 -a--
732087_20190903114346777000.xml 18.979 03-09-2019 11:43--
732087_20190903114248766000.xml 18.979 03-09-2019 11:42 -a--
732087_20190903114245183000.xml 18.979 03-09-2019 11:42-a--
731054_20190903110744981000.xml 18.851 03-09-2019 11:07 -a-- 
731218_20190903102953782000.xml 17.224 03-09-2019 10:29 -a--
731218_20190903102939235000.xml 17.224 03-09-2019 10:29-a--
731218_20190903102939093000.xml 17.224 03-09-2019 10:29 -a--
731218_20190903102936730000.xml 17.224 03-09-2019 10:29-a--
731218_20190903102928927000.xml 17.224 03-09-2019 10:29-a--

上面是否有可能,让 VBA / Powershell 或其他代码查看这个文件夹,让我们说每小时一次并在每个 Casenumber/date 中执行提到的仅最新文件的移动?

可能出现的问题:

如果“移动”脚本正在运行,同时/同时创建了一个新的导出文件,脚本是否会考虑该文件,并在下次运行时处理它。所以它不会意外地将文件移动到 OLD 文件夹。

可能的解决方案:最初运行脚本时,可能会将文件移动到临时文件夹,因此如果创建了新的导出文件,则临时文件夹中的文件与新创建的文件是隔离的。新创建的文件将在下一次脚本运行中进行处理。

我找到了一个主题,但不确定它是否可以以任何方式使用: 如何编写 Windows 批处理脚本来复制目录中的最新文件?

标签: vbapowershell

解决方案


找到了 2 个 powershell 解决方案,经过一番编辑,可以用于此,感谢 ChristianB & ArcSet & user3019391 等:

# Copy files without renaming files
# creates foldernames from part of filename before underscore.
# copying files and grouping them in the folders created, matching the first part of filename.


$SourceFolder = "C:\simtest\"
$targetFolder = "C:\simtest2\"

$numFiles = (Get-ChildItem -Path $SourceFolder -Filter *.xml).Count
$i = 0

clear-host;
Write-Host 'This script will copy ' $numFiles ' files from ' $SourceFolder ' to ' $targetFolder
Read-host -prompt 'Press enter to start copying the files'

Get-ChildItem -Path $SourceFolder -Filter *.xml | % { 
    [System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_", "\"))

    # keeping filesnames as original with 2 lines #
    $destination = [System.IO.Path]::GetDirectoryName($destination.FullName)
    $destination = $destination.FullName + "\" + $_.Name
    ## end of 2 lines 

    if (!(Test-Path -Path $destination.Directory )) {
        New-item -Path $destination.Directory.FullName -ItemType Directory 
    }
    [int]$percent = $i / $numFiles * 100

    copy-item -Path $_.FullName -Destination $Destination.FullName
    Write-Progress -Activity "Copying ... ($percent %)" -status $_  -PercentComplete $percent -verbose
    $i++
}
Write-Host 'Total number of files read from directory '$SourceFolder ' is ' $numFiles
Write-Host 'Total number of files that was copied to '$targetFolder ' is ' $i
Read-host -prompt "Press enter to complete..."
clear-host;

第二部分,代码将每个子文件夹中的最新文件移动到另一个文件夹

<#Gets subfolders

Gets all files in subfolders

Sorts files by Creation Date into a array

Gets first Entry

Moves file to Destination directory

*It will overwrite files with the same name in the destination folder #>


Function Get-LatestFiles($SourceFolder,$Destination){
    $Subfolders = Get-ChildItem $SourceFolder -Directory
    [System.Collections.ArrayList]$SubFoldersExpanded = new-object System.Collections.ArrayList
    Foreach($SubFolder in $SubFolders){
        $SubFolderExpanded =  $Subfolder | %{(Get-ChildItem $_.FullName -File -Depth 1 | Sort-Object -Property CreationTime -Descending)}
        if($SubFolderExpanded.Count -gt 0){
            $SubFolderExpanded[0] | %{Move-Item $_.FullName -Destination $Destination -force}
        }
    }
}
Get-LatestFiles -SourceFolder C:\Simtestsubfolders\simtest2 -Destination C:\SimtestsubfoldersDest

推荐阅读