powershell unzip script involving folder creation

synetron
synetron used Ask the Experts™
on
Greetings,

I'm working on a first ever Powershell project and I'm fighting to climb the curve.

Scope:
receive 3 zip files daily in folder: d:\shared\ftp
name convention is:
AAAyyyymmdd-HH.MM.SS.zip
AAByyyymmdd-HH.MM.SS.zip
AACyyyymmdd-HH.MM.SS.zip

extract for each AAA* to
d:\shared\Master\AAA\yyyymmdd (based on zip file name not current date) and
d:\shared\working\AAA\yyyymmdd
move current zip to d:\shared\archive
repeat until no more AAA

move to AAB, repeat steps above then AAC

I started by creating the date stamp directory with:
#create destination
cd c:\test\Master\MAC
md "$((get-date).toString('yyyyMMdd'))"

Open in new window


but this doesn't work when there is a backlog in the ftp directory as there is now.

I can identify the oldest of each file with:

cd c:\test\ftp
dir *AAA -recurse | sort -property lastwritetime | select -first 1 | #replace with unzip

dir *AAB -recurse | sort -property lastwritetime | select -first 1 | #replace with unzip

dir *AAC -recurse | sort -property lastwritetime | select -first 1 | #replace with unzip

Open in new window


assuming i could replace the end with a pipe to unzip the selected file it feels close, but I'm starting to hit the learning curve hard.

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I've been toying around with
#set alias to command line version of 7zip
set-alias uzip "C:\Program Files (x86)\7-Zip\7z.exe"
 
#find all files in the immediate directory
dir "C:\Users\USER1\Documents\script\test\ftp\AAA*.zip" | foreach-object{
 
dir *MAC -recurse | sort -property lastwritetime | select -first 1 | uzip z -o"C:\Users\USER1\Documents\script\test\Master\AAA"

dir *MAC -recurse | sort -property lastwritetime | select -first 1 | uzip z -o"C:\Users\USER1\Documents\script\test\Working\AAA"

Move-Item .\archive

}
 

Open in new window


this returns:
The string starting:
At C:\Users\USER1\Documents\script\test\unzip_archive.ps1:9 char:133
+ dir *AAA -recurse | sort -property lastwritetime | select -first 1 | uzip z -o"C:\Users\USER1\Documents\script\t
est\Working\AAA <<<< "
is missing the terminator: ".
At C:\Users\USER1\Documents\script\test\unzip_archive.ps1:15 char:1
+  <<<<
    + CategoryInfo          : ParserError: (
Move-Item .\archive
}

:String) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

obviously it does not like my attempt to list all files by creation date and perform an action based on oldest first more than once as I have it.

This does not include my original issue of trying to create a short sub-folder based on a portion of the name, but rather the whole name of the zip. As this would be a unique name, I'm sure I can live with it for the time being.

If anyone can help me see the light and clean up this code a bit, it would be greatly appreciated.

thanks.
Commented:
function Extract-Zip
{
	param([string]$zipfilename, [string] $destination)

	if(test-path($zipfilename))
	{	
		$shellApplication = new-object -com shell.application
		$zipPackage = $shellApplication.NameSpace($zipfilename)
		$destinationFolder = $shellApplication.NameSpace($destination)
		$destinationFolder.CopyHere($zipPackage.Items())
	}
}


#find all files in the immediate directory
dir "C:\script\test\ftp\AAA*.zip" | foreach-object{
	extract-zip $_ c:\script\test\Master\AAA
	extract-zip $_ c:\script\test\Working\AAA

}

Move-Item "C:\script\test\ftp\AAA*" "C:\script\test\ftp\archive"

Open in new window


OK, for anyone it helps in the future i give this wonderful words of wisdom imparted on me by a good friend, "sorry bro, I'm a geek, give me a second I've got to read everything."

ahh the error of my ways, if I'd just read all of THIS PAGE I would have seen the forest sooner.

please take note to all out there learning as I am that in order to reference the currently processed instance of my "foreach-object()" use a simple $_ which fits as the first required input of the function of unzip which is built in the opening section. so unzip (path of orginal) (path of destination) is unzip $_ (path of destination) in a foreach-object situation.

I hope this helps someone.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial