Currently, I am writing a script that moves PDF files that are wrongfully zipped to a certain folder. These files will then get unzipped and the contained files will get moved to a different folder. This all works at the moment.
What i need to add now is the following: I want to make it so that it automatically renames the contained file to the name the original ZIP file had. So when i have a ZIP file called "testzip" and the contained file is called "testcontained", i want it to rename "testcontained" to "testzip".
This would be useful in keeping track of which files are associated with which ZIP-files.
Param (
$SourcePath = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\struktur_id_1225\',
$ZipFilesPath = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip',
$UnzippedFilesPath = 'Z:\Documents\16_Med._App\Auftraege\PDFPrzemek\Zip\unzipped'
)
$VerbosePreference = 'Continue'
#region Test folders
@($SourcePath, $ZipFilesPath, $UnzippedFilesPath) | Where-Object {
-not (Test-Path -LiteralPath $_)
} | ForEach-Object {
throw "Path '$_' not found. Make sure that the folders exist before running the script."
}
#endregion
#region Get all files with extension .pdf
$Params = @{
Path = Join-Path -Path $SourcePath -ChildPath 'ext_dok'
Filter = '*.pdf'
}
$PDFfiles = Get-ChildItem @Params
Write-Verbose "Got $($PDFfiles.count) files with extension '.pdf' from '$($Params.Path)'"
#endregion
#region Move PDF and HL7 files
$MDMpath = Join-Path -Path $SourcePath -ChildPath 'MDM'
foreach ($PDFfile in ($PDFfiles | Where-Object {
(Get-Content $_.FullName | Select-Object -First 1) -like 'PK*'})
) {
$MoveParams = @{
Path = $PDFfile.FullName
Destination = Join-Path -Path $ZipFilesPath -ChildPath ($PDFfile.BaseName + '.zip')
}
Move-Item @MoveParams
Write-Verbose "Moved file '$($MoveParams.Path)' to '$($MoveParams.Destination)'"
$GetParams = @{
Path = Join-Path -Path $MDMpath -ChildPath ($PDFfile.BaseName + '.hl7')
ErrorAction = 'Ignore'
}
if ($HL7file = Get-Item @GetParams) {
$MoveParams = @{
Path = $HL7file
Destination = $ZipFilesPath
}
Move-Item @MoveParams
Write-Verbose "Moved file '$($MoveParams.Path)' to '$($MoveParams.Destination)$($HL7file.Name)'"
}
}
#endregion
#region Unzip files
$ZipFiles = Get-ChildItem -Path $ZipFilesPath -Filter '*.zip' -File
foreach ($ZipFile in $ZipFiles) {
$ZipFile | Expand-Archive -DestinationPath $UnzippedFilesPath -Force
Write-Verbose "Unzipped file '$($ZipFile.Name)' in folder '$UnzippedFilesPath'"
}
#endregion
As you might have noticed, i am unzipping PDF files, which is kinda weird. Let me explain: We have a lot PDF's that cant be opened. After running some checks, we came to the conclusion that some of them are wrongfully Zipped. To find which ones, i check the first bytes of the PDF files. If they contain "PK*", they are ZIP-Files.
To unzip them with the script, i need to rename them to change the file extension to ".zip". If the files have ".pdf", then you cant unzip them, even if they are technically zipped.
So, does anyone have an idea on what i could do so that it gets automatically renamed?
Thanks in advance! if theres any missing info, let me know.
UPDATE
I have now tested something, but it isnt working. This is my attempt at making this work:
ForEach ($File in (Get-ChildItem $UnzippedFilesPath))
{ #rename unzipped file
Rename-Item $File.Name -NewName ($ZipFile.BaseName)
}
The issue is that i dont know how to call back to the original zip file. I dont think using $ZipFile works. This is the error i get:
Rename-Item : Cannot rename because item at 'test.txt' does not exist.
At line:4 char:2
+ Rename-Item $File.Name -NewName ($ZipFile.BaseName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand