0

I have a short question, but I am standing on the wall for too long now, so I have to ask you.... The situation is: I have a special filetype, in different folders and subfolders. I already managed to find the files, write them into a TXT-File and I also managed to split the path so I can name a ZIP-File with the Folder-Name and Date. But the only thing I do not get is how to only zip the special file of folder1 in a Zip-archiv "folder1-date.zip" and the file of folder2 in a Zip-archiv "folder2-date.zip". Code part looks like this:

[string[]]$dirs = (Split-Path (Split-Path -Path $output -Parent) -Leaf | Foreach-Object { $i++; $_ })
[string[]]$arrayFromFile = Get-content -Path 'C:\TEMP\output.txt'
foreach ($file in $arrayFromFile) {
foreach ($dir in $dirs){
#
Compress-Archive -Path $file -CompressionLevel Optimal -Update -DestinationPath $destination\$dir-$date.zip }
}

The Problem is, that every file with the extension found is in every ZIP-Archiv (logic because it is a foreach in a foreach) but I can not find the right way to do it.... Thank you for the help!

Shawn
  • 47,241
  • 3
  • 26
  • 60
linedan
  • 3
  • 1
  • 1
    It's a bit hard to understand what you're trying to achieve, it would be easier if you add what you currently have as file/folder structure and what you want as a result – Santiago Squarzon Jan 25 '22 at 14:33
  • 1
    Where are `$output`, `$date` and `$destination` defined? Does `C:\TEMP\output.txt` contain file _paths_, or just file _names_? – Mathias R. Jessen Jan 25 '22 at 14:48
  • 1
    As above, edit your quesiton to include current structure, the content of `output.txt` and expected result. It's highly likely there's also a much more efficient way to to what you're trying than retrieving all the paths and then using nested `ForEach` loops – Scepticalist Jan 25 '22 at 14:48

2 Answers2

0

This will get the desired outcome and not have to save output to a text file.

$origin = "C:\TEMP\"
$filetyp = ".stl, .vol, .pct, .tif"
$destination = "C:\Daten\zipstore\"
$date = $(Get-Date -Format d)


$fileNames = Get-ChildItem "$origin" -Recurse | Where {$_.extension -eq ".stl"} | ForEach-Object { $_.FullName }

foreach ($file in $fileNames) {

    $dir = (Split-Path (Split-Path -Path $file -Parent) -Leaf)
    
    Compress-Archive -Path $file -CompressionLevel Optimal -Update -DestinationPath $destination\$dir-$date.zip
}
jeff_C
  • 16
  • 3
  • 1
    Good Morning!Thank you, works perfectly! As always, simple solution but it is like you do not see the forest with all this tress hahaha – linedan Jan 26 '22 at 07:58
0

Thank you for the answer! Here you have the whole code:

# Variablen Definition: 
# 
$origin = "C:\TEMP\"
$filetyp = ".stl, .vol, .pct, .tif"
$destination = "C:\Daten\zipstore\"
$date = $(Get-Date -Format d)
#
#
# Auslesen aller Files die bestimmte Dateiendung haben: 
#
Get-ChildItem "$origin" -Recurse | Where {$_.extension -eq ".stl"} | ForEach-Object { $_.FullName } > C:\TEMP\output.txt
#
#
# Remove filename, keep path to file / split and only keep last directory:
#
$output = Get-content C:\TEMP\output.txt
$i = 0
#
[string[]]$dirs = (Split-Path (Split-Path -Path $output -Parent) -Leaf | Foreach-Object { $i++; $_ })
#
#
# Create ZIP-Archiv:
#

[string[]]$arrayFromFile = Get-content -Path 'C:\TEMP\output.txt'
foreach ($file in $arrayFromFile) {
foreach ($dir in $dirs){
#
Compress-Archive -Path $file -CompressionLevel Optimal -Update -DestinationPath $destination\$dir-$date.zip }
}
#
# 
# Delete files not needed anymore:
Remove-Item -Path $origin -Include *.txt -Recurse -Force
#

Maybe this helps!

linedan
  • 3
  • 1