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



编辑:

最初,我的问题是为什么第一件代码无法使用。如果我在循环外部的单个文件上自行运行它,则UNZIP操作正在起作用。但是,一旦我用循环包裹它,它就不会可行,也没有红色错误。

thansk @nemze,他/她的答案启发了我从:

中更改我的代码
$7ZipPath = "C:7z7za"
$zipFolderRoot = "Z:long folder pathTest FolderUnzip Test"
$zipOutPath = "Z:long folder pathTest FolderUnzip TestUnzip"
$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:7z7za"
$zipFolderRoot = "Z:long folder pathTest FolderUnzip Test"
$zipOutPath = "Z:long folder pathTest FolderUnzip TestUnzip"
$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
}

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

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

我想实现的目标:

  • data.xls从20181001文件夹重命名为20181001.xls
  • data.xls从20181008文件夹重命名为20181008.xls

修订的代码仍在读取$zipFolderChild作为完整路径,如何仅提取文件夹名称而不是?

edit3:

试图将重命名的陈述进入循环,但不确定如何使-NewName参数起作用,$zipFolderChild.Name.xls Clear不起作用。也尝试了:

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

在循环内,也无法工作。

最后工作:

$7ZipPath = "C:7z7za"
$zipFolderRoot = "Z:long folder pathTest FolderUnzip Test"
$zipOutPath = "Z:long folder pathTest FolderUnzip TestUnzip"
$zipFilePassword = "TEST123"
ls -path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = "$zipFolderChildData.zip"
$cmd = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword ""$zipFile"""
iex $cmd
$folder= $zipFolderChild.Name
$xlsFile = "$zipOutPathData.xls"
$NewName = "$zipOutPath$folder.xls"
&  Rename-Item -Path "$zipOutPathData.xls" -NewName $NewName
}

这对我有用:

$7ZipPath = "C:7z7za"
$zipFolderRoot = "Z:long folder pathTest FolderUnzip Test"
$zipOutPath = "Z:long folder pathTest FolderUnzip TestUnzip"
$zipFilePassword = "TEST123"
Get-ChildItem -Path $zipFolderRoot -Exclude Unzip -Directory
Foreach ($zipFolderChild in (Get-ChildItem -Path $zipFolderRoot -Exclude Unzip))
{
    $zipFile = "$zipFolderChildData.zip" 
    $command = "x", "$zipFile", "-p$zipFilePassword", "-y", "-o$zipOutPath"
    & $7ZipPath @command
    #file rename command that I have not written yet
}

我将剥离用于$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++
}

它可以更好地工作吗?

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

$ zipfile不会以单语引号获得变量:

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

相关内容

  • 没有找到相关文章

最新更新