Computer Hope

Microsoft => Microsoft Windows => Windows Vista and 7 => Topic started by: JOhnathan.Pierce on April 10, 2013, 12:11:20 PM

Title: Please help me alter this script
Post by: JOhnathan.Pierce on April 10, 2013, 12:11:20 PM
I need help in altering this script. This unzips a compressed folder and extracts the files to a different folder. What I need is for it to automatically overwrite any files already in that folder.

Code: [Select]
strZipFile = ""                                       'name of zip file
    outFolder = ""                                       'destination folder of unzipped files
   
    Set objShell = CreateObject( "Shell.Application" )
    Set objSource = objShell.NameSpace(strZipFile).Items()
    Set objTarget = objShell.NameSpace(outFolder)
    intOptions = 256
    objTarget.CopyHere objSource, intOptions

Any help is greatly appreciated!
Title: Re: Please help me alter this script
Post by: Salmon Trout on April 10, 2013, 01:21:04 PM
Add 16 to the value of intOptions. (You needed to Google for "copyhere")

You already chose 256 ("Display a progress dialog box but do not show the file names") or was it already in a script you got from the web? Anyhow if you add 16 ("Respond with "Yes to All" for any dialog box that is displayed") you will enforce those two behaviours on the file copy process.

You can do it like this

intOptions = 272

or like this if you want

intOptions = 256 + 16

Option flags (these are cumulative. That means "add them together".)

   4 Do not display a progress dialog box.
   8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
  16 Respond with "Yes to All" for any dialog box that is displayed.
  64 Preserve undo information, if possible.
 128 Perform the operation on files only if a wildcard file name (*.*) is specified.
 256 Display a progress dialog box but do not show the file names.
 512 Do not confirm the creation of a new directory if the operation requires one to be created.
1024 Do not display a user interface if an error occurs.
2048 Version 4.71. Do not copy the security attributes of the file.
4096 Only operate in the local directory. Do not operate recursively into subdirectories.
8192 Version 5.0. Do not copy connected files as a group. Only copy the specified files.


example:

intOptions = 4 + 16 + 1024

or

intOptions = 1044

Do not display a progress dialog box
Respond with "Yes to All" for any dialog box that is displayed
Do not display a user interface if an error occurs



Title: Re: Please help me alter this script
Post by: JOhnathan.Pierce on April 10, 2013, 01:36:35 PM
That was it. I found this on the web and everything was already in place. Thank you very much!
Title: Re: Please help me alter this script
Post by: JOhnathan.Pierce on April 10, 2013, 02:37:04 PM
I am still having an issue where it asks me if I want to overwrite on a windows xp computer?
Title: Re: Please help me alter this script
Post by: Salmon Trout on April 10, 2013, 03:13:14 PM
show the script you are using
Title: Re: Please help me alter this script
Post by: JOhnathan.Pierce on April 11, 2013, 06:08:58 AM
Code: [Select]
    strZipFile = "C:\Boxend\Labels.zip"                           'name of zip file
    outFolder = "C:\Boxend\"                                       'destination folder of unzipped files
   
   
    Set objShell = CreateObject( "Shell.Application" )
    Set objSource = objShell.NameSpace(strZipFile).Items()
    Set objTarget = objShell.NameSpace(outFolder)
    intOptions = intOptions = 4 + 16 + 1024
    objTarget.CopyHere objSource, intOptions

Thank you for all the help!

It works fine on my Windows 7 laptop but when I use this on a Windows XP machine, it still brings up the overwrite box that says yes, yes to all etc.
Title: Re: Please help me alter this script
Post by: Salmon Trout on April 11, 2013, 12:20:55 PM
I checked this on an XP machine, and yes, you are right, in that OS you cannot silently overwrite existing files with the same name with copyhere when extracting from a zip. I did a bit of research and found Microsoft forum discussions which indicate that it works fine on Windows 7, erratically on Vista, and not at all on XP.

The documentation for folder.copyhere

http://msdn.microsoft.com/en-us/library/bb787866

says

Quote
Note In some cases, such as compressed (.zip) files, some option flags may be ignored by design.

A suggested workaround (I have not tried it) is to extract the files to a temporary folder and then copy them to the actual destination

Set fso = CreateObject("Scripting.FileSystemObject")

'create temporary folder with random name
Randomize
tempFolder = fso.BuildPath(fso.GetSpecialFolder(2), Fix(Rnd * 100000))
fso.CreateFolder tempFolder

strZipFile = "Location.zip"    'name of zip file
outFolder = "Location output folder" 'destination folder of unzipped files

Set objShell = CreateObject( "Shell.Application" )
Set objSource = objShell.NameSpace(strZipFile).Items
Set objTarget = objShell.NameSpace(tempFolder)
objTarget.CopyHere objSource

fso.CopyFolder tempFolder, outFolder, True
fso.DeleteFolder tempFolder, True   'delete temporary folder

Title: Re: Please help me alter this script
Post by: Salmon Trout on April 11, 2013, 02:20:16 PM
A suggested workaround (I have not tried it)

I have now tested this script on Windows 7 and XP

Refer to previous posts for option flags

'name of zip file
strZipFile = "C:\Test\Test.zip"

'destination folder of unzipped files
 outFolder = "C:\Testfolder"
 
Set objShell = CreateObject( "Shell.Application" )
Set objTarget = objShell.NameSpace(outFolder)
Set fso = CreateObject("Scripting.FileSystemObject")

'create folder with random name in system temp folder
tname = fso.GetTempName()
tempFolder = fso.BuildPath(fso.GetSpecialFolder(2), tname)
fso.CreateFolder tempFolder

Set objSource = objShell.NameSpace(strZipFile).Items
Set objTemp = objShell.NameSpace(tempFolder)

'extract zip contents to temp folder
intOptions = 256+16
objTemp.CopyHere objSource, intOptions

set objsource = objShell.NameSpace(tempFolder).items

'copy from temp folder to target folder
intOptions = 256+16
objTarget.CopyHere objSource, intOptions

'delete temp folder and its contents
fso.DeleteFolder tempFolder, True   'delete temporary folder



Title: Re: Please help me alter this script
Post by: Salmon Trout on April 11, 2013, 02:32:40 PM
intOptions = intOptions = 4 + 16 + 1024

Just noticed this in your code that you posted. Not sure why you have intOptions = twice, but that line will not do what you think it will.