PowerShell - Creating Zip file leaves zero-byte files

When running the following on our Windows 2003 with folder compresssion turned on, I am seeing zero-byte files left-over..  Something is odd..
function create7zip([String] $aZip, [String] $aDir, $DTNew){
	If  ((Get-Item "$aDir").length -eq 0) {
		Remove-Item -Force $aDir
	} else {
		[string]$pathToZipExe = "D:\WORK\SCRIPTS\7za.exe";
		[Array]$arguments = "a", "-tzip", "$aZip", "$aDir", "-r";
		& $pathToZipExe $arguments;
		Start-Sleep -s 3
		if ($DTNew) {
			(dir $aZip).lastwritetime = $DTNew
		}
		Remove-Item -Force $aDir
	}
}

Open in new window


And is "called" by..
# 3 ARCHIVE LOGS
$server = gc env:computername
$date = Get-Date -f yyyy-MM-dd
$ZipFolder = "D:\logs\ArchiveTemp\"
$ZipFolder2 = "D:\logs\ArchiveTemp\AudForen\"
$ZipFolder3 = "D:\logs\ArchiveTemp\Tomcat\"
$company = "COMPANY"
$DataCenter = "ANYTOWN"
# LOAD IN THE FUNCTION TO DETECT TOMCAT INFO
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path
. ( Join-Path $ScriptDir tomcat.ps1 )
. ( Join-Path $ScriptDir sftp.ps1 )
. ( Join-Path $ScriptDir zip.ps1 )
. ( Join-Path $ScriptDir testlock.ps1 )
$returnvalue = TomcatLog
$LogLocation = $returnvalue[2]  # LogDir
if (Test-Path -Path $LogLocation)
{
	$files = Get-ChildItem $LogLocation
	foreach ($file in $files)
	{
		$FLName = $file.Name
		$FLPath = $file.FullName
		$CharacterCount = [regex]::matches($FLName, "-").count
		if ($CharacterCount -gt 1)
		{
			$YearVar = $FLName.Split("-")[0]
			$YearVar = $YearVar.Substring($YearVar.Length-4, 4)
			$MonthVar = $FLName.Split("-")[1]
			$DayVar = $FLName.Split("-")[2]
			$DateVar = $YearVar + "-" + $MonthVar + "-" + $DayVar
			$DateVar = $DateVar.Substring(0, 10)
			$DateAdNm = Get-Date (Get-Date $DateVar).AddDays(1) -format yyyyMMdd
			$DateAd = Get-Date (Get-Date $DateVar).AddDays(1) -format yyyy-MM-dd
			if (!($DateVar -eq $date))
			{
				if ($FLName -match 'audit|forensic')
				{
					If (!(TestFileLock $FLPath -eq -true))
					{
					$AuditLogZip = $ZipFolder2 + "{0:yyyy-MM-dd}_{1}_{2}@{3}.zip" -f $DateAdNm, $company, $server, $DataCenter
					#compress $AuditLogZip $FLPath $DateAd
					create7zip $AuditLogZip $FLPath $DateAd
					SFTP $AuditLogZip
					}
				}
				elseif ($CharacterCount -gt 2)
				{
					$HourVar = $FLName.Split("-")[3]
					if ($HourVar -gt 11)
					{
						$HourVar = $HourVar - 12
						$HourVar = $HourVar.ToString("00")
					}
					if ($HourVar -gt 0)
					{
					$Var = $MonthVar + "-" + $DayVar + "-" + $YearVar +"-"+$HourVar+"-00-00-compressed.zip"
					}
					If (!(TestFileLock $FLPath -eq -true))
					{
					#$HourlyZip = $ZipFolder + "{0:yyyy-MM-dd}-{1}{2}-00-00-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
					#compress $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					#Write-host $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					$HourlyZip = $ZipFolder + "{0}-{1}" -f $server, $Var
					#compress $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					create7zip $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					}
				}
				else
				{
					If (!(TestFileLock $FLPath -eq -true))
					{
					$TomCatZip = $ZipFolder3 + "{0}-Tomcat-{1:yyyy-MM-dd}-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
					#compress $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					#Write-host $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					create7zip $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
					}
				}
			}
		}
		else
		{
		$CurrentDate = Get-Date
		$CurrentDate = $CurrentDate.AddHours(-($CurrentDate.Hour))
		$CurrentDate = $CurrentDate.AddMinutes(-($CurrentDate.Minute))
		$fileaffected = Get-ChildItem $FLPath |  select LastWriteTime
		If ($fileaffected.LastWriteTime -lt $CurrentDate) {
		#write-host "we are going to move the file $FLPath $fileaffected.LastWriteTime"
			If (!(TestFileLock $FLPath -eq -true))
			{
			$TomCatZip = $ZipFolder3 + "{0}-Tomcat-{1:yyyy-MM-dd}-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
			#compress $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
			create7zip $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
			Write-host $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
			}
		#} else {
		#write-host "we are going to leave the file $FLPath $fileaffected.LastWriteTime"
		}
		#If ($file.LastWriteTime -lt $DatetoMove) {
		#	$TomCatZip = $ZipFolder3 + "{0}-Tomcat-{1:yyyy-MM-dd}-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
		#	compress $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
		#	}
		}
	}
}

Open in new window


Have tried a couple versions of 7-Zip..
D:\WORK\Scripts>7z

7-Zip 9.22 beta  Copyright (c) 1999-2011 Igor Pavlov  2011-04-18

as well as:
D:\WORK\Scripts>7za

7-Zip (A) 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Note - We are running Tomcat and not IIS (no ISAPI Redirect) - hence this is not relevant: http://support.microsoft.com/kb/817442 but we do have folder compression turned on.

Turned folder compression off and we still see the zero-byte files.

Thanks,

Kent
LVL 17
Kent DyerIT Security Analyst SeniorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Aren't the names of the zero byte files hinting somewhere to? Did you try to debug the code, as you are using several different tools / commands, and each could be the culprit?
0
Kent DyerIT Security Analyst SeniorAuthor Commented:
They are the same files that are being Zipped.  They are just being left as traces of their former self..  The files are the the date of the time of the delete.

Thanks,

Kent
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry, all I can suggest then is to use Process Monitor from www.sysinternals.com with a filter of "Path", "contains", ".zip", and/or "Image Name", "contains", "7zip" (or which exe you use), while acting on a single archive.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Kent DyerIT Security Analyst SeniorAuthor Commented:
Ok..  This is not a really good solution..  But, I understand - this is one of those "blue sky" questions.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you do not know the reason for a file error, using some kind of monitoring is the best practice. And everything else are guesses about what could be the culprit.
But there is one additional guess: Is there an antivirus software installed? They tend to hold files open, and sometimes delay write/deletion just enough to cause an internal application failure.
0
Kent DyerIT Security Analyst SeniorAuthor Commented:
Did actually fix this..

Changed the order of how the function is executed.
function create7zip([String] $aZip, [String] $aDir, $DTNew){
	If  ((Get-Item "$aDir").length -eq 0) {
		Start-Sleep -m 10
		Remove-Item -Force $aDir
	} else {
		[string]$pathToZipExe = "D:\WORK\SCRIPTS\7za.exe";
		[Array]$arguments = "-tzip", "-y","a", "$aZip", "$aDir";
		& $pathToZipExe $arguments;
		Remove-Item $aDir
		if ($DTNew) {
			(dir $aZip).lastwritetime = $DTNew
		}
	}
}

Open in new window


We are now removing the file that we are archiving before we set the create date of the Zip file.

HTH,

Kent
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Strange that 7zip does not have a Move option ...
0
Kent DyerIT Security Analyst SeniorAuthor Commented:
Personally, I think with the call to re-timestamp the file, maybe the issue.

Here is the VBS-equivalent:
' -- Re-date files
'    Call Touch(Server.MapPath("/"), "somefile.htm", "2005-09-01") 
'    Call Touch("C:\", "somefile.txt", "2012-01-01") 
Sub Touch(strDir, strFileName, NewDate)
	Dim objShell, objFolder, objFile
	Set objShell = CreateObject("Shell.Application") 
	Set objFolder = objShell.NameSpace(strDir) 
	Set objFile = objFolder.ParseName(strFileName) 
	If oFSO.FileExists(strDir & strFileName) Then
		objFile.ModifyDate = NewDate 
	End If
End Sub

Open in new window


Thanks,

Kent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.