Re: CopyHere ignores vOptions on Windows PowerShell

  • Thread starter Shay Levy [MVP]
  • Start date
S

Shay Levy [MVP]

Try this:

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> ----------------------------
LL>
LL> Thanks in advance.
LL>
LL> "Shay Levy [MVP]" wrote:
LL>

>> Hi Leonard,
>>
>> 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>

>>>> Hi Leonard,
>>>>
>>>> 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=10
>>>> 0
>>>> 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&Site
>>>> I
>>>> 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/sh
>>>> e
>>>> 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=10
>>>> 0
>>>> 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>
 

My Computer

S

Shay Levy [MVP]

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>> 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>>
LL>> Thanks in advance.
LL>>
LL>> "Shay Levy [MVP]" wrote:
LL>>

>>> Hi Leonard,
>>>
>>> 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>
>>>>> Hi Leonard,
>>>>>
>>>>> 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=1
>>>>> 0
>>>>> 0
>>>>> 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&Sit
>>>>> e
>>>>> I
>>>>> 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/s
>>>>> h
>>>>> e
>>>>> 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=1
>>>>> 0
>>>>> 0
>>>>> 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>
 

My Computer

Top