-3

I use Window's Shell CopyHere method to unzip archives in some batches. It generally works, as shown in the script example below, but gives an error in some cases. In particular, I found it gives an error when unzipping an archive to a special system folder like Downloads, if it was moved by a user to a new location, despite the folder symlink was left by OS in its default location in the user profile, and the user has write permissions for the new location.

Is there a way to work around this limitation or modify the code below without changing its overall batch hybrid approach?

<!-- : Begin batch script
@echo off
set "dir=%temp%" & set "file=%USERPROFILE%\Backup\archive.zip\"
cscript //nologo "%~f0?.wsf" "%dir%" "%file%"
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
 set fso = CreateObject("Scripting.FileSystemObject")
 Set Ag=Wscript.Arguments
 If NOT fso.FolderExists(Ag(0)) Then
 fso.CreateFolder(Ag(0))
 End If
 set objShell = CreateObject("Shell.Application")
 set FilesInZip = objShell.NameSpace(Ag(1)).items
 objShell.NameSpace(Ag(0)).CopyHere(FilesInZip)
 set fso = Nothing
 set objShell = Nothing
</script></job>

:: Error if user profile's Downloads folder was moved from default location

\test.bat?.wsf(15, 3) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'
sambul35
  • 1,058
  • 14
  • 22
  • @Dummy00001 Why do you think Shell tag is not relevant to my question, if it directly asks about the Shell method used in the batch? IMO its a typical example of unnecessary minor edit prohibited by SO rules made to justify downvote without any attempt to answer the question. – sambul35 Jul 29 '16 at 15:24
  • The batch is a hybrid, pls do not remove Shell-method tag from it. Unfortunately, any tag related to shell methods use is truncated to Shell tag by the site. – sambul35 Jul 29 '16 at 15:46
  • 1
    the `shell` tag is used for Unix/Linux/POSIX shells, not for Windows. Simply read the description of the tag. – Dummy00001 Jul 30 '16 at 16:53
  • @Dummy00001 This simply means, a similar tag like _shell-method_ should be created for Windows shell. Can you do that assuming you have enough rep, or suggest an alternative here, unless you deny Windows has shell. Without offering an alternative, your minor edit and useless downvote seems to violate SO rules for edits. – sambul35 Jul 30 '16 at 17:18
  • @Dummy00001 May be you need to read [this](http://meta.stackoverflow.com/questions/330186/what-tag-to-use-for-windows-shell-questions) topic. And the tag description says "_most often associated_" with Linux. It doesn't prohibit its use in Windows. – sambul35 Jul 30 '16 at 21:43

1 Answers1

0

It appears CopyHere method doesn't support special folder path redirection. If a user changed location of a special Windows folder listed in the Enumerating Special Folders document like My Documents, a simple VBS construct would allow to find current folder and file path:

set objShell = CreateObject("Shell.Application")
set objFolder = objShell.Namespace(&H5&)
set FilesInZip = objShell.NameSpace(Ag(1)).items
objShell.NameSpace(objFolder).CopyHere(FilesInZip)

Otherwise, VBS doesn't offer any method to find current special folder path. The only way I found to get it in a hybrid batch & VBS script is by querying the Registry key HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders. Current .zip file path is then sent as cscript argument using script in question.

For example, if archive.zip to be unzipped to current Downloads folder:

@echo off
setlocal EnableDelayedExpansion
set "key=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
for /f "tokens=* Skip=2" %%g in ('reg query "%key%" 2^>nul') do (
    echo %%g | find /i "Downloads" >nul && for %%o in (%%g) do (
        set "disk=%%o" & if exist "!disk:~0,3!" set dir=%%o ))
echo !dir!

K:\Downloads
sambul35
  • 1,058
  • 14
  • 22