Title: How to Unzip a file with dynamic name using batch file?
Tags: batch-file;server
Question: All i am trying is to automate the unzip process through batch file and scheduling the same, it worked when the file name is static (mentioned below)
@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "D:\QlikSense\Data\OBL\FTP\" "D:\QlikSense\Data\OBL\FTP\T56Q_OBL001_20200312111131.zip"
Call :UnZipFile "D:\QlikSense\Data\OBL\FTP\" "D:\QlikSense\Data\OBL\FTP\T56Q_OBL002_20200312111131.zip"
exit /b
:UnZipFile <ExtractTo&gt; <newzipfile&gt;
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
&gt;%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
&gt;&gt;%vbs% echo If NOT fso.FolderExists(%1) Then
&gt;&gt;%vbs% echo fso.CreateFolder(%1)
&gt;&gt;%vbs% echo End If
&gt;&gt;%vbs% echo set objShell = CreateObject("Shell.Application")
&gt;&gt;%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
&gt;&gt;%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
&gt;&gt;%vbs% echo Set fso = Nothing
&gt;&gt;%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
timeout 20
But the same is not working when it is made for Dynamic using ' * ' (find the code below)
@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "D:\QlikSense\Data\OBL\FTP\" "D:\QlikSense\Data\OBL\FTP\T56Q_OBL001_*.zip\"
Call :UnZipFile "D:\QlikSense\Data\OBL\FTP\" "D:\QlikSense\Data\OBL\FTP\T56Q_OBL002_*.zip\"
exit /b
:UnZipFile <ExtractTo&gt; <newzipfile&gt;
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
&gt;%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
&gt;&gt;%vbs% echo If NOT fso.FolderExists(%1) Then
&gt;&gt;%vbs% echo fso.CreateFolder(%1)
&gt;&gt;%vbs% echo End If
&gt;&gt;%vbs% echo set objShell = CreateObject("Shell.Application")
&gt;&gt;%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
&gt;&gt;%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
&gt;&gt;%vbs% echo Set fso = Nothing
&gt;&gt;%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
timeout 20
The following is the error reflecting on the case
C:\Users\TECHADMIN\Appdata\Local\Temp\4_.vbs(6, 1) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'
Comment: It is called "asterix in file name".

Comment: Sorry for the quick comment. The official name of this kind of filenames is not "dynamic". It is called "asterix".

Comment: This looks awfully complex. Can I suggest using PowerShell?

Comment: i am sorry ! i couldnt understand, can you please elaborate ?

Comment: @JamesZ Yes please

Here is the accepted answer: @echo off
setlocal
cd /d "%~dp0"
Call :UnZipFile "D:\QlikSense\Data\OBL\FTP\" "D:\QlikSense\Data\OBL\FTP\T56Q_OBL001_*.zip"
Call :UnZipFile "D:\QlikSense\Data\OBL\FTP\" "D:\QlikSense\Data\OBL\FTP\T56Q_OBL002_*.zip"
exit /b
:UnZipFile <ExtractTo&gt; <NewZipFile&gt;
set "vbs=%temp%\_.vbs"
if exist "%vbs%" del /f /q "%vbs%"
(   echo strExtractTo = WScript.Arguments.Item(0^)
echo strNewZipFile = WScript.Arguments.Item(1^)
echo WScript.echo strExtractTo,  strNewZipFile
echo set objFSO = CreateObject("Scripting.FileSystemObject"^)
echo if not objFSO.FolderExists(strExtractTo^) Then
echo   fso.CreateFolder(strExtractTo^)
echo end if
echo set objShell = CreateObject("Shell.Application"^)
echo set FilesInZip = objShell.NameSpace(strNewZipFile^).items
echo objShell.NameSpace(strExtractTo^).CopyHere(FilesInZip^)
echo Set objFSO = Nothing
echo Set objShell = Nothing
) &gt; "%vbs%"
for %%A in ("%~2") do cscript //nologo "%vbs%" "%~1" "%%~A"
if exist "%vbs%" del /f /q "%vbs%"
timeout 20
Added argument handling for the VBScript and use a for loop to handle the asterisk wildcard used in the zip filenames.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:03/18/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<batch-file><server>', 'Title': 'How to Unzip a file with dynamic name using batch file?', 'ViewCount': 108, 'id': 60734998, 'user_AboutMe': None, 'user_AccountId': 17998318.0, 'user_DisplayName': 'Keerthivasan R', 'user_Reputation': 3.0, 'user_Views': 2.0, 'user_id': 13080795.0}}