Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Wait for file copy?

1,156 views
Skip to first unread message

AndrewM

unread,
Aug 22, 2008, 10:02:01 AM8/22/08
to
$logs | Add-Zip( $smtp_archive_folder.FullName + "\" +
$date.ToString("yyyyMM") + ".zip")
$logs | Remove-Item

From Add-Zip
foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
Start-sleep -milliseconds 500
}

Add-Zip uses XP/2003's compressed folders and the .copyhere method. My
problem is that second line executes while the Add-Zip is still churning
through the files in $logs, so files are deleted before they're archived.
Short of sleeping for an arbitrary amount of time (5,10,20 seconds), is there
any way I can wait on the copy? I tried replacing copyhere with movehere -
the files were still created in the zip file, but they were not removed from
the source location.

Alex K. Angelopoulos at

unread,
Aug 22, 2008, 2:51:24 PM8/22/08
to
The real problem occurs in what you didn't show; whatever Add-Zip works
from, it's performing operations asynchronously. If $zipPackage represents a
custom component of some kind, it may also have a synchronous copy command,
which will wait until it finishes to return. If instead it's just a
reference to a Shell.Application namespace reference to a zip file, the
answer is not so good. The Shell.Application methods are basically all
asynchronous because they're designed for GUI use, where actual return to
the caller would be pointless; instead, it raises events to the desktop
shell. If this is the case, there are two likely solutions.

(1) Start out with a count of the files in the zip file - I think it should
be $zipPackage.Items().Count - and start a sleep loop that waits until it
increments by one. It should look like this:

$zippedCount = $zipPackage.Items().Count
while($zipPackage.Items().Count -lt ($zippedCount + 1) ){
Start-Sleep -milliseconds 20}

The one problem with this method is that if you can't copy a particular
file, or if you copy over a pre-existing file in the zip archive, you won't
get the count to increment by one.

(2) Use a library designed for this kind of problem. One possibility is this
one, which is free and open source:
http://www.icsharpcode.net/OpenSource/SharpZipLib/
I suspect you will be _much_ happier with the results of that, rather than
using Shell.Application for this particular problem.


"AndrewM" <And...@discussions.microsoft.com> wrote in message
news:5E8F764C-7E93-48E2...@microsoft.com...

AndrewM

unread,
Aug 22, 2008, 3:03:03 PM8/22/08
to
Thanks - that makes perfect sense. I'll give the first option a try (I am
using a Shell.Application namespace reference to a zip). I'm archiving old,
daily smtp logs, so I shouldn't have the problem of copying over an
identically named file in the destination zip. If I run into problems, I'll
look into the library you linked.

Thanks again - I appreciate the detail in your reply.

Shay Levy [MVP]

unread,
Aug 23, 2008, 3:15:13 PM8/23/08
to
Hi AndrewM,

Piping to out-null should wait for Add-Zip to complete before moving on to
the next command:

PS > $logs | Add-Zip... | out-null

You may also want to check Write-Zip from PowerShell Community Extensions
(PSCX) @ http://www.codeplex.com/PowerShellCX

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

A> $logs | Add-Zip( $smtp_archive_folder.FullName + "\" +
A> $date.ToString("yyyyMM") + ".zip")
A> $logs | Remove-Item
A> From Add-Zip
A> foreach($file in $input)
A> {
A> $zipPackage.CopyHere($file.FullName)
A> Start-sleep -milliseconds 500
A> }
A> Add-Zip uses XP/2003's compressed folders and the .copyhere method.
A> My problem is that second line executes while the Add-Zip is still
A> churning through the files in $logs, so files are deleted before
A> they're archived. Short of sleeping for an arbitrary amount of time
A> (5,10,20 seconds), is there any way I can wait on the copy? I tried
A> replacing copyhere with movehere - the files were still created in
A> the zip file, but they were not removed from the source location.
A>


Alex K. Angelopoulos at

unread,
Aug 23, 2008, 3:21:24 PM8/23/08
to
Yeah, that makes sense. You may want to poison the sleep loop, though, so if
nothing changes after a while it shouts at you and dies.

"AndrewM" <And...@discussions.microsoft.com> wrote in message

news:AE8B56B0-980F-473B...@microsoft.com...

leslacroix

unread,
Dec 16, 2009, 10:07:22 PM12/16/09
to
Try this:

foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
$maxLoops = 2*60*10 # 2 minutes
while ($zipPackage.items().item($_.name) -eq $null) {
if (--$maxLoops -le 0) {
Throw "timeout exceeded"
}
Start-sleep -milliseconds 100
}
}

The inner while loop waits up to 2 minutes for the item to appear in the zip folder's item list, and throws an error if it doesn't. Not exactly complete error checking, but it's better than nothing.

Andrew wrote:

Wait for file copy?
22-Aug-08

Previous Posts In This Thread:

On Friday, August 22, 2008 10:02 AM
Andrew wrote:

Wait for file copy?

On Friday, August 22, 2008 2:51 PM
Alex K. Angelopoulos wrote:

On Friday, August 22, 2008 3:03 PM
Andrew wrote:

Thanks - that makes perfect sense.
Thanks - that makes perfect sense. I'll give the first option a try (I am
using a Shell.Application namespace reference to a zip). I'm archiving old,
daily smtp logs, so I shouldn't have the problem of copying over an
identically named file in the destination zip. If I run into problems, I'll
look into the library you linked.

Thanks again - I appreciate the detail in your reply.

"Alex K. Angelopoulos" wrote:

On Saturday, August 23, 2008 3:21 PM
Alex K. Angelopoulos wrote:

Yeah, that makes sense.


Yeah, that makes sense. You may want to poison the sleep loop, though, so if
nothing changes after a while it shouts at you and dies.

On Sunday, August 24, 2008 8:53 AM
Shay Levy [MVP] wrote:

Hi AndrewM,Piping to out-null should wait for Add-Zip to complete before
Hi AndrewM,

Piping to out-null should wait for Add-Zip to complete before moving on to
the next command:

PS > $logs | Add-Zip... | out-null

You may also want to check Write-Zip from PowerShell Community Extensions
(PSCX) @ http://www.codeplex.com/PowerShellCX

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

A> $logs | Add-Zip( $smtp_archive_folder.FullName + "\" +
A> $date.ToString("yyyyMM") + ".zip")
A> $logs | Remove-Item
A> From Add-Zip
A> foreach($file in $input)
A> {
A> $zipPackage.CopyHere($file.FullName)
A> Start-sleep -milliseconds 500
A> }

A> Add-Zip uses XP/2003's compressed folders and the .copyhere method.
A> My problem is that second line executes while the Add-Zip is still
A> churning through the files in $logs, so files are deleted before
A> they're archived. Short of sleeping for an arbitrary amount of time
A> (5,10,20 seconds), is there any way I can wait on the copy? I tried
A> replacing copyhere with movehere - the files were still created in
A> the zip file, but they were not removed from the source location.
A>


Submitted via EggHeadCafe - Software Developer Portal of Choice
C# : Create Setup project which also include multiple applications in one setup
http://www.eggheadcafe.com/tutorials/aspnet/dcc5ab62-b875-4852-9e34-8bc2794e4fb8/c--create-setup-project.aspx

0 new messages