提问人:Ashar 提问时间:10/12/2023 最后编辑:Ashar 更新时间:10/13/2023 访问量:118
递归搜索当前目录中的几个文件夹,并将找到的文件夹添加到 zip 文件中
search recursively for couple of folders in present directory and add the found folders to a zip file
问:
对于Windows中的给定文件夹,假设我希望递归搜索这两个文件夹,并且必须在文件夹下。$currentDir = Get-Location
json
txt
db_execute
下面是我的powershell,它不能按预期工作。
# Define the names of the folders to search for
$folderNames = "db_execute\json", "db_execute\txt"
$zipFileName = "Folders.zip"
$currentDir = Get-Location
# Create an empty zip file
Compress-Archive -LiteralPath $currentDir -DestinationPath $zipFileName -CompressionLevel Optimal -Update
# Loop through each folder name
foreach ($folderName in $folderNames) {
# Find all the subdirectories with the given name in the current directory recursively
$subDirs = Get-ChildItem -Path $currentDir -Directory -Recurse -Filter $folderName
# Loop through each subdirectory
foreach ($subDir in $subDirs) {
# Add the subdirectory to the zip file
Compress-Archive -LiteralPath $subDir.FullName -DestinationPath $zipFileName -CompressionLevel Optimal -Update
}
}
这创建了一个具有多个文件和文件夹,但我需要它仅包含这两个文件夹及其内容。Folder.zip
db_execute_scripts\json,db_execute_scripts\txt
$currentDir
| -- source
|--- repo
| db_execute
|--- txt
|--- json
请注意,可以位于$currentDir内的任何位置。以上只是一个示例,此结构可能会发生变化。db_execute
期望
Folders.zip
|--- db_execute
|--- txt
|--- json
你能建议我在 PowerShell 中做错了什么吗?
答:
-1赞
The Freelancer
10/12/2023
#1
@Ashar,您可以使用此脚本来实现您的要求
# Define the names of the folders to search for
$folderNames = @("json", "txt")
$zipFileName = "Folders.zip"
$currentDir = Get-Location
# Loop through each folder name
foreach ($folderName in $folderNames) {
# Find the folder with the given name in the current directory recursively
$folder = Get-ChildItem -Path $currentDir -Directory -Recurse -Filter $folderName
# Check if the folder exists
if ($folder) {
# Add the folder and its contents to the zip file
Compress-Archive -LiteralPath $folder.FullName -DestinationPath $zipFileName -CompressionLevel Optimal -Update
}
}
评论
0赞
The Freelancer
10/12/2023
原始脚本的问题在于,它使用 Compress-Archive -LiteralPath $currentDir -DestinationPath $zipFileName创建整个目录的存档,然后尝试添加符合条件的各个子目录。此方法无法按预期工作,因为它首先创建整个目录的 zip 文件(包括其所有内容),然后将各个文件夹添加到该 zip 中。它无法实现创建仅包含“db_execute_scripts\json”和“db_execute_scripts\txt”文件夹及其内容的 zip 文件的目标。
0赞
Doug Maurer
10/12/2023
您确定可以同时过滤两个文件夹名称/级别吗?我不相信这是按原样工作的。
2赞
Olaf
10/12/2023
@TheFreelancer 请不要添加其他信息作为评论。相反,编辑您的答案并将其添加到那里。提前致谢。👍🏼🤟🏼
0赞
Ashar
10/12/2023
@TheFreelancer答案不起作用.错误Get-ChildItem : Could not find a part of the path 'C:\Users\HOME\source\db_execute_scripts'. At line:11 char:15 + ... $folder = Get-ChildItem -Path $currentDir -Directory -Recurse -Filt ... + + CategoryInfo : ReadError: (C:\Users\HOME\source:String) [Get-ChildItem], DirectoryNotFoundException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem : Could not find a part of the path 'C:\Users\HOME\source\db_execute_scripts'. At line:11 + ... $folder = Get
0赞
The Freelancer
10/12/2023
@Ashar您可以使用更新的脚本。它在我的环境中运行良好。
-1赞
SimeonOnSecurity
10/12/2023
#2
@Ashar,请尝试这个。
# Define the subfolder name for the zip
$subfolderName = "db_execute"
# Define the selection folder
$selectionFolder = "db_execute"
# Define the output zip file name
$zipFileName = "folders.zip"
# Get the current directory
$currentPath = Get-Location
# Get all subdirectories recursively
$subDirectories = Get-ChildItem -Directory -Recurse -Path $currentPath
# Filter subdirectories based on specific criteria, e.g., you can check for "json" and "txt" subdirectories
$jsonFolders = $subDirectories | Where-Object { $_.FullName -like "*\$selectionFolder\*" } | Where-Object { $_.Name -eq "json" }
$txtFolders = $subDirectories | Where-Object { $_.FullName -like "*\$selectionFolder\*" } | Where-Object { $_.Name -eq "txt" }
# Check if both "json" and "txt" subdirectories exist
if ($jsonFolders.Count -gt 0 -and $txtFolders.Count -gt 0) {
# Create a temporary folder
$tempDir = New-Item -Path (Join-Path -Path $currentPath -ChildPath $subfolderName) -ItemType Directory -Force
# Copy the content of the "json" and "txt" subdirectories to the temporary folder
Copy-Item -Path $jsonFolders.FullName -Destination (Join-Path -Path $tempDir.FullName -ChildPath "json") -Recurse
Copy-Item -Path $txtFolders.FullName -Destination (Join-Path -Path $tempDir.FullName -ChildPath "txt") -Recurse
# Compress the temporary folder into a zip file
Compress-Archive -Path $tempDir.FullName -DestinationPath (Join-Path -Path $currentPath -ChildPath $zipFileName) -Force
# Clean up the temporary folder
Remove-Item -Path $tempDir.FullName -Recurse -Force
Write-Host "Folders have been compressed into $zipFileName with subfolder '$subfolderName'."
} else {
Write-Host "One or both of the 'json' and 'txt' subdirectories do not exist."
}
评论
0赞
Ashar
10/12/2023
在此处获取第 8 行的错误 ->Path
Compress-Archive : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:8 char:24
0赞
Ashar
10/12/2023
我改成了.脚本运行正常,但未创建 zip 文件 @ 。我曾经硬编码过绝对路径,但这也没有帮助。Compress-Archive -Path $null -DestinationPath $zipFileName -Force
Compress-Archive -Path $currentDir -DestinationPath $zipFileName -Force
$currentDir
-Path
0赞
SimeonOnSecurity
10/12/2023
@Ashar抱歉,我在创建空 zip 文件时出错。现在就试试吧。
0赞
Ashar
10/13/2023
它仍然不起作用。未创建 zip 文件。另外,我的要求不是找到任何文件夹,而只找到文件夹下的文件夹json
txt
db_execute
0赞
SimeonOnSecurity
10/13/2023
@Ashar我已经测试了您的用例并对其进行了更新。这不是世界上最有效的东西,但它有效。
-1赞
lit
10/12/2023
#3
问题在于 $subDirs 数组的创建。此代码查找以 $folderNames 数组中的值结尾的目录路径。
似乎不需要创建空的 .zip 文件。
$folderNames = "db_execute\json", "db_execute\txt"
$zipFileName = "Folders.zip"
$currentDir = Get-Location
# Get a list of directories that end with one of the $folderNames
$subDirs = Get-ChildItem -Recurse -Directory -Path $currentDir |
ForEach-Object {
foreach ($folderName in $folderNames) {
if ($_.FullName.EndsWith($folderName)) { $_.FullName }
}
}
foreach ($subDir in $subDirs) {
# Add the subdirectory to the zip file
Compress-Archive -LiteralPath $subDir -DestinationPath $zipFileName -CompressionLevel Optimal -Update
}
评论
0赞
Ashar
10/13/2023
未创建 zip
0赞
lit
10/13/2023
您要在哪个目录中查找文件?请描述具体情况。您是否使用调试器逐步完成了此操作?Folders.zip
评论
Get-ChildItem
-Filter
Get-ChildItem -Path . -Directory -Recurse -Filter 'db_execute\txt'
PS source> Get-ChildItem -Path . -Directory -Recurse -Filter 'db_execute\txt' Get-ChildItem : Could not find a part of the path 'C:\Users\HOME\source\db_execute'. At line:1 char:1 + Get-ChildItem -Path . -Directory -Recurse -Filter 'db_execute\txt' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (C:\Users\HOME\source:String) [Get-ChildItem], DirectoryNotFoundException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand