Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Wait for file copy?

Reply
 
Old 08-22-2008   #1 (permalink)
AndrewM


 
 

Wait for file copy?

$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.




My System SpecsSystem Spec
Old 08-22-2008   #2 (permalink)
Alex K. Angelopoulos


 
 

Re: Wait for file copy?

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" <AndrewM@xxxxxx> wrote in message
news:5E8F764C-7E93-48E2-9ACA-09827414B27B@xxxxxx
Quote:

> $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.
>
>
>
My System SpecsSystem Spec
Old 08-22-2008   #3 (permalink)
AndrewM


 
 

Re: Wait for file copy?

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:
Quote:

> 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" <AndrewM@xxxxxx> wrote in message
> news:5E8F764C-7E93-48E2-9ACA-09827414B27B@xxxxxx
Quote:

> > $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.
> >
> >
> >
>
My System SpecsSystem Spec
Old 08-23-2008   #4 (permalink)
Shay Levy [MVP]


 
 

Re: Wait for file copy?

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>


My System SpecsSystem Spec
Old 08-23-2008   #5 (permalink)
Alex K. Angelopoulos


 
 

Re: Wait for file copy?

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" <AndrewM@xxxxxx> wrote in message
news:AE8B56B0-980F-473B-832D-1A42A90E4415@xxxxxx
Quote:

> 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:
>
Quote:

>> 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" <AndrewM@xxxxxx> wrote in message
>> news:5E8F764C-7E93-48E2-9ACA-09827414B27B@xxxxxx
Quote:

>> > $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.
>> >
>> >
>> >
>>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Copy File to users home drives if a certain file exists VB Script
open a file, wait for user to amend file, close & process PowerShell
file copy operations using source file input and script? PowerShell
vista can't copy large files? another XP file-copy bug? Vista General
vista stops install after file copy before 'file gathering' no err Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46