Folder Operations — k.file
Directory management and folder listing on
k.file
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
Server-side KScript (k.api, <script env="server">, scheduled jobs) | None (operates directly on site disk directories; use k.site.files for the CMS File Library) | Create, check, rename, and delete disk folders and list FileInfo / FolderInfo entries |
Quick Start
Create a directory, check existence, list subfolders, and filter files inside it:
js
k.api.post(() => {
k.file.createFolder("demo-dir")
k.file.write("demo-dir/readme.txt", "hello")
const exists = k.file.folderExists("demo-dir")
const files = k.file.folderFiles("demo-dir", { searchPattern: "*.txt" })
const folders = k.file.subFolders()
return { exists, fileCount: files.length, folderCount: folders.length }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
folderFiles(folder, options?) | Lists files in a folder with optional recursion and wildcard filtering | FileInfo[] |
subFolders(folder?) | Lists subfolders under a folder or the disk root | FolderInfo[] |
folderExists(path) | Checks whether a disk folder exists | boolean |
createFolder(folder, parentFolder?) | Creates a folder under root or parentFolder | void |
renameFolder(oldFolder, newFolder) | Renames a disk folder (throws if source is missing or target exists) | void |
deleteFolder(folder) | Deletes a folder and all nested files and subfolders | void |
getAllFiles(includeAuthor?) | Lists all files across the site disk directory | FileInfo[] |
Methods & Properties
folderFiles()
Gets all files in the specified folder, with optional recursive directory traversal and filename wildcard filtering via options.
| Parameter | Type | Required | Description |
|---|---|---|---|
folder | string | Yes | Folder path |
options | SearchFolderOptions | No | Search options |
Returns: FileInfo[]
js
k.api.get("folderFiles", () => {
const files = k.file.folderFiles("test-folder")
return { count: files.length }
})subFolders()
Gets all subfolders under the specified folder.
| Parameter | Type | Required | Description |
|---|---|---|---|
folder | string | No | Folder path; defaults to the root directory |
Returns: FolderInfo[]
js
k.api.get("subFolders", () => {
const folders = k.file.subFolders("test-folder")
return { count: folders.length }
})folderExists()
Checks whether a folder exists.
| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | Folder path |
Returns: boolean
js
k.api.get("folderExists", () => {
return k.file.folderExists("test-folder")
})createFolder()
Creates a folder.
| Parameter | Type | Required | Description |
|---|---|---|---|
folder | string | Yes | Folder name |
parentFolder | string | No | Parent folder path |
Returns: void
js
k.api.post("createFolder", () => {
k.file.createFolder("new-folder")
const folders = k.file.subFolders()
const exists = folders.some((f) => f.name === "new-folder")
return { created: exists }
})renameFolder()
Renames a folder. Throws an exception if oldFolder does not exist ('{oldFolder}' folder not exist) or if newFolder already exists ('{newFolder}' folder exist).
| Parameter | Type | Required | Description |
|---|---|---|---|
oldFolder | string | Yes | Original folder path |
newFolder | string | Yes | New folder path |
Returns: void
js
k.api.post("renameFolder", () => {
if (k.file.folderExists("new-folder")) {
k.file.deleteFolder("new-folder")
}
k.file.createFolder("old-folder")
k.file.renameFolder("old-folder", "new-folder")
const folders = k.file.subFolders()
const newExists = folders.some((f) => f.name === "new-folder")
const oldExists = folders.some((f) => f.name === "old-folder")
return { newExists: newExists, oldExists: oldExists }
})deleteFolder()
Deletes a folder and all of its files and subfolders.
| Parameter | Type | Required | Description |
|---|---|---|---|
folder | string | Yes | Folder path |
Returns: void
js
k.api.post("deleteFolder", () => {
k.file.createFolder("folder-to-delete")
k.file.deleteFolder("folder-to-delete")
const exists = k.file.folderExists("folder-to-delete")
return { deleted: !exists }
})getAllFiles()
Gets all files under the site disk directory.
| Parameter | Type | Required | Description |
|---|---|---|---|
includeAuthor | boolean | No | Whether to include author information; defaults to false |
Returns: FileInfo[]
js
k.api.get("getAllFiles", () => {
const files = k.file.getAllFiles()
return { count: files.length }
})TypeScript Definitions
ts
interface FileIO {
folderExists(path: string): boolean;
getAllFiles(): FileInfo[];
getAllFiles(IncludeAuthor: boolean): FileInfo[];
folderFiles(folder: string, options?: SearchFolderOptions): FileInfo[];
subFolders(folder?: string): FolderInfo[];
createFolder(Folder: string, ParentFolder: string): void;
createFolder(Folder: string): void;
renameFolder(oldFolder: string, newFolder: string): void;
deleteFolder(Folder: string): void;
}Related Docs
- k.file — Core file operations
- FileInfo / SearchFolderOptions — k.file — File, folder, and search data structures
- k.file.resumableUpload — Resumable upload
- k.site — Site management