RAS standard library
Files and folders (fs)
The RAS standard library module for working with files and folders. It creates one Scripting.FileSystemObject and reuses it, and uses ADODB.Stream for reading and writing with a specified encoding. It is emitted into the VBA module RAS_Fs.
import * as fs from "fs";
export function convertFiles(dir: string) {
for (const p of fs.walk(dir, "*.txt")) { // searches subfolders too
const text = fs.readText(p); // detected in the order BOM → UTF-8 → Shift_JIS
fs.writeText(p + ".sjis", text, "shift_jis");
}
fs.writeText(dir + "\\log.txt", "Done\r\n", append: true); // append to the end
for (const line of fs.readLines(dir + "\\list.txt", "utf8")) {
debug.print(line);
}
}
Encoding names
Case, _ and - are ignored.
| Name | Aliases | When reading | When writing |
|---|---|---|---|
"auto" |
"" |
Detected from the BOM and whether the data is valid UTF-8; if neither, Shift_JIS | Same as "utf8" |
"utf8" |
"utf-8" |
The BOM may or may not be present | No BOM |
"utf8bom" |
"utf-8-bom", "utf-8-sig", etc. |
Same as "utf8" |
With BOM |
"cp932" |
"shift_jis", "sjis", "windows-31j", "ms932", etc. |
Shift_JIS (CP932, including Windows extension characters) | Same |
"eucjp" |
"euc-jp" |
EUC-JP | Same |
"iso2022jp" |
"iso-2022-jp", "jis" |
JIS (ISO-2022-JP) | Same |
"utf16le" |
"utf-16", "unicode" |
UTF-16 LE (the BOM may or may not be present) | With BOM |
"utf16be" |
"utf-16be", "unicodeFFFE" |
UTF-16 BE (same as above) | With BOM |
Any other name is passed as is to the charset of ADODB.Stream ("big5", "iso-8859-1", etc.).
For reading and writing with VBA's Open / Print # / Close (ANSI and UTF-16 only), see the cheat sheet.
Reference
Function fs.exists
function exists(p: string): boolean
Whether a file or folder exists.
Example: if (!fs.exists(path)) throw new Error("Not found: " + path);
Function fs.isFile
function isFile(p: string): boolean
Whether a file exists (false for a folder).
Function fs.isDirectory
function isDirectory(p: string): boolean
Whether a folder exists (false for a file).
Function fs.list
function list(dir: string, hidden: boolean = false): string[]
Paths of the folders and files directly in the folder (folders first). By default hidden files, system files, names starting with . and lock files starting with ~$ are left out (hidden: true includes them).
An empty array when the folder does not exist.
Example: fs.list("C:\\data").filter(p => p.endsWith(".csv"))
Function fs.walk
function walk(dir: string, pattern: any = "*", directories: boolean = false, files: boolean = true, hidden: boolean = false): string[]
Paths of the files in the folder (including subfolders) whose names match pattern.
pattern is a wildcard ("*.xlsx", case-insensitive) or a regular expression (/^sales_\d+\.csv$/i).
directories: true also returns folders, and files: false returns no files. By default hidden files, system files, names starting with . and lock files starting with ~$ are left out,
and hidden folders are not searched (hidden: true includes them). An empty array when the folder does not exist.
Example: fs.walk("C:\\data", "*.csv"), fs.walk(dir, /^\d{4}$/, directories: true, files: false)
Function fs.encodingName
function encodingName(name: string): string
Normalizes an encoding name: "auto", "utf8", "utf8bom", "cp932" (Shift_JIS), "eucjp", "iso2022jp", "utf16le", "utf16be".
Other names are returned in lowercase as they are (passed to the charset of ADODB.Stream).
Example: fs.encodingName("Shift_JIS") → "cp932", fs.encodingName("UTF-8") → "utf8"
Function fs.detectEncoding
function detectEncoding(p: string): string
Guesses the encoding of a file: "utf8bom" / "utf16le" / "utf16be" if there is a BOM, otherwise "utf8" if it is valid UTF-8 (including ASCII-only and empty files),
otherwise "cp932" (Shift_JIS).
Example: if (fs.detectEncoding(path) == "cp932") { … }
Function fs.readText
function readText(p: string, encoding: string = "auto"): string
Reads a text file. encoding is "auto" (default; decided by the BOM and by whether the bytes are valid UTF-8, falling back to Shift_JIS),
"utf8" (with or without a BOM), "cp932" (also "shift_jis", "sjis"), "eucjp", "iso2022jp", "utf16le", "utf16be" and so on.
A leading BOM is removed. Line breaks are kept as in the file (use readLines to split into lines). Error 53 if the file does not exist.
Example: const text = fs.readText("C:\\data\\sales.csv");, fs.readText(path, "shift_jis")
Function fs.writeText
function writeText(p: string, text: string, encoding: string = "utf8", append: boolean = false): void
Writes a text file (the file is recreated; with append: true the text is appended). Line breaks are written as in text.
encoding is "utf8" (default; no BOM), "utf8bom" (with a BOM; for CSV files opened in Excel), "cp932" (also "shift_jis", "sjis"), "eucjp", "iso2022jp",
"utf16le" / "utf16be" (with a BOM) and so on. No BOM is written when appending. Characters that cannot be represented become ?.
Example: fs.writeText("out.txt", text), fs.writeText(path, line + "\r\n", "shift_jis", append: true)
Function fs.readLines
function readLines(p: string, encoding: string = "auto"): string[]
Reads a text file into an array of lines (line breaks can be CR LF, LF or CR and are not included; there is no empty line after the last line break).
encoding is as in readText (default "auto").
Example: for (const line of fs.readLines(path)) { … }
Function fs.writeLines
function writeLines(p: string, lines: string[], encoding: string = "utf8", newline: string = "\r\n", append: boolean = false): void
Writes an array of lines to a text file (each line is followed by newline, default CR LF). encoding and append are as in writeText (default "utf8").