-3

I have a program that have ongoing export of files. Another program needs to import these files.

But it only need to import the newest file within a "filename/date range".

The filename contains a 6 digit number (casenumber) and a date+timestamp.

Example of file name:

732087_20190903114454043000

Picture for below color explanation.

enter image description here

Numbers in red are the casenumber.
Numbers in blue are the date (year/month/day).
Numbers in yellow are time (hour/minute/second/tenth or hundredth of a second.

Example of files created with ONLY same case number and Date stamp. (I isolated the range to only one Casenumber for this example)

732087_20190903114454043000.xml 18.979  03-09-2019 11:44    -a--
732087_20190903114452887000.xml 18.979  03-09-2019 11:44    -a--
732087_20190903114451713000.xml 18.979  03-09-2019 11:44    -a--
732087_20190903114446353000.xml 18.979  03-09-2019 11:44    -a--
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    -a--
732087_20190903114431784000.xml 18.979  03-09-2019 11:44    -a--
732087_20190903114424999000.xml 18.979  03-09-2019 11:44    -a--
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    -a--
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    -a--
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    -a--
732087_20190903114248766000.xml 18.979  03-09-2019 11:42    -a--
732087_20190903114245183000.xml 18.979  03-09-2019 11:42    -a--

If I want to only move the last file (newest) created: 732087_20190903114454043000.xml to a certain destination folder ex: n:\Outbox\Projectdata\ and the rest in the same Casenumber range should be moved to an sub folder in ex: n:\Outbox\Projectdata\old

A real example of several casenumbers created within same date and where the files I marked with bold should be moved to n:\Outbox\Projectdata\ the rest to 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    -a--
732087_20190903114451713000.xml 18.979  03-09-2019 11:44    -a--
732087_20190903114446353000.xml 18.979  03-09-2019 11:44    -a--
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    -a--
732087_20190903114431784000.xml 18.979  03-09-2019 11:44    -a--
732087_20190903114424999000.xml 18.979  03-09-2019 11:44    -a--
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    -a--
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    -a--
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    -a--
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--

Is it above possible, to get an VBA / Powershell or other code to look at this folder, lets say once every hour and perform the mentioned moved of ONLY newest file within each Casenumber/date?

Problems that may occur:

If "move" script is running and in the meantime/same instant a new exportfile is created, would the script take that file(s) into account, and treat it in next run. So it does not accidentally move the file to OLD folder.

Possible solution: When script is running at first, maybe move the files to a temp folder, so if a new export file is created, the files in the temp foler is isolated from newly created files. Newly created files that would be trteated in the next script run.

I found a topic but not sure if it is useable in any way: How do I write a Windows batch script to copy the newest file from a directory?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
muggi
  • 11
  • 3
  • 1
    [Group](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object) by casenumber. [Sort](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object) [each](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object) group and [pick](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object) the last element. Then move those files. – Ansgar Wiechers Sep 04 '19 at 11:54

1 Answers1

0

Found 2 powershell solutions that after editing a bit, can be used for this thank you to among others 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;

Second part, where code moves newest file from each subfolder, to another folder

<#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
muggi
  • 11
  • 3