This is a simple vbscript compression script. works flawlessly on text files even bigger than 15GB.
We have a SSIS data generation package installed on our DB server which generates huge data files for data mining operations for current year, current month. The below scripts are doing the rest; compress the files and FTP to the data mining server location.
Save the above script as compress_ftp.vbs, and run it on task scheduler with cscript. For the total file size around 20GB, this process will take around 1 hour.
We have a SSIS data generation package installed on our DB server which generates huge data files for data mining operations for current year, current month. The below scripts are doing the rest; compress the files and FTP to the data mining server location.
'======================================================' Function : Zip datamart CSV, CTL files to local server for archive purposes.'======================================================Function WindowsZip(sFile, sZipFile, szPath, szDate)Set oZipShell = CreateObject("WScript.Shell")Set oZipFSO = CreateObject("Scripting.FileSystemObject")Set LogFile = oZipFSO.CreateTextFile(szPath & "\Logs\log_" & szDate & ".log", true)LogFile.WriteLine("=======================================")LogFile.WriteLine(Now & " - Compressing CSV file - Starting...")LogFile.WriteLine("=======================================")If Not oZipFSO.FileExists(sZipFile) ThenNewZip(sZipFile)End IfSet oZipApp = CreateObject("Shell.Application")Set source = oZipApp.NameSpace(sFile).ItemssZipFileCount = oZipApp.NameSpace(sZipFile).items.CountoZipApp.NameSpace(sZipFile).Copyhere sourceLogFile.WriteLine(sZipFile & " - Compressing -")'Keep script waiting until Compressing is doneOn Error Resume NextsLoop = 0Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.CountWscript.Sleep(100)sLoop = sLoop + 1LoopOn Error GoTo 0LogFile.WriteLine(sZipFile & " - Successfully compressed -")LogFile.WriteBlankLines 1LogFile.CloseSet LogFile = NothingSet oZipShell = NothingSet oZipFSO = Nothing
End FunctionSub NewZip(sNewZip)Set oNewZipFSO = CreateObject("Scripting.FileSystemObject")Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)oNewZipFile.CloseSet oNewZipFSO = NothingWscript.Sleep(500)End Sub
'======================================================' Function : Upload datamart CSV, CTL files to MTMS FTP server.'======================================================Function FTPUpload(sSite, sUsername, sPassword, sLocalFile, sRemotePath, szPath, szDate, szYear, szMonth)Const OpenAsDefault = -2Const FailIfNotExist = 0Const ForReading = 1Const ForWriting = 2Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")Set oFTPScriptShell = CreateObject("WScript.Shell")Set LogFile = oFTPScriptFSO.OpenTextFile(szPath & "\Logs\log_" & szDate & ".log", 8)LogFile.WriteLine("========================================")LogFile.WriteLine(Now & " - Sending CSV file on FTP - Starting...")LogFile.WriteLine("========================================")sRemotePath = Trim(sRemotePath)sLocalFile = Trim(sLocalFile)'Check to ensure that a remote path was'passed. If it's blank then pass a "\"If Len(sRemotePath) = 0 ThensRemotePath = "\"End IfIf InStr(sLocalFile, "*") ThenIf InStr(sLocalFile, " ") ThenFTPUpload = "Error: Wildcard uploads do not work if the path contains a " & "space." & vbCRLFFTPUpload = FTPUpload & "This is a limitation of the Microsoft FTP client."LogFile.WriteLine(FTPUpload)Exit FunctionEnd IfElseIf Len(sLocalFile) = 0 Or Not oFTPScriptFSO.FileExists(sLocalFile) Then'nothing to uploadFTPUpload = "Error: File Not Found."LogFile.WriteLine(sLocalFile & FTPUpload)Exit FunctionEnd If'--------END Path Checks---------'build input file for ftp commandsFTPScript = sFTPScript & "USER " & sUsername & vbCRLFsFTPScript = sFTPScript & sPassword & vbCRLFsFTPScript = sFTPScript & "prompt " & vbCRLFsFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLFsFTPScript = sFTPScript & "binary" & vbCRLFsFTPScript = sFTPScript & "lcd " & szPath & vbCRLFsFTPScript = sFTPScript & "mput " & sLocalFile & vbCRLFsFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLFsFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempNamesFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName'Write the input file for the ftp command'to a temporary file.Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)fFTPScript.WriteLine(sFTPScript)fFTPScript.CloseSet fFTPScript = NothingoFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & sSite & " > " & sFTPResults, 0, TRUEWscript.Sleep 1000'Check results of transfer.Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, FailIfNotExist, OpenAsDefault)'sResults = fFTPResults.ReadAllDo Until fFTPResults.AtEndOfStreamsResults = sResults & vbCRLF & fFTPResults.ReadLineLoopfFTPResults.CloseoFTPScriptFSO.DeleteFile(sFTPTempFile)oFTPScriptFSO.DeleteFile (sFTPResults)If InStr(sResults, "226 Transfer complete.") > 0 ThenFTPUpload = "226 Transfer complete."ElseIf InStr(sResults, "File not found") > 0 ThenFTPUpload = "Error: File Not Found"ElseIf InStr(sResults, "cannot log in.") > 0 ThenFTPUpload = "Error: Login Failed."ElseFTPUpload = sResults & "Error: Unknown."End IfLogFile.WriteLine(FTPUpload)LogFile.WriteBlankLines 1LogFile.CloseSet oFTPScriptFSO = NothingSet oFTPScriptShell = NothingSet LogFile = NothingEnd Function
Dim dtTemp, szMonth, szDayName, szDay, szYear, inFilename, outFilename, szPath, szDate
dtTemp = Date
'Get the 2 digit month. VBScript does not include a function for this
if (len(CStr(Month(dtTemp))) < 2) then
szMonth = "0" & CStr(Month(dtTemp))
else
szMonth = CStr(Month(dtTemp))
end if
if (len(CStr(Day(dtTemp))) < 2) then
szDay = "0" & CStr(Day(dtTemp))
else
szDay = CStr(Day(dtTemp))
end if
szYear = Year(dtTemp)
szPath = "D:\FILES"
szDate = szYear & szMonth & szDay
inFilename = szPath & "\" & szYear & "\" & szMonth
outFilename = szPath & "\Archive\COMPRESSED" & ".ZIP"
Wscript.Echo WindowsZip(inFilename, outFilename, szPath, szDate)
Wscript.Echo FTPUpload("xx.xx.xx.xx", "userid", "password", outFilename, "REMOTEPATH", szPath, szDate, szYear, szMonth)
Save the above script as compress_ftp.vbs, and run it on task scheduler with cscript. For the total file size around 20GB, this process will take around 1 hour.
Comments
Post a Comment