Powershell + 7zip 批量提取和重命名 - 收到 "No files to process" 消息

Powershell + 7zip to batch extract and rename - getting "No files to process" message

已编辑:

最初,我的问题是为什么第一段代码不起作用。如果我 运行 在循环外的单个文件上自行解压缩操作,则解压缩操作有效。但是一旦我用循环把它包起来,它就不起作用了,也没有红色错误。

感谢@nemze,his/her 回答启发了我更改我的代码:

ZipPath = "C:zza"
$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 = "& ZipPath x -o'$zipOutPath' -y -p$zipFilePassword $zipFile"
iex $command
#file rename command that I have not written yet
}

收件人:

ZipPath = "C:zza"
$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 = "& ZipPath x -o'$zipOutPath' -y -p$zipFilePassword ""$zipFile"""
iex $command
#file rename command that I have not written yet
}

通过将 $zipFile 定义移到 ForEach 循环之外,这有效!

我认为我的第二个障碍现在转移到在循环内重命名我的文件。

我想要达到的目标:

修改后的代码仍然读取 $zipFolderChild 作为完整路径,我怎样才能只提取 文件夹名称

编辑3:

试图将重命名语句放入循环中,但不确定如何使 -NewName 参数起作用,$zipFolderChild.Name.xls clear 不起作用。也试过:

$folder= $zipFolderChild.Name
#file rename command that I have not written yet
$rename = "-path", "$zipOutPath\Data.xls" ,"-NewName", "$folder.xls"
& Rename-Item @rename

在循环内,也不起作用。

终于开始工作了:

ZipPath = "C:zza"
$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 = "& ZipPath 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
}

这个效果更好吗?

& ZipPath x -o$zipOutPath -y -p$zipFilePassword $zipFile

$zipFile 不会获取带单引号的变量:

 $zipFile = "$zipFolderChild" + "\" + "Data.zip"

这对我有用:

ZipPath = "C:zza"
$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"
    & ZipPath @command
    #file rename command that I have not written yet
}

我用 splatting 代替 $command

编辑:OP 第二个问题的一些示例。

试试这两个例子,看看会发生什么。

第一个:

$i = 0
Foreach ($zipFolderChild in (Get-ChildItem -Path $zipFolderRoot -Exclude Unzip))
{
    $zipFolderChild.name
    Write-Output "step"
}

第二个:

$i = 0
$folder = Get-ChildItem -Path $zipFolderRoot -Exclude Unzip
Foreach ($zipFolderChild in $folder)
{      
    $folder[$i].name
    Write-Output $i
    $i++
}