使用递归搜索所有目录将文件提取到与存档相同的文件夹
我正在 PowerShell 中执行一项自动化任务,该任务使用递归和 7z.exe 实用程序将多个 .tar 存档的内容提取到各自的子文件夹中。
我遇到了一个问题,输出转储在我的工作目录中,而不是子目录中 gci -r 找到了原始的 tarball。
到目前为止,我有:
$files=gci -r | where {$_.Extension -match "tar"}
foreach ($files in $files) {
c:\7z.exe e -y $file.FullName }
关于在循环内设置工作目录的建议或 7z 提示值得赞赏。
I'm working on an automation task in PowerShell that extracts the contents of several .tar archives to their respective subfolders using recursion and the 7z.exe utility.
Im running into an issue where the output dumps in my working directory instead of the subdirectory gci -r found the original tarball.
So far I have:
$files=gci -r | where {$_.Extension -match "tar"}
foreach ($files in $files) {
c:\7z.exe e -y $file.FullName }
Advice on setting the working directory within the loop or 7z tips are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当谈到powershell时,我完全一无所知,但是在下载了一个充满子目录(本质上是“publisher.title.author.year”)的巨大种子后,每个子目录都包含一个或多个zip文件,解压后每个文件都包含一个多文件的一部分-file rar arhive,最后一旦组装好,其中包含一个(无用的命名)pdf文件...
所以我想出了这个粗略且准备好的脚本来递归到每个子目录,将zip解压缩到该子目录中目录,然后组装 rar 文件并将 pdf 解压到该目录中,然后用目录名称重命名 pdf,并将其向上移动到一个目录(所以最后我得到了充满有意义命名的 pdf 文件的基本目录..无论如何
- 就像我说的,我实际上没有 powershell 知识,所以我发布这个主要是因为我浪费了两个小时来写它(我可以在 python 或 perl 中在 5-10 分钟内完成:P)并且因为如果它在净我如果我需要的话可能实际上会再次找到它呵呵
I am totally totally clueless when it comes to powershell, but after downloading an enormous torrent full of subdirectories ("publisher.title.author.year" essentially) each containing one or more zip files, which when unzipped each contained a part of a multi-file rar arhive, and FINALLY once assembled that contained a (uselessly named) pdf file...
so I came up with this rough and ready script to recurse into each subdirectory, unzip the zips into that directory, then assemble the rar files and extract the pdf into that directory, then rename the pdf with the directory name, and move it up a directory (so by the end I ended up with the base directory full of meaningfully named pdf files...
anyway - like I said I have literally no powershell knowledge so I'm mainly posting this because I wasted two hours writing it (I could have done it in 5-10 minutes in python or perl :P) and because if it's on the net I might actually find it again if I need to hehe
几点:
1) 使用
x
标志而不是e
来保留路径。2) 使用
-o
指定目标/输出。我将目标文件夹作为 tar 文件的名称(不带扩展名),并将路径与 tar 文件相同。您可以删除该文件夹并只保留路径。3)您只是使用
gci -r
- 它会在当前目录中查找文件。我在下面包含了$scriptDir
,它将位于脚本路径的目录下。要搜索整个计算机,请执行类似gci c:\ -r
的操作,我将这样做:
Few points:
1) Use
x
flag instead ofe
to preserve paths.2) Use
-o
to specify destination / output. I am taking the destination folder to be the name of the tar file ( without extension ) and the path to be the same as the tar file. You can remove the folder and just have the path.3) You are just using
gci -r
- it will look for the files in the current directory. I have included$scriptDir
below, which will under the directories the script's path. To search entire machine, do something likegci c:\ -r
This is how I will do it:
尝试一下。性能提示,使用 Filter 参数而不是 where-object。
Give this a try. Performance tip, use the Filter parameter instead of where-object.
我是OP(做了一个新的帐户)。感谢manojlds,我更改
为
以便输出到与存档相同的目录并维护树结构。
我还进行了调整
,
因为它抛出了一个错误(7z 问题?)。
非常感谢。
I'm the OP (made a new acct). Thanks manojlds, I changed
to
in order to output to the same directory as the archive and maintain the tree structure.
I also tweaked
to
because it was throwing an error (7z issue?).
Thanks much.