How do I select files in a folder based on part of filename and zip them in Powershell? -


i'm new powershell(using powershell 2.0 btw) , trying make script several things(this 3rd script or so). have things in place last thing remaining group files of different types (xml, tfw , tif) in folder, based on first part of filename(first 3 characters) , zip these files several zip-files name first 3 characters, either in same location or in new one.

sample of folder content:

001.tif 001.tfw 001.metadata.xml 002.tif 002.tfw 002.metadata.xml 003.tif 003.tfw 003.metadata.xml 003_svel.tif 003_svel.tfw 003_svel.metadata.xml 

wanted result:

001.zip containing 001.tif, 001.tfw, 001.metadata.xml 002.zip containing 002.tif, 002.tfw, 002.metadata.xml 003.zip containing 003.tif, 003.tfw, 003.metadata.xml, 003_svel.tif, 003_svel.tfw , 003_svel.metadata.xml 

i have installed 7-zip zipping , using commandline version. i've used 7-zip local on testfiles , got work, tif-files. have source folder search latest created folder , process files in it. have far(powershell 2.0):

$dir_source = "c:\test" $new_folder = get-childitem $dir_source -recurse | { $_.psiscontainer} |  sort-object lastwritetime -descending |  select-object -expandproperty    fullname <-first 1 get-childitem $new_folder -recurse -exclude metafile.xml |  group-object {$_.name.substring(0,3)}  

this gives me list of grouped files in lates created folder based on first 3 characters in filename. show files in each group. below:

count name group
----- ---- -----
3 003 {c:\test\20150708 063255_b\003.metafile.xml, c:\test\20150708 063255_b\003.tfw, c:\test\20150708 063255_b\003.tif}
6 004 {c:\test\20150708 063255_b\004.metafile.xml, c:\test\20150708 063255_b\004.tfw, c:\test\20150708 063255_b\004.tif,c:\test... 6 009 {c:\test\20150708 063255_b\009.metafile.xml, c:\test\20150708 063255_b\009.tfw, c:\test\20150708 063255_b\009.tif,c:\test...

now next step ist take these groups , zip them. ideally create these zip-files in different destination directory (i believe can change when setting $directory- variable in script below.)

foreach ($group in $dataset) {                  $name = $file.name                  $directory = $file.directoryname                 $zipfile = $file.name + ".zip"                  sz -t7z "$directory\$zipfile" "$directory\$name"  

this last code causing trouble. either message:

7-zip (a) 9.20 copyright (c) 1999-2010 igor pavlov 2010-11-18 error: c:\test\dest_test460.zip not supported archive system error: incorrect function.

,or

warning: cannot find 1 file 7-zip (a) 9.20 copyright (c) 1999-2010 igor pavlov 2010-11-18 scanning \460: warning: system cannot find file specified.

,or starts zipping files on userprofile zip-file. depending on changes $group-value. believe there 1 ore more basic errors in script causing this, , i'm asking help. may approaching wrong way first grouping files want , try zip them?

anyone can see error or give me hint have do?

thanks time!

share|improve question

lee holmes new-zipfile job, has 2 versions 1 of them using icsharpcode.sharpziplib.dll compress, , other not require it, wrapped 2nd 1 function:

function new-zipfile {  param( ## name of zip archive create $path = $(throw "specify zip file name"), ## switch delete zip archive if exists. [switch] $force ) set-strictmode -version 3 ## create zip file $zipname = $executioncontext.sessionstate.path.getunresolvedproviderpathfrompspath($path) ## check if file exists already. if does, check ## -force - generate error if not specified. if(test-path $zipname) { if($force) { remove-item $zipname -force } else { throw "item specified name $zipname exists." } } ## add dll helps file compression add-type -assembly system.io.compression.filesystem try { ## open zip archive $archive = [system.io.compression.zipfile]::open($zipname, "create") ## go through each file in input, adding zip file ## specified foreach($file in $input) { ## skip current file if zip file if($file.fullname -eq $zipname) { continue } ## skip directories if($file.psiscontainer) { continue } $item = $file | get-item $null = [system.io.compression.zipfileextensions]::createentryfromfile( $archive, $item.fullname, $item.name) } } { ## close file $archive.dispose() $archive = $null }  } 

to use example:

dir c:\folder -recurse | new-zipfile -path c:\temp\folder.zip 

the source file(for 1 use icsharpcode): http://poshcode.org/2202

share|improve answer
    
hi. should work take consideration first 3 letters of filenames? (i've updated question sample.) – aldursil jul 8 '15 @ 16:16

use previous answer function new-zipfile , use one:

$foldername = "c:\temp"  $files = dir $foldername  $prfx = @() foreach ($file in $files) { $prfx += $file.name.substring(0,3) }  $prfx = $prfx | group  foreach ($prf in $prfx) { $prf = $prf.name.tostring() dir $files | ? {$_.name -match "^$prf"} | new-zipfile -path "$foldername\$prf.zip" } 

according example output 3 zip files want, use first 3 letters of file, can change in line $prfx += $file.name.substring(0,3) , set different if needed.

good luck

share|improve answer
    
thanks, far i'm not able work, , feel suggested zip-code lot more complicated using 7zip. isnt suggestion on grouping same solution? -- get-childitem $new_folder -recurse -exclude metafile.xml | group-object {$_.name.substring(0,3)} -- – aldursil jul 9 '15 @ 10:53

could not suggested solution work, had problems configuring icsharpcode. wanted use 7zip, since still under updating regime.

ended copying files temp folders based on filenames , zip each folder. after delete tempfolders files. ugly code, job.

# create folder based on filename , copy files respective folder get-childitem $new_folder -filter *.* | where-object {!$_.psiscontainer} | foreach-object{  $dest = join-path $_.directoryname $_.name.substring(0,3)  if(!(test-path -path $dest -pathtype container)) {     $null = md $dest }  $_ | copy-item -destination $dest -force  }  # create zip-file of each folder dir $new_folder | where-object { $_.psiscontainer } | foreach-object { sz -t7z -mx9 "$dir_dest\$_.zip" $_.fullname } # delete temp-folders dir $new_folder | where-object { $_.psiscontainer } | remove-item -recurse 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -