Modify script to include folder to zip

Hi,

I need help with modifying my script, because the guy, who created it for me does not currently respond.

# target folder
$directoriesList = @('C:\test\test\');
# folder mask
$directoryMask = 'U*'
# folder separator
$directorySeparator = '\'

Add-Type -AssemblyName "system.io.compression.filesystem"

try
{
foreach ($directory in $directoriesList){
$directories = Get-ChildItem -Directory -Path $directory | Where-Object -FilterScript {($_.Name -like $directoryMask)}
Echo (' ')
Echo ('Searching in ' + $directory + '')
foreach ($dir in $directories){
Echo ('zipping directory ' + $dir.Name + ' ...')
$compressedFileName = ($directory + $directorySeparator + $dir.Name + ".zip")
[System.IO.Compression.ZipFile]::CreateFromDirectory(($directory + $directorySeparator + $dir.Name), $compressedFileName)
Echo ('File ' + $compressedFileName + ' CREATED')
Echo ('Deleting directory ' + $directory + ' ...')
Remove-Item ($directory + $directorySeparator + $dir.Name) -Force -Recurse
Echo ('Directory ' + $directory + ' DELETED')
}
Echo ('Directory ' + $directory + '\ is DONE')
}
}
finally
{

}

This script takes all files in each sub-folder (U***) under the target folder and archives it in a zip file named according to the relevant sub-folder (U***.zip). This zip archive does not include the sub-folder, so when I open the zip archive I can see directly the list of files.

I need to modify it so that the zip achive contains also the sub-folder and then the files, ie U***.zip -> U***/files.

Thanks a lot for your advice.

Petr

 

 

If you are in PowerShell version 5.0 or later, you can use Compress-Archive cmdlet.

Compress-Archive -Path c:\Sourefolder -DestinationPath c:\NewZip.zip

[quote quote=123038]If you are in PowerShell version 5.0 or later, you can use Compress-Archive cmdlet.

<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59771px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
Compress-Archive -Path c:\Sourefolder -DestinationPath c:\NewZip.zip
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

If I change Get-ChildItem to Compress-Archive and run the script, it prompts me to specify a destination path.

The destination path should be the folder where are all U*** folders located. There will be four destination folders eg:

# target folder
$directoriesList = @('C:\test\test\'
                     'C:\test\test1\'
                     'C:\test\test2\'
                     'C:\test\test3\');

The current script does not ask about a destination folder, it does all the archiving automatically.

I suggest you to read the help documentation of Compress-Archive cmdlet , will give you a better picture.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-6

[quote quote=123812]I suggest you to read the help documentation of Compress-Archive cmdlet , will give you a better picture.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-6

[/quote]

Thank you for taking the time kvprasoon, but I do not learn to script in PowerShell, I’m not a programmer, I search for someone who could tell me exactly what to change in my script to get the requested result.

Well,

Myself is also not a programmer, Many of the PowerShell community members are also not programmers, majority of the learnings we had are from the requirements we had at work.

If you feel PowerShell is gonna make your work better and fast, you should definitely take time to learn it. If you work with Windows systems, PowerShell is knowledge is a must. Once you are around, you will automatically learn the scripting part of it.

Don Jones’s “Learn PowerShell in a month of Lunches” is a great book to start with and Is highly recommended.

You can do something like this after some testing. Change the -whatif to -verbose.

ls -dir $directory/u* | 
foreach { 
  compress-archive $_ "$_.zip" -whatif
  if ($?) { 
    rm -r $_ -whatif
  }
}  

Sigh. Why doesn’t this work?

ls -dir $directory u*

But this does.

ls -dir -path $directory -filter u*

If you want to treat -path and -filter as postional parameters, they have to be in the right positions.

ls -dir $directory u* won’t work, I agree, but ls $directory u* -dir would, because it puts the positional parameters in the right places.

Thanks all of you for your effort, but I already have the desired solution. It was just about adding new parameters [pre]0, $true[/pre] on the line 19.

You’re right. Somehow I didn’t think the -parameters mattered. Maybe another cmdlet was more forgiving.