【问题标题】:Powershell - Unzip all zipped files in a directory as well as rename them based on the parent file namesPowershell - 解压缩目录中的所有压缩文件,并根据父文件名重命名它们
【发布时间】:2022-01-06 15:36:42
【问题描述】:

我有一个文件夹和一组文件,如下所示:

  • 根目录 >
  • 父 1 >
  • 文件 1 > export.zip > export.txt
  • 文件 2 > export.zip > export.txt
  • 文件 3 > export.zip > export.txt
  • 父 2 >
  • 文件 1 > export.zip > export.txt
  • 文件 2 > export.zip > export.txt
  • 文件 3 > export.zip > export.txt

所以根文件夹包含数百个父文件夹,每个父文件夹包含数百个文件,每个文件包含一个标题为“export”的zipfile,每个zipfile包含一个.txt 文件,也称为“导出”

我需要得到

  1. 所有这些 .txt 文件都来自 zip 文件,
  2. 所有这些 .txt 文件都使用已知方法命名为独特的东西,并且
  3. 并移动到一个通用文件夹位置。

到目前为止,我已经完成了一些流程,但没有什么可以完成所有三件事。通过执行以下两行,我可以根据文件和父名称将 zipfile 重命名为独特的名称:

Get-ChildItem -LiteralPath 'C:\Insert Root Directory here' -Recurse | Rename-Item -NewName { $_.Parent.Name+'_'+$_.Name}

然后

Get-ChildItem -LiteralPath 'C:\Insert Root Directory here' -Filter *.zip -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}

然后我尝试了我在此处找到的东西来解压缩 zip 文件,重命名它们包含的 .txt 文件,并将它们同时移动到新位置,但它因“movefileinfoitemunauthorizedaccesserror microsoft.powershell.commands.moveitemcommand”错误而出错。我试过修补它,但我无法让它工作。如果在“#Move Our Temp Files”之后删除所有内容,错误就会消失,但结果仍然不成功。我在 $TempPath 位置得到一个名为 export 的 .txt 文件。 :

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

谁能帮我实现上述目标?接受任何和所有想法。谢谢!

【问题讨论】:

    标签: powershell copy rename unzip


    【解决方案1】:

    PS 5.1 或更高版本中的Expand-Archive 应该有助于简化您正在寻找的内容:

    $dst = "c:\destination"
    $tmp = "c:\temporary"
    
    # Get a list of all the zip files
    Foreach ($zipfile in (Get-ChildItem "C:\source\*.zip" -Recurse)) {
    
      # Extract the 'extract.txt' file to your temp folder
      Expand-Archive -Path $zipfile -DestinationPath $tmp -Force
    
      # Move the extract.txt to your destination folder
      # And rename based on the source folder and zip file name
      Move-Item -Path "$tmp\export.txt" -Destination "$dst\$($zipfile.Directory.BaseName)$($zipfile.BaseName).txt"
    
    }
    

    MoveItem 错误可能是由于此路径不正确:"$UnzipPath/$ZipFileActualName.txt"。您似乎没有在脚本中的任何位置设置$UnzipPath

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 2017-10-16
      • 2019-03-23
      • 2015-12-26
      • 1970-01-01
      • 2016-12-28
      • 2016-04-20
      • 1970-01-01
      • 2019-11-14
      相关资源
      最近更新 更多