2011-09-01 34 views
0

我正在使用PowerShell中的一个自动化任务,它使用递归和7z.exe实用工具将几个.tar归档的内容提取到其各自的子文件夹。使用递归搜索所有目录将文件解压缩到同一文件夹

我遇到了输出转储到我的工作目录而不是子目录gci -r找到原始tarball的问题。

到目前为止,我有:在设置循环内的工作目录,或7z格式的技巧赞赏

$files=gci -r | where {$_.Extension -match "tar"} 
     foreach ($files in $files) { 
      c:\7z.exe e -y $file.FullName } 

建议。

回答

1

几点:

1)使用的,而不是ex标志保存路径。

2)使用-o指定目标/输出。我将目标文件夹作为tar文件的名称(不带扩展名),路径与tar文件相同。您可以删除该文件夹,只需要路径。

3)您只是使用gci -r - 它将查找当前目录中的文件。我在下面包含了$scriptDir,这将在脚本路径下的目录下。要搜索整个机器,这样做gci c:\ -r

这是我会怎么做:

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path 
$z ="7z.exe" 

$files=gci $scriptDir -r | where {$_.Extension -match "tar"} 
foreach ($file in $files) { 
    $source = $file.FullName 
    $destination = Join-Path (Split-Path -parent $file.FullName) $file.BaseName 
    write-host -fore green $destination 
    $destination = "-o" + $destination 
    & ".\$z" x -y $source $destination 
} 
1

我是OP(做了一个新的ACCT)。谢谢manojlds,我改变

$destination = Join-Path (Split-Path -parent $file.FullName) $file.BaseName 

$destination = Join-Path (Split-Path -parent $file.FullName) $file.DirName 

,以输出到同一目录中存档和维护树状结构。 我还调整了

$z ="7z.exe" 
... 
& ".\$z" x -y $source $destination 

$z="c:\7z.exe" 
... 
& "$z" x -y $source $destination 

,因为它抛出一个错误(7Z问题?)。

非常感谢。

1

试试看。性能提示,请使用Filter参数而不是Where对象。

gci -r -filter *.tar | foreach { c:\7z.exe x -y $_.FullName $_.DirectoryName } 
+0

我需要使用-o标志和逃生b C的/在路径中有空格的目录名称:'GCI -r - filter * .tar | foreach {c:\ 7z.exe x -y $ _。FullName -o“$(​​'”'+ $ _。DirectoryName +'“'')”}' – lapropriu

3

我是当它涉及到PowerShell中完全完全一无所知,但下载一个巨大的洪流充满子目录(“publisher.title.author.year”基本上)各包含一个或多个zip文件,之后拉开时每个包含一个多文件rar arhive的一部分,最后一次汇编,其中包含一个(无用的名称)pdf文件...

所以我想出了这个粗略和准备好的脚本,以递归到每个子目录,解压缩拉到该目录中,然后组装rar文件并将pdf提取到该目录中,然后使用目录名称重命名pdf,然后将其移动到一个目录中(所以最后我以基础目录结束l有意义的命名pdf文件...

无论如何 - 就像我说的我几乎没有PowerShell的知识,所以我主要发布这个,因为我浪费了两个小时写它(我可以在5-10分钟内完成python或perl:P),因为如果它是在网络上我可能居然再次找到它,如果我需要嘿嘿

$subdirs = Get-ChildItem | ?{$_.Attributes -match 'Directory'} 
foreach ($dir in $subdirs) { 
    cd $dir 
    $zip get-childitem | where {$_.Extension -match "zip"} 
    C:\7z.exe x -y $zip.FullName 
    $rar = get-childitem | where { $_.Extension -match "rar"} 
    C:\7z.exe x -y $rar 
    $pwd = get-item . 
    $newname = $pwd.basename+".pdf" 
    get-childitem *.pdf | rename-item -newname $newname 
    move-item $newname ..\ 
    cd .. 
} 
相关问题