I have a folder and set of files like so:
- Root >
- Parent 1 >
- file 1 > export.zip > export.txt
- file 2 > export.zip > export.txt
- file 3 > export.zip > export.txt
- Parent 2 >
- file 1 > export.zip > export.txt
- file 2 > export.zip > export.txt
- file 3 > export.zip > export.txt
So the root folder contains hundreds of parent folders, each parent folder contains hundreds of files, each file contains a single zipfile with the title "export", and each zipfile contains a single .txt file, also named "export".
I need to get
- all of those .txt files out of the zipfiles,
- all of those .txt files named something unique using a known method, and
- and moved to a common folder location.
So far I have a few bits of the process working but nothing that does all three things. I can rename the zipfiles something unique, based on the file and parent names, by doing these two lines:
Get-ChildItem -LiteralPath 'C:\Insert Root Directory here' -Recurse | Rename-Item -NewName { $_.Parent.Name+'_'+$_.Name}
and then
Get-ChildItem -LiteralPath 'C:\Insert Root Directory here' -Filter *.zip -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}
Then I tried something I found on here to unzip the zipfiles, rename the .txt files they contain, and move them simultaneously to new location, but it is erroring with a "movefileinfoitemunauthorizedaccesserror microsoft.powershell.commands.moveitemcommand" error. I have tried tinkering around with it but I cannot get it to work. If remove everything after "#Move Our Temp Files" the error goes away but the outcomes is still not successful. I get a single .txt file named export in the $TempPath location. :
$ZipFilesPath = "C:\Insert Root Directory here";
#Unzip To Temp Folder Defined Here
$TempPath = "C:\Insert Location to put .txt files here"
#Create Our Temp File If It Doesnt Exist (Will Be Deleted Again)
If(!(test-path $TempPath))
{
New-Item -ItemType Directory -Force -Path $TempPath
}
$Shell = New-Object -com Shell.Application
$Location = $Shell.NameSpace($TempPath)
$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
$FileCounter = 1
foreach ($ZipFile in $ZipFiles) {
#Get The Base Filename without the Filepath
$ZipFileActualName = [io.path]::GetFileNameWithoutExtension($ZipFile.FullName)
write-host "File: " $ZipFileActualName
$ZipFolder = $Shell.NameSpace($ZipFile.fullname)
$Location.Copyhere($ZipFolder.items(), 1040)
#Move Our Temp Files
$txtFiles = Get-ChildItem $TempPath *.txt
$txtFiles |% {Move-Item $_.Fullname "$UnzipPath/$ZipFileActualName.txt"}
#Move Along to Next File
$FileCounter++
}
Can anyone help me achieve the above? Open to any and all ideas. Thanks!