PS Script to zip same name Files in a directory e.g. PDFs & word document

Welcome Forums General PowerShell Q&A PS Script to zip same name Files in a directory e.g. PDFs & word document

Viewing 8 reply threads
  • Author
    Posts
    • #174937
      Participant
      Topics: 1
      Replies: 3
      Points: 24
      Rank: Member

      Hi All,

      Hope you all intelligent people are well. i am new to this still learning so you can understand i am no where know PS scripts like you guys. so i thought to request you guys if you could help me. i am looking to write a script where it will zip the same name files into one zip folder for example....

      abc_1.pdf, abc_2.pdf, abc_3.pdf, ABC_ltr.doc

      def_1.pdf, def_2.pdf, def_3.pdf, def_ltr.doc...and so on

      so basically i want to zip all the "abc" file name files into one zip and all the "def" files name into another zip. this is simple i guess for you guys as there is not other criteria of time or date or anything. simply create a zip folder with the same name files and also name the zip folder with the same name.

      hope you would be able to help.

      Many thanks in advance.

      regards

    • #175030
      Participant
      Topics: 1
      Replies: 1635
      Points: 3,089
      Helping Hand
      Rank: Community Hero

      so i thought to request you guys if you could help me. i am looking to write a script where it will zip the same name files into one zip folder for example....

      We can surely help but we will not do your job. So you will have to do the actual work. 😉

      We are used to get some code to debug. So please show your code and the complete error messages if you get some. (Please format your code as code as described in the very first post on top of the list of posts in this forum: Read Me Before Posting! You'll be Glad You Did! ... and please format the error messages as code as well if you post some)

      To get you started you should read the help for Get-ChildItem, Foreach-Object and Compress-Archive.
      You should read each of these help topics completely including the examples to learn how to use them.

      • #175744
        Participant
        Topics: 1
        Replies: 3
        Points: 24
        Rank: Member

        $path = "C:\backup\DEV82"
        if(!(Test-Path -Path $path )){
        New-Item -ItemType directory -Path $path
        }

        cd C:\inetpub\wwwroot\dev82\
        $SOURCE = Get-ChildItem * -Directory|Where-Object {$_.FullName -match "App_Config|Resources|bin"}

        $dtstamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
        $DESTINATION = Join-path -path $path -ChildPath "$dtstamp.zip"
        Add-Type -assembly "system.io.compression.filesystem"
        If(Test-path $DESTINATION) {
        Remove-item $DESTINATION
        }

        Foreach ($s in $SOURCE)
        {
        Compress-Archive -Path $s.fullname -DestinationPath $DESTINATION -Update
        }

    • #175750
      Participant
      Topics: 1
      Replies: 1635
      Points: 3,089
      Helping Hand
      Rank: Community Hero

      Could you please format your code as code as requested?
      Thanks

    • #175753
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      James,

      Thank you for posting the code, what is the issue you are facing at this time? Is it working as expected or are you receiving errors? What is the issue you face at this time with your script? I would also not change the directory to the location unless totally necessary, you are already pulling the full path. I would look at adding some error handling as well, before placing this in production. How will your script handle if there are no directories/files that match your where-object filter?

      Before:

      cd C:\inetpub\wwwroot\dev82\
      $SOURCE = Get-ChildItem * -Directory|Where-Object {$_.FullName -match "App_Config|Resources|bin"}
      After:
      $SOURCE = Get-ChildItem -Path C:\inetpub\wwwroot\dev82\ -Directory | Where-Object {$_.FullName -match "App_Config|Resources|bin"}
    • #175762
      Participant
      Topics: 6
      Replies: 18
      Points: 70
      Rank: Member

      You could copy the same files into temp folders and zip those? Or alternatively, use 7 zip with arguments (.e.g Start-Process )
      https://sevenzip.osdn.jp/chm/cmdline/syntax.htm

    • #175882
      Participant
      Topics: 6
      Replies: 108
      Points: 302
      Helping Hand
      Rank: Contributor

      And I don't see anything in your code where you're filtering on filename. Have your requirements changed?

      You can probably do what you initially posted with something like this for each of the filename filters you've got:

      (Get-ChildItem -Path *.ps1).Fullname | Compress-Archive -DestinationPath C:\newDesination\newZip.zip
      

      You'll have to include the setup steps and filename variables, etc. but that's pretty quick and easy.

    • #176119
      Participant
      Topics: 1
      Replies: 3
      Points: 24
      Rank: Member

      Hi,

      my apologies to all for the trouble. right i have manage to right a new one, which kind of works but not completely. it zips up the zip folder(for example) in a directory into on big zip, but it takes all the files in the directory. even though i am trying to specify to pick only the .zip folders. i have not done error handling yet. all i want to do is once this one is working, is try to zip folders into one big zip folder. if this works then i can try to figure out to zip similiar name file into one zip folder.

      `cls`

      `$source = "C:\AUGZip"`

      `$dest = "C:\\HOLA"`

      `Write-Verbose "Getting list of files" -Verbose`

      `$files = Get-ChildItem ($_.Name -like "*.zip") -Path $source`

      `while ($readyfile.EndOfStream -eq $false){`

      `switch ($readyfile.ReadLine()){`

      `$_ {$count++;$num=$_; If ($count -eq 1){$newdir = 1000 ; `
      `New-Item -ItemType Directory -Path "$source\$newdir" -ErrorAction SilentlyContinue}}`

      `$_ {If ($match = $files | where {$_.BaseName -eq $num}){$match |`

      `move-Item -Destination "$dest\$newdir" ;`

      `Write-Verbose "Moving file $num to $dest\$newdir !!!" -Verbose} Else`

      `{Write-Verbose "$num not found" -Verbose}}`

      `$_ {If ($count % 1000 -eq 0){$newdir = $count+1000 ;`

      `new-item -ItemType Directory -Path "$dest\$newdir" -ErrorAction SilentlyContinue}}`

      `}`

      `}`

      `add-type -AssemblyName 'System.IO.Compression.filesystem'`

      `Write-Verbose "Creating archive" -Verbose`

      `[System.IO.Compression.ZipFile]::CreateFromDirectory($source,"$dest\$(((get-date).ToLocalTime()).ToString("ddMMyyyy-hhmmss tt")).zip")`

    • #176167
      Participant
      Topics: 6
      Replies: 108
      Points: 302
      Helping Hand
      Rank: Contributor

      So, after formatting your code in the PowerShell ISE, it looks like your switch block isn't working like you think it should. Each of your conditions are the same ($_) so you are executing the code in all 3 of them every time. And I don't see anything that defines the $ eadyfile or $match variables, so we're still missing something...

      How are you executing this script? Is it saved as a .ps1 and then run? Or is this a transcript from you typing stuff into a console?

    • #176731
      Participant
      Topics: 1
      Replies: 3
      Points: 24
      Rank: Member

      for now i running this on ISE, just put wrote the script on the tab and running it manually to test

Viewing 8 reply threads
  • You must be logged in to reply to this topic.