Skip to content

Instantly share code, notes, and snippets.

@chrismckinnel
Last active March 3, 2023 00:26
Show Gist options
  • Save chrismckinnel/f6696e81193ee0c14c464a3e37502a12 to your computer and use it in GitHub Desktop.
Save chrismckinnel/f6696e81193ee0c14c464a3e37502a12 to your computer and use it in GitHub Desktop.
Set fileSystemObject = CreateObject ("Scripting.FileSystemObject")
Set stdout = fileSystemObject.GetStandardStream (1)
Set stderr = fileSystemObject.GetStandardStream (2)
Function ExportAccessToCSV()
stdout.WriteLine "Exporting Access table " & table & " to CSV"
Dim database: database = WScript.Arguments(0)
Dim table: table = WScript.Arguments(1)
Set wshShell = WScript.CreateObject("WScript.Shell")
Dim exportDir: exportDir=wshShell.CurrentDirectory & "\"
stdout.WriteLine "Setting export directory to current directory: " & exportDir
Dim exportFile: exportFile = getExportFilename(exportDir, table)
stdout.WriteLine "Setting export filename to: " & exportFile
If checkIfExportFileExists(exportDir, exportFile) Then
archiveExistingExportFile exportDir, exportFile
End if
Set connection = DatabaseConnection(database)
stdout.WriteLine "Executing export query..."
connection.Execute "SELECT * INTO [text;HDR=Yes;Database=" & exportDir & _
";CharacterSet=65001]." & exportFile & " FROM " & table
stdout.WriteLine "Success! Export to CSV complete."
End Function
Function getExportFilename(exportPath, table)
Dim exportFilename: exportFileName = table & "_" & Year(Date) _
& Right("00" & Month(Date),2) & Right("00" & Day(Date) ,2) & ".csv"
getExportFilename = exportFilename
End Function
Function checkIfExportFileExists(exportDir, exportFile)
stdout.WriteLine "Checking if export file " & exportDir & exportFile & " exists..."
If fileSystemObject.FileExists(exportPath & exportFile) Then
stdout.WriteLine "Export file already exists!"
checkIfExportFileExists = True
Exit Function
End If
checkIfExportFileExists = False
End Function
Function archiveExistingExportFile(exportDir, exportFile)
Dim archiveDir: archiveDir = exportDir & "archive\"
If Not fileSystemObject.FolderExists(archiveDir) Then
fileSystemObject.CreateFolder(archiveDir)
End If
archiveFilename = getArchiveFilename(archiveDir, exportFile)
stdout.WriteLine "Archiving existing export file (" & exportFile & ")" _
& " to " & archiveFilename
fileSystemObject.MoveFile exportDir & exportFile, archiveDir & archiveFilename
End Function
Function getArchiveFilename(archiveDir, exportFile)
Dim archiveFilename: archiveFilename = Left(exportFile, InStrRev(exportFile,".") - 1)
Dim archiveExtension: archiveExtension = Mid(exportFile, InStrRev(exportFile,".") + 1)
Dim newArchiveFilename: newArchiveFilename = exportFile
index = 1
Dim newArchiveFilepath: newArchiveFilepath = archiveDir & newArchiveFilename
Do While fileSystemObject.FileExists(newArchiveFilepath)
newArchiveFilename = archiveFilename & "_" & index & "." & archiveExtension
newArchiveFilepath = archiveDir & newArchiveFilename
index = index + 1
Loop
getArchiveFilename = newArchiveFilename
End Function
Function DatabaseConnection(database)
stdout.WriteLine "Connecting to database..."
Set connection = CreateObject("ADODB.Connection")
connection.Open _
"Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source =" & database
Set DatabaseConnection = connection
stdout.WriteLine "Successfully connected to database " & database
End Function
Function usage()
stdout.WriteLine "Note: For this script to work you need to invoke it with the"
stdout.WriteLine "32-bit version of wscript.exe due to the database drivers it uses"
stdout.WriteLine
stdout.WriteLine "c:\Windows\SysWOW64\wscript.exe access-to-csv.vbs ACCESS_DB_FILENAME TABLE_TO_EXPORT"
stdout.WriteLine
stdout.WriteLine "Example usage:"
stdout.WriteLine
stdout.WriteLine "c:\Windows\SysWOW64\wscript.exe access-to-csv.vbs your-db.accdb CHOLERA"
End Function
If Not WScript.Arguments.Count = 2 Then
usage()
WScript.Quit
End If
ExportAccessToCSV()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment