Modify script to include folder to zip

Welcome Forums General PowerShell Q&A Modify script to include folder to zip

This topic contains 9 replies, has 4 voices, and was last updated by

js
 
Participant
1 month ago.

  • Author
    Posts
  • #123018

    Participant
    Points: 20
    Rank: Member

    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

     

     

  • #123038

    Participant
    Points: 889
    Helping Hand
    Rank: Major Contributor

    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
    
  • #123228

    Participant
    Points: 20
    Rank: Member

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

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

    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.

  • #123812

    Participant
    Points: 889
    Helping Hand
    Rank: Major Contributor

    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

  • #123939

    Participant
    Points: 20
    Rank: Member

    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

    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.

    • #123941

      Participant
      Points: 889
      Helping Hand
      Rank: Major Contributor

      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.

  • #123956
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    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*
    
  • #124544

    Participant
    Points: 32
    Rank: Member

    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.

    • #124674
      js

      Participant
      Points: 326
      Helping Hand
      Rank: Contributor

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

  • #124607

    Participant
    Points: 20
    Rank: Member

    Thanks all of you for your effort, but I already have the desired solution. It was just about adding new parameters 

    0, $true

    on the line 19.

You must be logged in to reply to this topic.