(ファイルが見つかりませんと言われることがある。ディレイが必要かもしれない)
Last active
October 22, 2021 07:19
-
-
Save Getaji/695b9964d557dbc1d7274c599527e112 to your computer and use it in GitHub Desktop.
Denoでディレクトリを監視して新しいファイルがあったらリネームするスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // deno run --allow-read --allow-write watch-and-rename.ts | |
| import * as path from "https://deno.land/std/path/mod.ts"; | |
| const DIR = "ここに監視対象のディレクトリパスを指定"; | |
| // ファイル名を変換 | |
| function rename(fileName: string): string { | |
| return "renamed-" + fileName; | |
| } | |
| // パスを変換 | |
| function changePath(filePath: string): string { | |
| const fileDir = path.dirname(filePath); | |
| const fileName = path.basename(filePath); | |
| return path.join(fileDir, rename(fileName)); | |
| } | |
| for await (const fsEvent of Deno.watchFs(DIR)) { | |
| if (fsEvent.kind === "create") { | |
| fsEvent.paths.forEach((oldPath) => { | |
| const newPath = changePath(oldPath); | |
| if (oldPath !== newPath) { | |
| Deno.rename(oldPath, newPath); | |
| console.log("OLD:", oldPath); | |
| console.log("NEW:", newPath); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment