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.
Related
I'm totally new to scripting. For the following example of codes written in batch file:
#ECHO OFF
ECHO ----------------------------------------------------------------------------------------------------
SET /P URL="[Enter video URL] "
ECHO ----------------------------------------------------------------------------------------------------
goto formatList
:formatList
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
youtube-dl -F %URL%
ECHO ----------------------------------------------------------------------------------------------------
goto selection
:selection
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
ECHO a) Video + Audio
ECHO b) Single format (Audio only / Video only)
ECHO.
SET /P option="Select option: "
if %option% == a (goto download)
if %option% == b (goto downloadSingle)
ECHO.
ECHO Unknown value
ECHO ----------------------------------------------------------------------------------------------------
goto selection
:download
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
SET /P video="Select video format: "
SET /P audio="Select audio format: "
SET /P location="Specify download location: "
ECHO.
youtube-dl --write-sub --embed-subs -o %%location%%/%%(title)s.%%(ext)s -f %video%+%audio% -i --ignore-config --hls-prefer-native %URL%
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
PAUSE
EXIT
:downloadSingle
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
ECHO ----------------------------------------------------------------------------------------------------
SET /P format="Select format: "
ECHO.
youtube-dl --write-sub --embed-subs -o %%location%%/%%(title)s.%%(ext)s -f %%format%% -i --ignore-config --hls-prefer-native %URL%
ECHO ----------------------------------------------------------------------------------------------------
ECHO.
PAUSE
EXIT
How to instead of having to type the address of folder path via 'SET /P location="Specify download location:"', have the batch file open up File Browser to select a folder and set it in the %location% variables.
Any kind of help is greatly appreciated.
Here is an example using a vbscript into a batch file :
#echo off
Title Browse a Folder and select it
Call :BrowseFolder "Select the Source folder" "C:\Program"
Set "LocationFolder=%MyFolder%"
echo "%LocationFolder%"
Pause & Exit
::-----------------------------------------------------------
:BrowseFolder
set MyFolder=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
>%vbs% echo set WshShell=CreateObject("WScript.Shell")
>>%vbs% echo set shell=CreateObject("Shell.Application")
>>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "set MyFolder=Dialog Cancelled"
>>%vbs% echo WScript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo "set MyFolder=" ^& p
cscript //nologo %vbs% > %cmd%
#for /f "delims=" %%a in (%cmd%) do %%a
#for %%f in (%vbs% %cmd%) do if exist %%f del %%f
#for %%g in ("vbs cmd") do if defined %%g set %%g=
Exit /B
::-----------------------------------------------------------
Here is another code with Powershell and Batch
:: fchooser.bat
:: launches a folder chooser and outputs choice to the console
:: https://stackoverflow.com/a/15885133/1683264
#echo off
Title Browse a Folder and select it with Powershell and Batch
setlocal
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
setlocal enabledelayedexpansion
echo You chose !folder!
pause
endlocal
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::=.%.*"
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?