Does anyone know why the CopyHere ignores vOptions?
I'm using Windows PowerShell 2 CTP2, Windows XP, and Microsoft .NET
Framework 3.5.
Related information available below. However, no solution given.
1.
http://www.codeproject.com/KB/cs/decompresswinshellapics.aspx?df=100&forumid=221403&exp=0&select=1405570#xx1405570xx
Quoted from Gerald Gibson Jr:
What I have found is the vOptions parameter does not work. This is what led
me to making this article in the first place. I wanted to use the vOptions
paramter '4' to tell it to NOT display a dialog box at all... but it simply
does not work. To get around this I used the console app approach where I can
tell it to not show any output.
End Quote
2. https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1471722&SiteID=1
Quote from brentil:
I'm really not sure if this is the right place for this, but I saw some
others ask about the CopyHere in this section as well.
I'm attempting to use the CopyHere Shell32 function through a C#
application. I'm compilign with VS2005 latest SP at the time of writing and
newest .NET 2 on a WinXP SP2 machine and attempting to run the code on a
Win2003 Server SP2.
The line of code specifies for vOptions 20 which is
* 4 Do not display a progress dialog box.
* 16 Respond with "Yes to All" for any dialog box that is displayed.
DestFlder.CopyHere(items, 20);
Well it seems to be ignoring the options. It shows the decompression window
and then prompts to overwrite every file. Another individual here has
replicated this as well;
http://www.codeproject.com/csharp/decompresswinshellapics.asp?df=100&forumid=221403&exp=0&select=1405570#xx1405570xx
Am I just doing it wrong using the value 20, or is it ignoring the option?
End Quote
Below are my Windows PowerShell script.
---------------------------------------------------------------------------------
function Extract-Zip #Usage: Extract-Zip C:\Temp\Archive.zip
c:\Temp\Destination
{
param([string]$zipfilename, [string] $destination)
if(test-path($zipfilename))
{
[System.Int32]$yesToAll = 16
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
#Copy one file by one file
# foreach ($item in $zipPackage.Items())
# {
# $destinationFolder.CopyHere($item,$yesToAll)
# }
#Copy the whole folder
$destinationFolder.CopyHere($zipPackage.Items(),$yesToAll)
}
}
$source = "C:\Scripts\MicrosoftSysInternalsDownloads\Packages\Autoruns.zip"
$dest = "C:\Temp"
Extract-Zip $source $dest
---------------------------------------------------------------------------------
Thanks in advance.
This works for me without a problem:
[System.Int32]$yesToAll = 16
$shellApplication = new-object -com shell.application
$test1 = $shellApplication.NameSpace("D:\Scripts\temp\test1")
$test2 = $shellApplication.NameSpace("D:\Scripts\temp\test2")
$test2.CopyHere($test1,$yesToAll)
BTW, if you already use powershell code, why don't you use PowerShell's native
commands:
# copies the entire contents of the $test1 directory into the $test2 directory
and overwrites the files
PS > copy-item $test1 -destination $test1 -recurse -force
---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
LL> Hi,
LL>
LL> Does anyone know why the CopyHere ignores vOptions?
LL>
LL> I'm using Windows PowerShell 2 CTP2, Windows XP, and Microsoft .NET
LL> Framework 3.5.
LL>
LL> Related information available below. However, no solution given.
LL>
LL> 1.
LL>
LL> http://www.codeproject.com/KB/cs/decompresswinshellapics.aspx?df=100
LL> &forumid=221403&exp=0&select=1405570#xx1405570xx
LL>
LL> Quoted from Gerald Gibson Jr:
LL> What I have found is the vOptions parameter does not work. This is
LL> what led
LL> me to making this article in the first place. I wanted to use the
LL> vOptions
LL> paramter '4' to tell it to NOT display a dialog box at all... but it
LL> simply
LL> does not work. To get around this I used the console app approach
LL> where I can
LL> tell it to not show any output.
LL> End Quote
LL> 2.
LL> https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1471722&SiteI
LL> D=1
LL>
LL> Quote from brentil:
LL> I'm really not sure if this is the right place for this, but I saw
LL> some
LL> others ask about the CopyHere in this section as well.
LL> I'm attempting to use the CopyHere Shell32 function through a C#
LL> application. I'm compilign with VS2005 latest SP at the time of
LL> writing and newest .NET 2 on a WinXP SP2 machine and attempting to
LL> run the code on a Win2003 Server SP2.
LL>
LL> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/she
LL> llcc/platform/shell/reference/objects/folder/copyhere.asp
LL>
LL> The line of code specifies for vOptions 20 which is
LL>
LL> * 4 Do not display a progress dialog box.
LL> * 16 Respond with "Yes to All" for any dialog box that is
LL> displayed.
LL> DestFlder.CopyHere(items, 20);
LL>
LL> Well it seems to be ignoring the options. It shows the
LL> decompression window
LL>
LL> and then prompts to overwrite every file. Another individual here
LL> has
LL>
LL> replicated this as well;
LL>
LL> http://www.codeproject.com/csharp/decompresswinshellapics.asp?df=100
LL> &forumid=221403&exp=0&select=1405570#xx1405570xx
LL>
LL> Am I just doing it wrong using the value 20, or is it ignoring the
LL> option? End Quote
LL>
LL> Below are my Windows PowerShell script.
LL>
LL> --------------------------------------------------------------------
LL> -------------
LL>
LL> function Extract-Zip #Usage: Extract-Zip C:\Temp\Archive.zip
LL> c:\Temp\Destination
LL> {
LL> param([string]$zipfilename, [string] $destination)
LL> if(test-path($zipfilename))
LL> {
LL> [System.Int32]$yesToAll = 16
LL> $shellApplication = new-object -com shell.application
LL> $zipPackage = $shellApplication.NameSpace($zipfilename)
LL> $destinationFolder = $shellApplication.NameSpace($destination)
LL> #Copy one file by one file
LL> # foreach ($item in $zipPackage.Items())
LL> # {
LL> # $destinationFolder.CopyHere($item,$yesToAll)
LL> # }
LL> #Copy the whole folder
LL> $destinationFolder.CopyHere($zipPackage.Items(),$yesToAll)
LL> }
LL> }
LL> $source =
LL> "C:\Scripts\MicrosoftSysInternalsDownloads\Packages\Autoruns.zip"
LL> $dest = "C:\Temp"
LL>
LL> Extract-Zip $source $dest
LL>
LL> --------------------------------------------------------------------
LL> -------------
LL>
LL> Thanks in advance.
LL>
Thanks for the reply.
The CopyHere from Shell.Application object ignores the vOptions to force
override.
The problem here is that Copy-Item currently cannot work on the
Shell.Application folder objects.
#Copy the whole folder
#$destinationFolder.CopyHere($zipPackage.Items(),$yesToAll)
Copy-Item $zipPackage.Items() -Destination $destinationFolder -Recurse -Force
The Copy-Item code above will throw us an exception as shown below.
System.__ComObject System.__ComObject System.__ComObject System.
__ComObject' because it does not exist.
Thanks in advance.
Try to add 'self.path' to each shellNameSpace folder. copy-item expects a
path on the special folder.
Again, if you already using powershell in your solution, make it powershell
only, its only one line of code :)
$desktop = 0
[System.Int32]$yesToAll = 16
$shellApplication = new-object -com shell.application
$dt = $shellApplication.NameSpace($desktop).self.path
$destinationFolder = $shellApplication.NameSpace("d:\scripts\temp\test2").self.path
#$destinationFolder.CopyHere($dt,$yesToAll)
copy-item $dt -destination $destinationFolder -recurse -force
---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
LL> Hi Shay,
LL>
LL> Thanks for the reply.
LL>
LL> The CopyHere from Shell.Application object ignores the vOptions to
LL> force override.
LL>
LL> The problem here is that Copy-Item currently cannot work on the
LL> Shell.Application folder objects.
LL>
LL> #Copy the whole folder
LL> #$destinationFolder.CopyHere($zipPackage.Items(),$yesToAll)
LL> Copy-Item $zipPackage.Items() -Destination $destinationFolder
LL> -Recurse -Force
LL>
LL> The Copy-Item code above will throw us an exception as shown below.
LL> System.__ComObject System.__ComObject System.__ComObject System.
LL> __ComObject' because it does not exist.
LL>
LL> Thanks in advance.
LL>
LL> "Shay Levy [MVP]" wrote:
LL>
Thanks for your fast reply.
The problem I faced here is to extract contents in the zip file into a
folder, which it will also overwrite any files contained within that folder.
Do you have any "Windows PowerShell" way to extract zip file and force
overwrite of files on the destination folders?
I did tried your advices. It copied the Zip file only, but never extract the
contents out into the destination folder with force overwrite. Below are my
modified script...
------------------------------------------------------------------------------------------------
function Extract-Zip #Usage: Extract-Zip C:\Temp\Archive.zip
c:\Temp\Destination
{
param([string]$zipfilename, [string] $destination)
if(Test-Path($zipfilename))
{
[System.Int32]$yesToAll = 16
$shellApplication = New-Object -com Shell.Application
# $zipPackage = $shellApplication.NameSpace($zipfilename)
$zipPackage = $shellApplication.NameSpace($zipfilename).Self.Path
# $destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder =
$shellApplication.NameSpace($destination).Self.Path
#Copy one file by one file
# foreach ($item in $zipPackage.Items())
# {
# $destinationFolder.CopyHere($item,$yesToAll)
# }
#Copy the whole folder
# $destinationFolder.CopyHere($zipPackage.Items(),$yesToAll)
# Copy-Item $zipPackage.Items() -Destination $destinationFolder -Recurse
-Force
Copy-Item $zipPackage -Destination $destinationFolder -Recurse
-Force #Copy the whole zip file.
}
}
$source = "C:\Scripts\MicrosoftSysInternalsDownloads\Packages\Autoruns.zip"
$dest = "C:\Temp"
------------------------------------------------------------------------------------------------
Thanks in advance.
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)
# create temp directory to store the files
$tmpName = [System.IO.Path]::GetRandomFileName()
$tmpDir = New-Item -type directory -Path "$env:temp" -Name $tmpName -force
$files = $shellApplication.NameSpace($tmpDir.fullname)
$files.CopyHere($zipPackage.Items())
# move the files to $destinationFolder
dir $files.self.path | Move-Item -Destination $destinationFolder.self.path
-force
}
}
---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
LL> Hi Shay,
LL>
LL> Thanks for your fast reply.
LL>
LL> The problem I faced here is to extract contents in the zip file into
LL> a folder, which it will also overwrite any files contained within
LL> that folder.
LL>
LL> Do you have any "Windows PowerShell" way to extract zip file and
LL> force overwrite of files on the destination folders?
LL>
LL> I did tried your advices. It copied the Zip file only, but never
LL> extract the contents out into the destination folder with force
LL> overwrite. Below are my modified script...
LL>
LL> --------------------------------------------------------------------
LL> ----------------------------
LL>
LL> function Extract-Zip #Usage: Extract-Zip C:\Temp\Archive.zip
LL> c:\Temp\Destination
LL> {
LL> param([string]$zipfilename, [string] $destination)
LL> if(Test-Path($zipfilename))
LL> {
LL> [System.Int32]$yesToAll = 16
LL> $shellApplication = New-Object -com Shell.Application
LL> # $zipPackage = $shellApplication.NameSpace($zipfilename)
LL> $zipPackage =
LL> $shellApplication.NameSpace($zipfilename).Self.Path
LL> # $destinationFolder = $shellApplication.NameSpace($destination)
LL> $destinationFolder =
LL> $shellApplication.NameSpace($destination).Self.Path
LL> #Copy one file by one file
LL> # foreach ($item in $zipPackage.Items())
LL> # {
LL> # $destinationFolder.CopyHere($item,$yesToAll)
LL> # }
LL> #Copy the whole folder
LL> # $destinationFolder.CopyHere($zipPackage.Items(),$yesToAll)
LL> # Copy-Item $zipPackage.Items() -Destination $destinationFolder
LL> -Recurse
LL> -Force
LL> Copy-Item $zipPackage -Destination $destinationFolder
LL> -Recurse
LL> -Force #Copy the whole zip file.
LL> }
LL> }
LL> $source =
LL> "C:\Scripts\MicrosoftSysInternalsDownloads\Packages\Autoruns.zip"
LL> $dest = "C:\Temp"
LL>
LL> --------------------------------------------------------------------
LL> ----------------------------
Add the -force parameter to move-item (if necessary)
dir $files.self.path | Move-Item -Destination $destinationFolder.self.path
-force
---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
SL> Try this:
SL>
SL> function Extract-Zip{
SL> param([string]$zipfilename, [string] $destination)
SL> if(test-path $zipfilename){
SL> $shellApplication = new-object -com shell.application
SL> $zipPackage = $shellApplication.NameSpace($zipfilename)
SL> $destinationFolder = $shellApplication.NameSpace($destination)
SL> # create temp directory to store the files
SL> $tmpName = [System.IO.Path]::GetRandomFileName()
SL> $tmpDir = New-Item -type directory -Path "$env:temp" -Name
SL> $tmpName -force
SL> $files = $shellApplication.NameSpace($tmpDir.fullname)
SL> $files.CopyHere($zipPackage.Items())
SL> # move the files to $destinationFolder
SL> dir $files.self.path | Move-Item -Destination
SL> $destinationFolder.self.path
SL> -force
SL> }
SL> }
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL> blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
LL>> Hi Shay,
LL>>
LL>> Thanks for your fast reply.
LL>>
LL>> The problem I faced here is to extract contents in the zip file
LL>> into a folder, which it will also overwrite any files contained
LL>> within that folder.
LL>>
LL>> Do you have any "Windows PowerShell" way to extract zip file and
LL>> force overwrite of files on the destination folders?
LL>>
LL>> I did tried your advices. It copied the Zip file only, but never
LL>> extract the contents out into the destination folder with force
LL>> overwrite. Below are my modified script...
LL>>
LL>> -------------------------------------------------------------------
LL>> - ----------------------------
LL>> -------------------------------------------------------------------
LL>> - ----------------------------
Thanks for the great solution. Definitely the MVP for Windows PowerShell.
I think this solution will draw more people to use Windows PowerShell
compared to other conventional methods on force overwrite of files after
extracted from a zip file.
Thanks for the advice and help!
Let me share my last modified script on extracting zip content and force
overwrite on the destination folder.
---------------------------------------------------------------------------------
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)
# Create temp directory to store the files
$tmpName = [System.IO.Path]::GetRandomFileName()
$tmpDir = New-Item -Type Directory -Path "$Env:Temp" -Name $tmpName -Force
$files = $shellApplication.NameSpace($tmpDir.fullname)
$files.CopyHere($zipPackage.Items())
# Move the files to $destinationFolder
Dir $files.Self.Path | Move-Item -Destination
$destinationFolder.Self.Path -Force
}
}
$source = "C:\Scripts\MicrosoftSysInternalsDownloads\Packages\Autoruns.zip"
$destination = "C:\Temp"
Extract-Zip $source $destination
---------------------------------------------------------------------------------
Hope this helps.
There is an add-on zip library that may make more sense for some
powershell scripters:
http://blogs.msdn.com/dotnetinterop/archive/2008/06/26/zip-creation-within-powershell.aspx