Edited:
Originally, my question was why the first piece of code wont work. the unzip action was working if I run it on it own on on single file outside the loop. but once I wrapped it round with loop, it won't work, and there was no red error either.
Thansk @nemze , his/her answer inspired me to change the my code from:
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
ls -Path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -Path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = '"$zipFolderChild"+"\"+"Data.zip"'
$command = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword $zipFile"
iex $command
#file rename command that I have not written yet
}
To :
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
ls -Path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -Path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = "$zipFolderChild"+"\"+"Data.zip"
$command = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword ""$zipFile"""
iex $command
#file rename command that I have not written yet
}
By moving the $zipFile
definition outside of the ForEach
loop, this works!
I think my 2nd obstacle now move to getting my files renamed inside the loop.
What I am trying to achive:
The amended code is still reading $zipFolderChild
as the full path, how can I extract the folder name ONLY instead?
EDIT3:
Trying to get the rename statment into the loop, but not sure how to get the -NewName
argument work, $zipFolderChild.Name.xls
clear doesn't work. Also tried:
$folder= $zipFolderChild.Name
#file rename command that I have not written yet
$rename = "-path", "$zipOutPath\Data.xls" ,"-NewName", "$folder.xls"
& Rename-Item @rename
inside the loop, not working either.
FINALLY WORKING:
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
ls -path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = "$zipFolderChild\Data.zip"
$cmd = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword ""$zipFile"""
iex $cmd
$folder= $zipFolderChild.Name
$xlsFile = "$zipOutPath\Data.xls"
$NewName = "$zipOutPath\$folder.xls"
& Rename-Item -Path "$zipOutPath\Data.xls" -NewName $NewName
}
This is working for me:
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
Get-ChildItem -Path $zipFolderRoot -Exclude Unzip -Directory
Foreach ($zipFolderChild in (Get-ChildItem -Path $zipFolderRoot -Exclude Unzip))
{
$zipFile = "$zipFolderChild\Data.zip"
$command = "x", "$zipFile", "-p$zipFilePassword", "-y", "-o$zipOutPath"
& $7ZipPath @command
#file rename command that I have not written yet
}
I used splatting for $command
.
Edit: some examples for OP second question.
Try both examples and see what happens.
First:
$i = 0
Foreach ($zipFolderChild in (Get-ChildItem -Path $zipFolderRoot -Exclude Unzip))
{
$zipFolderChild.name
Write-Output "step"
}
Second:
$i = 0
$folder = Get-ChildItem -Path $zipFolderRoot -Exclude Unzip
Foreach ($zipFolderChild in $folder)
{
$folder[$i].name
Write-Output $i
$i++
}