【问题标题】:Powershell + 7zip to batch extract and rename - getting "No files to process" messagePowershell + 7zip 批量提取和重命名 - 收到“没有要处理的文件”消息
【发布时间】:2019-10-03 22:18:54
【问题描述】:

已编辑:

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

谢谢@nemze,他/她的回答启发了我改变我的代码:

$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
}

收件人:

$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
}

通过将$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", "$zipOutPath\Data.xls" ,"-NewName", "$folder.xls"
& Rename-Item @rename

在循环内,也不工作。

终于开工了:

$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
}

【问题讨论】:

    标签: powershell 7zip powershell-v6.0


    【解决方案1】:

    这对我有用:

    $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
    }
    

    我将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++
    }
    

    【讨论】:

    • 谢谢!这对我有用。我的第二个问题来了。如何获得“ChildFolder”名称?在代码中,$zipFolderChild 似乎是完整路径。但我想要: Data.xls 从 20181001 重命名为 20181001.xls Data.xls 从 20181008 重命名为 20181008.xls 抱歉问你们这些新手问题,这是我第一次编写 Powershell 脚本,我搜索的文档太庞大了.
    • 最好的方法是将Get-ChildItem赋值给变量,然后引用它的值,例如:$folder = Get-ChildItem -Path $zipFolderRoot -Exclude Unzip -Directory。通过使用$folder.name,您将获得文件夹名称数组。大家可以一一参考:$folder[0].name
    • 好的,但是[0] 等将是一个变量,这意味着我需要在循环中获取它并使用一个计数器,例如i=0i=i+1 等来完成这项工作。我说的对吗?
    • 您可以在Foreach 循环中使用$zipFolderChild.name 来获取当前文件夹名称。试试看会发生什么。
    • 抱歉,实际上,即使在使用您的代码时,循环也会为文件夹 20181008 循环两次,但从未从 20181001 中提取任何内容
    【解决方案2】:

    这样效果更好吗?

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

    $zipFile 不会得到带单引号的变量:

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

    【讨论】:

    • 遗憾的是没有。删除命令行中的单引号会产生错误。因为完整路径包含空格。
    • 路径中的空格对我有用。我是说根本不要使用iex。只需运行 &。
    • 也不工作,向我抛出大量红色错误
    • 从 $zipFile = 行中去掉单引号。
    • 遗憾的是,仍然无法正常工作。当我直接运行时,它不喜欢 $7ZipPath,带或不带引号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2013-10-31
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多