将来自选定目录的文件添加到7个zip存档,同时将相对目录结构保存在存档中

发布于 2025-02-07 22:06:20 字数 722 浏览 2 评论 0原文

我正在尝试将具有子目录的目录中的文件进行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2025-02-14 22:06:20

我必须咨询 faq 才能正确地:

7-zip仅存储文件的相对路径(没有驱动器字母
前缀)。您可以将电流文件夹更改为常见的文件夹
所有要压缩的文件,然后可以使用相对
路径:

  cd /dc:\ dir1 \   
7z.exe AC:\ a.7z file1.txt dir2 \ file2.txt
 

解决方案:

# Set base directory that is common for all files
Push-Location 'C:\users\user\appdata\local\folder'

# Get names of directories that match filter
$folderNames = (Get-ChildItem -Directory -Filter '*folder*').Name

# Call 7-zip, passing the list of directory names.
& 'C:\Program Files\7-Zip\7z.exe' a 'D:\TestBackup\Zipfile.7z' $folderNames

# Restore current directory
Pop-Location

备注:

  • 按钮设置当前目录,而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:

7-Zip stores only relative paths of files (without drive letter
prefix). You can change current folder to folder that is common for
all files that you want to compress and then you can use relative
paths:

cd /D C:\dir1\   
7z.exe a c:\a.7z file1.txt dir2\file2.txt

Solution:

# Set base directory that is common for all files
Push-Location 'C:\users\user\appdata\local\folder'

# Get names of directories that match filter
$folderNames = (Get-ChildItem -Directory -Filter '*folder*').Name

# Call 7-zip, passing the list of directory names.
& 'C:\Program Files\7-Zip\7z.exe' a 'D:\TestBackup\Zipfile.7z' $folderNames

# Restore current directory
Pop-Location

Remarks:

  • Push-Location sets the current directory, while Pop-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 for Resolve-Path -Relative.
  • Pass -Directory or -File to Get-ChildItem if you are only interested in either directories or files.
  • Use -Filter instead of Where-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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文