将来自选定目录的文件添加到7个zip存档,同时将相对目录结构保存在存档中
我正在尝试将具有子目录的目录中的文件进行zip文件,但我不知道如何将文件而不是子目录。
这是当前的设置:
C:\users\user\appdata\local\folder\
在此文件夹中,我需要其中的20个左右的文件夹中的3个,因此我使用Get-Childitem来完成此操作:
GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*}
现在我有了,我不想要子目录和只需要放在文件夹本身中的文件即可。我还没有找到这样做的方法,但是我已经接近使用此操作:
& "C:\program files\7-zip\7z.exe" "a" "D:\TestBackup\Zipfile.7z" (GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*} | select -expandproperty FullName)
但是这为我提供了该文件夹的全部内容。我想保留结构,以便看起来像这样:
folder 1\files
folder 2\files
folder 3\files
我希望我能很好地解释自己。这些文件都是不同类型的扩展名,因此我想要一种笼统的方法来执行此操作或在拉链时排除子目录。
I am trying to zip files that are in directories that have subdirectories and I can't figure out how to zip the files and not the subdirectories.
Here is the current setup:
C:\users\user\appdata\local\folder\
Inside of this folder, I need 3 out of the 20 or so folders that are in there so I used the Get-Childitem to accomplish this:
GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*}
Now that I have that, I don't want the subdirectories and just want the files that are sitting in the folder itself. I have not found a way to do this, but I have gotten close with using this:
& "C:\program files\7-zip\7z.exe" "a" "D:\TestBackup\Zipfile.7z" (GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*} | select -expandproperty FullName)
But this gives me the entire contents of the folder. I want to keep the structure so that it looks like this:
folder 1\files
folder 2\files
folder 3\files
I hope I am explaining myself well. The files are all different types of extensions so I was wanting a blanket way to do this or to exclude the subdirectories when zipping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须咨询 faq 才能正确地:
解决方案:
备注:
按钮
设置当前目录,而pop-location
还原以前的当前当前目录。正如7-ZIP常见问题解释所述,更改当前目录对于该解决方案至关重要。这也是设置resolve -path -Relative
的基本目录的唯一方法。-directory
或-file
Get-Childitem
如果您仅对目录或文件感兴趣。-filter
而不是where-object
(Alias?
)。-filter
更快,因为它使用了在较低API级别过滤的文件系统提供商,从而避免了PowerShell开销。I had to consult the FAQ to get this right:
Solution:
Remarks:
Push-Location
sets the current directory, whilePop-Location
restores the previous current directory. Changing the current directory is crucial for this solution, as explained by the 7-zip FAQ. It is also the only way to set the base directory forResolve-Path -Relative
.-Directory
or-File
toGet-ChildItem
if you are only interested in either directories or files.-Filter
instead ofWhere-Object
(alias?
) if you only need simple wildcard filtering.-Filter
is faster because it uses the FileSystem provider which filters at a lower API level, which avoids PowerShell overhead.