You definitely do. You may restart with explaing what youâre actually trying to achieve. Try to explain the bigger picture - not the way you think you have to go to come to this bigger picture.
Depending on the content of your zip files you may need to extract each zip file to its own destination folder. If so need to create them before you run Expand-Archive.
pls allow me to explain;
i have a daily ftp transfer which provide a zip folder with updates by folder name & date
what i do manually now is extract that protected zip folder into my server and execute a exe file which takes these data and insert into my sql db.
so i have sorted the ftp file transfer automated to my ftp folder via batch script
now the second half of
copy from ftp folder to local server unzipped content into specific folder and run exe and send me email that it has been successful or notâŚdoes this make sense?
That actually raises more questions than it answers.
You get a daily file transfer. OK.
Is it only one file per day or more?
If itâs more than one file - could these files be imported at once or do they have to be imported separately?
If they have to be imported separatly does the order matter?
When you start your executable for the import - do you specify a particular file or will it grab the file or files from a given folder?
Does the import executable exits by itself when itâs finished?
Will it output a return code if the import was successful or not?
Depending on your answers, more questions may arise.
Is it only one file per day or more? Yes only 1 zip file a day named ending by date
When you start your executable for the import - do you specify a particular file or will it grab the file or files from a given folder? it will grab files from the given folder
Does the import executable exits by itself when itâs finished? yes its exits automatically .
Will it output a return code if the import was successful or not? it just generates a log file of the progress output time it took to process
You have a folder with 1 file. The name does not matter if itâs just one file anyway. You get the file with Get-ChildItem specifying the ftp download folder and save the metadata in a variable.
You create a new temporary folder using the name property of the metadata object of the zip file.
You extract the zip file with Expand-Archive to this newly created folder.
You start your import executable pointing to the extraction folder and wait for termination of the executable.
You parse the log file for the result youâre after
You send the email with the status
you clean up the zip file - or you move it to a backup folder for savety reasons - and the temporarily created extraction folder
⌠and youâre done.
Do ONE Step at a time. When this one step works as expected move on to the next.
You have a folder with 1 file. The name does not matter if itâs just one file anyway. You get the file with Get-ChildItem specifying the ftp download folder and save the metadata in a variable.
this is where the issue is, it keeps 1 weeks files and get over written so thats where the wildcard doesnt work as you mentioned earlierâŚ
for item 1-3 i presume it would be something like this, please correct me if im wrong;
You say â⌠it keeps 1 weeks files âŚâ ⌠what is " it "? And why does it keep the files? And when the files have a date in their name why do they get overwritten?
It depends pretty much on the zip file. If it has a folder structure in it you could use this, but the extracted files will always end up in the folder you specified.
let me try editing tonight the rsync script of the folder to delete source that way I would only receive latest file, and only remain the latest single file from sourceâŚthat may help my situation I presume
Thatâs actually not necessary. PowerShell is easily able to grab only the newest file from a folder but because I donât know your environment I couldnât know that you need to take care about this detail. Thatâs what I meant with
I know it might be anoying but since I cannot see your screen and I cannot read you mind I cannot recommend to deal with something I donât know of.
Hereâs another suggestion combining information you gave and infomration I assumed because you did not provide yet:
$FTPDownloadFolder = '\\x.x.x.x\SanctionList'
$DestinationRoot = 'D:\Omni_DataMart_Lexisnexis\DataSource'
Get-ChildItem -Path $FTPDownloadFolder -Filter 'JAG01TD_WorldCompliancePlus*.Zip' -File |
Sort-Object -Property LastWriteTime |
Select-Object -Last 1 |
ForEach-Object {
$DestinationPath = Join-Path -Path $DestinationRoot $_.BaseName
if (-not (Test-Path -Path $DestinationPath)) {
New-Item -Path $DestinationPath -ItemType Directory |Out-Null
}
Expand-Archive -Path $_.FullName -DestinationPath $DestinationPath
<# Assuming your executable file can take a folder as parameter where
it expects the files to import #>
& 'Path to your executable' $DestinationPath
<# Here you could parse the log file and extract the info you're after.
When finished you can clean up the temporarily created folder #>
Remove-Item -Path $DestinationPath -Recurse -Force
}
This works but do allow me to explain what happens,
The file being copies is password protected so unable to extract it completely, and the exe file immediately executes despite the file not unzipped. It extracts into a folder instead of the content to be dumped into destination, and this error below is thrown;
Exception calling "ExtractToFile" with "3" argument(s): "The archive entry was compressed using an unsupported compression method."
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:1059 char:25
+ ... [System.IO.Compression.ZipFileExtensions]::ExtractToFile( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidDataException
So I see three options: Get the zip without password protection, extract the zip beforehand manually and only automate the rest or use a third party tool like 7-Zip whatâs able to extract password protected files from the command line.