Applying intelligence to 7zip in powershell

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Gavin Williams Gavin Williams 1 year, 11 months ago.

  • Author
    Posts
  • #28817
    Profile photo of Gavin Williams
    Gavin Williams
    Participant

    Hi All,
    I have a module where I can take in zip files and then I unzip them to a specific directory. However, sometimes the zip files themselves have double-nested folders. e.g. a zip file
    "book1.zip".. will contain a folder "book1" and then book1.txt. I want to keep the first level such that book1.zip will create
    destfolder\book1\book1.txt...
    but not
    destfolder\book1\book1details\book1txt

    However there aren't always 2 levels... so I want to examine the zip file, check to count how many levels there are, and either extract, or else go *into* the zip file and then extract the subfolder.

    Any ideas ? Many thanks

    		foreach ($i in $files)
    			{
    			write-output "$("Setting Location to Directory : ")$($i.DirectoryName)"
                set-location $i.Directoryname
    			Write-Output "$("Expanding ")$($i.name)$(" to ")$($workdir)$("\")$($ComicsDir)$("\")$($i.basename)"
    			$extractor = New-Object sevenzip.SevenZipExtractor($i.fullname)                                                                                                                                                                                                    
       			$extractor.ExtractArchive("$($ComicsDir)$("\")$($i.basename)")   
    			Remove-Item $i.name
    			#$testempty = dir 
    			#if (!$testempty)
    			#{
    			#	cd ..
    			#	Remove-Item $i.directoryname
    			#	$testempty = $null
    			#}
    			
    		}
    
  • #28843
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    May be easier to extract the zipped data to a temporary location and then examine

  • #28877
    Profile photo of Gavin Williams
    Gavin Williams
    Participant

    Interesting idea.. yes that could take me towards the answer.

    Now same question would apply... when I expand them.. how can I do a run across to say... "if there is one subdirectory in the temp folder then move it to the final folder... if there are 2 or more lower subdirectories, then move to the bottom one and move *that* to the final folder"?

You must be logged in to reply to this topic.