Windows API to extract zip files? - winapi

In Windows Explorer you can extract a compressed folder (a zip file)
Is there an API or command line to extract a zip file using the same method programatically?

You can use this VBScript script:
'Adapted from http://www.robvanderwoude.com/vbstech_files_zip.html
strFile = "c:\filename.zip"
strDest = "c:\files"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strDest) Then
objFSO.CreateFolder(strDest)
End If
UnZipFile strFile, strDest
Sub UnZipFile(strArchive, strDest)
Set objApp = CreateObject( "Shell.Application" )
Set objArchive = objApp.NameSpace(strArchive).Items()
Set objDest = objApp.NameSpace(strDest)
objDest.CopyHere objArchive
End Sub

Check Compress Zip files with
Windows Shell API and C#
You could use SharpZipLib which
is free for a dot net project.

I tried the above function Sub UnZipFile(...) under Excel 2010 and it was not working: Run-time error '91' (Object variable or With block not set) in the line
Set objArchive = objApp.Namespace(strArchive).Items()
and the line
Set objDest = objApp.Namespace(strDest)
is silently also not working: After execution the objDest is still nothing!
Microsoft's .Namespace() accepts as parameter either an object, a string constant or a string variable. With string variables there are often suspicious problems, which are requiring an workaround:
Set objArchive = objApp.Namespace(**CStr(** strArchive **)**).Items()
Set objDest = objApp.Namespace(**CStr(** strDest **)**)
or an alternative workaround
Set objArchive = objApp.Namespace(**"" &** strArchive).Items()
Set objDest = objApp.Namespace(**"" &** strDest)
And the line objDest.CopyHere objArchive was also not working: The destination folder remained empty!
Here a version, which is working in Excel 2010 and most probably also in other environments:
Sub UnZipFile(strZipArchive As String, strDestFolder As String)
Dim objApp As Object
Dim vItem As Variant
Dim objDest As Object
Set objApp = CreateObject("Shell.Application")
Set objDest = objApp.Namespace(CStr(strDestFolder))
For Each vItem In objApp.Namespace(CStr(strZipArchive)).Items
objDest.CopyHere vItem
Next vItem
End Sub

For C# or VB users, you can check the answer from MSDN:
https://msdn.microsoft.com/en-us/library/ms404280(v=vs.100).aspx
For .net 4.x, here is the sample code from MSDN
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string startPath = #"c:\example\start";
string zipPath = #"c:\example\result.zip";
string extractPath = #"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}

Related

How to create exe file using VBS

How to make executable file with VBS. There is a code for txt but how to change it to make from this exe
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const Append = 8
Dim FDir
FDir = ("Area where file will be saved")
Const FName_Ext = "Title of document.txt"
Dim Final
Final = FDir + FName_Ext
Dim objtxt
set objtxt = objFSO.CreateTextFile(Final, True)
Set objtxt = Nothing
Dim FWrite
Set FWrite = objFSO.OpenTextFile(Final, Append)
FWrite.WriteLine("this is the text in the file.!!! hahaha lol wohoo yada yada yada. okay done!")
FWrite.Close()
Set FWrite = Nothing
Set objFSO = Nothing
Get familiar with PE Headers . Short, this is a lot of binary data that tells windows where to find what when it starts to execute your file. Although vbs was not designed for binary magic, the simple text way does the trick if you have saved your binary data into a variable, at least according to this microsoft doc.

VBScript create and open a new file

I try to make a script in VBScript for PowerAMC.
And I've got an error.
I checked all elements to make a file with the content (XSD file):
private Sub writeInFile(pathFolder, pathFile, val)
Output "WriteInFile["&pathFolder&pathFile&"]"
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(pathFolder&pathFile, true)
If (fso.FileExists(pathFolder&pathFile)) Then
MyFile.WriteLine(val)
Else
ouput "File can't be create"
End If
MyFile.Close
end Sub
And the file exists with good content, but if I try to read it with:
public Function readFile(path)
'Declare variables
Dim objFSO, objReadFile, contents
'Set Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile(path, 1, false)
'Read file contents
contents = objReadFile.ReadAll
'Close file
objReadFile.close
'Cleanup objects
Set objFSO = Nothing
Set objReadFile = Nothing
readFile = contents
End Function
I get that : "ÿþ<" for only content, but if I try to read a file that is not created by the previous function, it runs perfectly.
I think the problem comes from Unicode format,
take a look at this => FileSystemObject - Reading Unicode Files

csv file import to access database using vba

Im using visual basic and visual studio 2010.
I researched on importing csv files to access database and I found this generic codes. The problem is, I'm really new in visual basic. I declared the variables but I got the error: "Declaration expected".. and the Do while and Loop are having an error which is: "Statement cannot appear outside of a method
Public Class Form1
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
blnHasFieldNames = True
strPath = "C:\Users\vissia18\Desktop\ReportDB\"
strTable = "Report"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
strFile = Dir()
Loop
End Class
The first thing I see is that no method or subroutine has been declared.
That is essential.
How about declaring a Main like this:
Public Class Form1
Sub Main()
Dim strPathFile As String
Dim strFile As String
...
End Sub
End Class
This will give your application a staring point.
May I suggest MSDN - Microsoft Developer Network - specifically the video walk through labelled "Visual Basic Fundamentals: Development for Absolute Beginners".

create folder using WshUserEnv

Sub Copy_TNSNamesORA()
' Now look for SQLNET.ora file in %userprofile%\appdata\Roaming and if that exists copy the file to the
' the TNSNAMES folder
Dim fso
Dim f
Dim wshShell
Dim wshUserEnv
Dim TNSFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshUserEnv = WshShell.Environment("PROCESS")
TNSFolder = WshUserEnv("TNSNAMES")
Dim SQLOraTempFileName
Dim SQLOraLocalFileName
SQLOraTempFileName = WshUserEnv("userprofile") & "\appdata\Roaming" & "\Oracle\SQLNET.ORA"
SQLOraLocalFileName = TNSFolder & "SQLNET.ORA"
End Sub
I'm trying to create a folder in c\userprofile\appdata\roaming\oracle
named TNSNAMES by using this code. Can some one clarify for me that this code
TNSFolder = WshUserEnv("TNSNAMES") is suitable to use to create a folder?
According to the docs you create a folder by call the CreateFolder (surprise!) method of a FileSystemObject.
stolen demo code:
Function CreateFolderDemo
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateFolder("c:\New Folder") ' <-- new folder is born
CreateFolderDemo = f.Path ' <-- return folder spec (string) to caller
End Function
(Simply assigning a string containing a folder specification to a variable will copy the string but not automagically change your harddisk.)

Vbscript to navigate to multiple URLs listed in text file

I am trying to create a Vbscript to go through a text file containing different URLs and navigate to each URL using WshShell.Run. I am currently getting: "Object Required: urllist" Error.
Sorry I am new to Vbscript not sure where to go from here.
The urllist.txt is stored in the same directory.
Here is what I have so far:
dim listFile
dim WshShell
dim fName
Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile(urllist.txt)
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim fso
'do while not listFile.AtEndOfStream
fName = listFile.ReadLine()
Return = WshShell.Run("iexplore.exe " & fName, 1)
'loop
You missed quote-mark. Next line:
Set listFile = fso.OpenTextFile(urllist.txt)
Should be:
Set listFile = fso.OpenTextFile("urllist.txt")

Resources