> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# ファイルの管理

> FilesModule を通じて、アプリケーション内のファイルを閲覧、読み取り、作成、移動、削除します。

このページのすべてのメソッドは `app.files` にあります。

```typescript theme={null}
const app = await api.applications.fetch("abc123def456abc123def456");
app.files; // FilesModule
```

## ディレクトリ内のファイル一覧取得

`app.files.list(path?)` はディレクトリのエントリを返します。パスのデフォルトは `"/"` です。

```typescript theme={null}
const entries = await app.files.list("/");
console.log(entries);
// [
//   { type: "file",      name: "index.js", size: 123, lastModified: 1717084800000 },
//   { type: "directory", name: "src",      size: 0,   lastModified: 1717084800000 },
// ]
```

## ファイルの読み取り

`app.files.read(path)` はファイルの内容を Node.js の `Buffer` として返します。ファイルが存在しない場合は `undefined` を返します。

```typescript theme={null}
const buffer = await app.files.read("/index.js");
console.log(buffer?.toString("utf8"));
```

## ファイルの作成または上書き

`app.files.create(file, fileName, path?)` は `path/fileName` にファイルを書き込みます。パスのデフォルトは `"/"` です。

| Parameter  | Type               | Description                    |
| ---------- | ------------------ | ------------------------------ |
| `file`     | `string \| Buffer` | ローカルファイルのパス、または生の `Buffer` の内容 |
| `fileName` | `string`           | 拡張子付きのファイル名（例：`"version.ts"`）  |
| `path`     | `string`           | アプリ内の絶対ディレクトリパス（デフォルト `"/"`）   |

<Tabs>
  <Tab title="Buffer から">
    ```typescript theme={null}
    const content = Buffer.from("export default 1\n");
    await app.files.create(content, "version.ts", "/src");
    ```
  </Tab>

  <Tab title="ローカルパスから">
    ```typescript theme={null}
    import { join } from "node:path";

    const localPath = join(__dirname, "version.ts");
    await app.files.create(localPath, "version.ts", "/src");
    ```
  </Tab>
</Tabs>

<Warning>
  シグネチャは v4 で変更されました。以前のバージョンは `create(content, fullPath)` を受け付けていましたが、v4 では `create(content, fileName, path)` が必要です。
</Warning>

## ファイルの移動または名前変更

`app.files.move(path, newPath)` はファイルまたはディレクトリを移動または名前変更します。

```typescript theme={null}
await app.files.move("/version.ts", "/src/version.ts");
```

## ファイルまたはディレクトリの削除

`app.files.delete(path)` はファイルまたはディレクトリ全体を削除します。

```typescript theme={null}
await app.files.delete("/src/version.ts");
```
