-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLog.vbs
More file actions
81 lines (66 loc) · 2.54 KB
/
Copy pathFileLog.vbs
File metadata and controls
81 lines (66 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Option Explicit
'--------------------------------------------------------------------------------
' ScriptName : FileLog.vbs (was the extensionless note file "log write")
' Creator : Sungman Han
' Creation Date : 2019-09-16
' Description : Text file read / write helpers for the logging these SAP scripts
' do while running unattended.
'
' Usage : AppendLine "C:\log\aae.log", "started"
' vText = ReadAllText("C:\log\aae.log")
' vLines = ReadLines("C:\log\aae.log") ' array, empty file -> Array()
'
' Note : the original file was a pasted note whose quotes were smart quotes
' (U+201C / U+201D), so it could not run at all. It is real code now.
'
' OpenTextFile(path, iomode, create, format)
' iomode 1 = read, 2 = write (truncate), 8 = append
' create True = create when missing
' format -2 = system default, -1 = Unicode (UTF-16LE), 0 = ASCII
'--------------------------------------------------------------------------------
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
Const FMT_SYSTEM_DEFAULT = -2
Const FMT_UNICODE = -1
Const FMT_ASCII = 0
Sub AppendLine(vPath, vText)
Dim vStream
Set vStream = CreateObject("Scripting.FileSystemObject") _
.OpenTextFile(vPath, FOR_APPENDING, True, FMT_SYSTEM_DEFAULT)
vStream.WriteLine vText
vStream.Close
Set vStream = Nothing
End Sub
Sub WriteAllText(vPath, vText)
Dim vStream
Set vStream = CreateObject("Scripting.FileSystemObject") _
.OpenTextFile(vPath, FOR_WRITING, True, FMT_SYSTEM_DEFAULT)
vStream.Write vText
vStream.Close
Set vStream = Nothing
End Sub
Function ReadAllText(vPath)
Dim vStream
ReadAllText = ""
Set vStream = CreateObject("Scripting.FileSystemObject") _
.OpenTextFile(vPath, FOR_READING, False, FMT_SYSTEM_DEFAULT)
' ReadAll raises on an empty file, so the guard is required
If Not vStream.AtEndOfStream Then ReadAllText = vStream.ReadAll()
vStream.Close
Set vStream = Nothing
End Function
' Returns an array of lines. Splitting the whole text is both shorter and safer
' than ReDim Preserve in a ReadLine loop, which VBScript rejects on the array
' returned by Array().
Function ReadLines(vPath)
Dim vText
vText = ReadAllText(vPath)
If vText = "" Then
ReadLines = Array()
Exit Function
End If
vText = Replace(Replace(vText, vbCrLf, vbLf), vbCr, vbLf)
If Right(vText, 1) = vbLf Then vText = Left(vText, Len(vText) - 1)
ReadLines = Split(vText, vbLf)
End Function