1
votes

I have a batch script that does the following tasks.
1. Create a backup folder
2. Zip specific files (text files in .log format) and move zipped files to backup folder
3. Delete the original files after moving

To accomplish the 2nd task, I'm writing the commands into VBScript file from batch script like below and then executing the VBScript at the end of batch script.

echo Option Explicit >> zipIt.vbs
REM remaining commands
CScript zipIt.vbs

The batch script successfully performs all 3 tasks for a file of size 100 MB. But fails at VBScript command for file of size 1 GB. Following error is thrown at command prompt

C:\Users\Administrator\Desktop\zipIt.vbs(18, 1) Microsoft VBScript runtime error
: Permission denied

Below is the content of zipIt.vbs Written line numbers here for convenience.

1. Option Explicit 
2. Dim FileToZip, Result 
3. Dim oShell 
4. Dim file 
5. Dim oFileSys 
6. Dim winShell 
7. FileToZip = "C:\Program Files\logs\File_2013-04-29.log" 
8. Result = "C:\Program Files\logs\File_2013-04-29.log.zip" 
9. Set oShell = CreateObject("WScript.Shell") 
10. Set oFileSys = CreateObject("Scripting.FileSystemObject") 
11. Set file = oFileSys.CreateTextFile(Result, True) 
12. file.Write "PK" & Chr(5) & Chr(6) & String(18, 0) 
13. file.Close 
14. Set file = nothing 
15. set winShell = createObject("shell.application") 
16. winShell.namespace(Result).CopyHere FileToZip 
17. wScript.Sleep(5000) 
18. oFileSys.DeleteFile FileToZip

Getting the same error if VBScript is run alone. Is it because of large file size?

2
VBScript is just creating an empty zip file i.e. File_2013-04-29.log.zip But it fails to add the .log file and delete the original file. - Shrik

2 Answers

0
votes

This is most definitely a problem with memory, seeing as applications are capped at a maximum of 2GB, within windows at least, it is most likely an issue of the program trying to access memory that it was not allocated.

0
votes

Alternative solution

Split large log files in little chunks:

BreakFile = "C:\Users\Administrador\Desktop\Test.txt"
limit = 400000 ' Bytes

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Breakfile, 1)

FiletoSplit = objFSO.GetFileName(BreakFile)
FolderDest = Mid(objFSO.GetAbsolutePathName(BreakFile),1,Len(objFSO.GetAbsolutePathName(BreakFile))-(Len(FiletoSplit)))
FileSplitName = objFSO.GetBaseName(BreakFile)
dtmStart = Now()

Set objFile = objFSO.OpenTextFile(Breakfile, 1)
strContents = objFile.ReadAll
FileNum = 1
fname = FolderDest & FileSplitName & "Split " & FileNum & ".txt"
Set objFile1 = objFSO.OpenTextFile(fname, 2, True)
CountLines = 0
arrLines = Split(strContents, vbCrLf)
HeaderText = arrLines(0)

For i = 0 to ubound(arrlines)
    strLine = arrLines(i) & vbCrLf
    objFile1.Write strLine
    If (Countlines) < limit Then
        countlines = countlines + 1
    ElseIf Countlines >= limit Then
        objFile1.Close
        Countlines = 0
        FileNum = FileNum + 1
        fname = FolderDest & FileSplitName & "Split " & FileNum & ".txt"
        Set objFile1 = objFSO.OpenTextFile(fname, 2, True)
        objFile1.Write HeaderText & vbCrLf
    End If
Next

objFile.Close
dtmEnd = Now()

Then Zip it all together, and when extract the content, use a "Copy /B" batch command like this to join the chunks:

Copy /B "MyLog_Part*" "MyLog_Complete.log"