I have a cobbled together batch script that can extract a single zip file using built in VBS. I am now attempting to get it to read multiple files in the folder, and append the datetime stamp to the filename before saving.
Working for one file is this:
cd /d %~dp0
Call :UnZipFile "%~dp0UNZIPPED\" "%~dp0dvt_trans_C_20190517123318.dat.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
Trying to loop with /r :
setlocal
cd /d %~dp0
for /r %%v in (*.zip) do
Call :UnZipFile "%~dp0UNZIPPED\" "%%v"
exit /b
Doesn't seem to be passing filename(s) to vbs
Then I am trying to append datetime to the end of the file name, here:
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
But I'm not even sure where to start.
Ideas?
All working with:
setlocal
cd /d %~dp0
for /r %%G in (*.zip) do Call :UnZipFile "%~dp0UNZIPPED\" "%%G"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
cd /d %~dp0UNZIPPED
ren *.dat "* %Date:/= % %Time::=.%.*"
Related
so I have 2 files, a zipped file with all my files/folders for a game and a unzipper.bat file that puts everything in the correct place to work.
I decided to use IExpress to make an installer for my game, and there was an option for running a file on installation, so i put setup.bat there thinking that it would run when all the files were installed, but it didnt do that.
how can I make it so that it runs setup.bat ones all the files have been installed?
edit: this i my current SED file
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=0
HideExtractAnimation=1
UseLongFileName=0
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=I
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[Strings]
InstallPrompt=Are you sure you want to install Block Dodger?
DisplayLicense=
FinishMessage=Thank you for installing Block Dodger.
TargetName=C:\Users\Gebruiker\Desktop\Block Dodger (installer).EXE
FriendlyName=Block Dodger installer
AppLaunched=cmd.exe /c unzipper.bat
PostInstallCmd=%SystemRoot%\System32\cmd.exe /C unzipper.bat
AdminQuietInstCmd=
UserQuietInstCmd=
FILE0="Block_Dodger.zip"
FILE1="unzipper.bat"
[SourceFiles]
SourceFiles0=C:\Users\Gebruiker\Desktop\
[SourceFiles0]
%FILE0%=
%FILE1%=
when i run the installer it gives the following error:
the batch file which couldnt be found (shortcut_creator.bat) is in the zipped file unzipper.bat is supposed to unzip.
this is the content of unzipper.bat:
#echo off
setlocal
cd /d %~dp0
Call :UnZipFile "%~dp0" "%~dp0Block_Dodger.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
del /f /q "%~dp0Block_Dodger.zip"
start /d "%~dp0Block_Dodger" shortcut_creator.bat
edit 2: i just checked the box "Store files using Long Files names inside Package" and it does work now, the only thing left now is how do i change where the files end up?
In your SED file, the PostInstallCmdshould be set as :
PostInstallCmd="%SystemRoot%\System32\cmd.exe /C setup.bat"
i fixed it by checking the box "Store files using Long Files names inside Package"
you can also do it by going to the SED file and setting
UseLongFileName=0
to
UseLongFileName=1
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> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%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> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%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(...)'
#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> <NewZipFile>
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
) > "%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.
The following code (adapted from https://stackoverflow.com/a/21709923/711006) unzips file named "batchX.zip" kept in folder 'Zipped'
#echo off
setlocal
cd /d %~dp0
Call :UnZipFile "D:\Unzipped\" "D:\Zipped\batchX.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
However, the file name has to be mentioned in the batch file (batchX.zip) for it to unzip the zip file.
Can we modify this batch code so that we do not need to mention the name, and any zip file placed can be unzipped?
Also, can we unzip a .rar file through a batch code?
Unzipping any file
Can we modify this batch code so that we do not need to mention the name, and any zip file placed can be unzipped ?
Batch files can utilize parameters, just like other scripting languages. The batch-file syntax is %1.
Call :UnZipFile "D:\Unzipped\" %1
Then call your batch-file with the parameter:
myscript.bat zipped-file.zip
Unrarring a file
Also, can we unzip a .rar file through a batch code?
Yes if you have the appropriate binary. You can download rar.exe (a part of WinRAR) and use it from a batch-file (see the manual for details), e.g.
rar e %1
I have a problem whereby I am trying to determine if given directory exists on file system before attempting to download a file.
batch file:
:: Create Apache Directory if does not exist
mkdir "%HOMEDRIVE%\Apache" 2> nul
:: Setup Apache Ant if Ant does not exist
if not exist "%HOMEDRIVE%\Apache\apache-ant-1.9.7\" (
:: Set filename variable
SET "FILENAME=%~dp0\apache-ant-1.9.7-bin.zip"
:: Download ANT from mirror
bitsadmin.exe /transfer "Apache Ant Download" http://mirrors.ukfast.co.uk/sites/ftp.apache.org//ant/binaries/apache-ant-1.9.7-bin.zip "%FILENAME%"
:: Copy Apache Ant to C:\Apache-Ant
xcopy "%~dp0apache-ant-1.9.7-bin.zip" %HOMEDRIVE%\Apache\.
:: Delete zip file from curent directory
del "%~dp0apache-ant-1.9.7-bin.zip"
:: Unzip Apache Ant to C:\Apache-Ant
call :UnZipFile "%HOMEDRIVE%\Apache\" "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip"
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
:: Delete zip folder
del "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip"
:: Set ANT_HOME path
setx ANT_HOME "%HOMEDRIVE%\Apache\apache-ant-1.9.7" /m
:: Add ANT to path
setx path "%PATH%;%HOMEDRIVE%\Apache\apache-ant-1.9.7\bin" /m
)
Updated: I have added #aschipfl suggestion
the directory C:/Apache/apache-ant-1.9.7 DOES exist so the code should fail but when run it still downloads the file and tries to do further setups there. Any idea whats wrong and why the if statement is executed where it should not be ?
Thanks
Labels are not allowed within a block (series of instructions within parentheses)
an md will create intermediate directories if required.
Batch has no idea of procedures. If you call a subroutine, then when the subroutine ends (reaches end-of-file or an exit) execution will return to the instruction after the call - so :UnZipFile with your code.
Move the :unzipfile routine to the end-of-file and insert a goto :eof directly before it to ensure the code does not flow-through to :unzipfile.
Add a goto :eof to the end of :unzipfileso that you can add extra code (like more subroutines) later. goto :eof specifically means "go to physical end-of-file" which terminates the current routine.
note that setx does NOT affect the current environment, nor does it affect existing cmd instances, only new cmd instances, hence execute both set and setx.
if exist "%HOMEDRIVE%\Apache\apache-ant-1.9.7\" goto ant197exists
:: Setup Apache Ant if Ant does not exist
md "%HOMEDRIVE%\Apache\apache-ant-1.9.7\" 2>nul
:: Set filename variable
SET "FILENAME=%~dp0\apache-ant-1.9.7-bin.zip"
:: Download ANT from mirror
bitsadmin.exe /transfer "Apache Ant Download" http://mirrors.ukfast.co.uk/sites/ftp.apache.org//ant/binaries/apache-ant-1.9.7-bin.zip "%FILENAME%"
:: Copy Apache Ant to C:\Apache-Ant
xcopy "%~dp0apache-ant-1.9.7-bin.zip" "%HOMEDRIVE%\Apache\."
:: Delete zip file from curent directory
del "%~dp0apache-ant-1.9.7-bin.zip"
:: Unzip Apache Ant to C:\Apache-Ant
call :UnZipFile "%HOMEDRIVE%\Apache\" "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip"
:: Delete zip folder
del "%HOMEDRIVE%\Apache\apache-ant-1.9.7-bin.zip"
:: Set ANT_HOME path
set "ANT_HOME=%HOMEDRIVE%\Apache\apache-ant-1.9.7"
setx ANT_HOME "%HOMEDRIVE%\Apache\apache-ant-1.9.7" /m
:: Add ANT to path
set "path=%PATH%;%ant_home%"
setx path "%PATH%" /m
:ant197exists
....whatever whatever
goto :eof
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
goto :eof
I was looking for a solution to unzip my .zip file with a windows bat file.
However the solution i found:
#echo off
setlocal
cd /d %~dp0
Call :UnZipFile "C:\Temp\" "c:\path\to\batch.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
Only extracts the first files which are in the myzip.zip/ path.
For example myzip.zip/file1.txt will get extracted while
myzip.zip/folder1/file2.txt will not. How can i modify this script so all files and folders within the zip will get extracted?