How to add a timestamp when moving the XLSX files to Archive

Viewed 38

How to add a timestamp when moving the XLSX files to Archive? I only want to add it after it converts to a csv file and then when I am moving this to an archive folder.

Function ExcelToCsv ($File) {
    $myDir = "\\emsfile1\Users Shared Folders\Woodgrain_PO_Automation\"
    $excelFile = "$myDir\" + $File + ".xlsx"
    $Excel = New-Object -ComObject Excel.Application
    $wb = $Excel.Workbooks.Open($excelFile)
    foreach ($ws in $wb.Worksheets) {
        $ws.SaveAs("$myDir\" + $File + ".csv", 6)
    }
    $wb.Close($true)
    $Excel.Quit()
}

$FileName = "Woodgrain_PO_Inbound"
ExcelToCsv -File $FileName

#Move files to XLSX Files to Archive
$file = "\\emsfile1\Users Shared Folders\Woodgrain_PO_Automation\*.xlsx"
$dest = "\\emsfile1\Users Shared Folders\Woodgrain_PO_Automation\Archive"
Move-Item $file -destination $dest -force
1 Answers

Try updating the LastWriteTime property before copying the files:

# get the list of new files
$files = Get-Item "\\emsfile1\Users Shared Folders\Woodgrain_PO_Automation\*.xlsx"

# update the timestamp to current
$files | foreach {$_.LastWriteTime = (Get-Date)}

# move files to XLSX Files to Archive
$dest = "\\emsfile1\Users Shared Folders\Woodgrain_PO_Automation\Archive"
$files | Move-Item -Destination $dest
Related