PowerShell Scripting Zipping Sub directories.
i appreciate help--
this first job working computers , understand scripting slightly. language remotely have grasp on java. anyways. need write script in powershell of supervisers command.
so have network drive has thousands of subfolders. each subfolder has 200 ".tif"s image drives.
i need script compress each subfolder individually. example
-1
--a
---file.tiff
--b
---filex.tiff
into
-1
--a
---a.zip
--c
---c.zip
does make sense? each sub-folders content replaced zip file.
here code found online, edited , works does, not want do... edited , broken dont kow if helps
function select-folder($message='select folder', $path = 0) { $object = new-object -comobject shell.application $folder = $object.browseforfolder(0, $message, 0, $path) if ($folder -ne $null) { $folder.self.path } } function add-zip { param([string]$zipfilename) if(-not (test-path($zipfilename))) { set-content $zipfilename ("pk" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipfilename).isreadonly = $false } $shellapplication = new-object -com shell.application $zippackage = $shellapplication.namespace($zipfilename) foreach($file in $input) { $zippackage.copyhere($file.fullname) start-sleep -milliseconds 500 } } $folder = select-folder 'select folder' #could prompt user deployment location also, final compressed file $deploymentfolder = "$folder\..\deployment" $types = ".tif, .badfile" #psiscontainer - fetches files not directories #group-object - assists in foreach loop performs copy $files= get-childitem $folder | where-object {-not$_.psiscontainer} | group-object extension $files = $files | where-object {$types-contains$_.name} new-item -itemtype directory -path $deploymentfolder -ea silentlycontinue $files | foreach-object { $_.group | copy-item -destination $deploymentfolder } get-childitem $deploymentfolder -recurse | add-zip "$folder\deploytiff.zip" #moving file because add-zip cannot take adjusted path ($folder\..\file.zip) input. #move-item "$folder\deploy.zip" "$folder\..\deploy.zip" remove-item $folder\..\deployment -recurse
thank hard working. it's difficult when try learn @ beginning.
actually, problem not difficult solve, if understand usage of get-childitem , invoke-expression.
here approach solve problem. of course, need install 7zip @ first. , replace d:\share , d:\apps\7zip\7z.exe proper path on computer.
it’s better install 7zip folder no space in path, path give here.
#compress files based on folder structure get-childitem -path d:\share\ -recurse | where-object {$_.psiscontainer} | foreach-object { $directoryfullname = $_.fullname $directoryname = $_.name invoke-expression -command "d:\apps\7zip\7z.exe $directoryfullname\$directoryname.zip $directoryfullname\*" } #move zip files archive folder get-childitem -path "d:\share" -recurse -filter "*.zip" | move-item -destination d:\archive #please add code here remove .tiff files<br/>
also, use following code in order simulate environment.
for ($counter=1;$counter -le 7;$counter++) { $foldername = get-date $((get-date).adddays(-$counter)) -format "yyyymmdd" new-item -path "d:\share" -itemtype directory -name $foldername | out-null for ($i=1;$i -le 10;$i++) { $filename = $i.tostring("d2") $content = "f" * 10485760 new-item -path "d:\share\$foldername" -itemtype file -name "$filename.tiff" -value $content | out-null } }
if have suggestions, please feel free tell me.
greg gu
please remember click “mark answer” on post helps you, , click “unmark answer” if marked post not answer question. can beneficial other community members reading thread.
Windows Server > Windows PowerShell
Comments
Post a Comment