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!