Skip to content

Instantly share code, notes, and snippets.

@johnsonjo4531
Last active September 11, 2019 02:49
Show Gist options
  • Save johnsonjo4531/3a18bc4539be33a2cfd965f93e12cdc6 to your computer and use it in GitHub Desktop.
Save johnsonjo4531/3a18bc4539be33a2cfd965f93e12cdc6 to your computer and use it in GitHub Desktop.
Prefix and givenfiles in a certain directory

Prefix and Unprefix files scripts

Tested with Deno v0.15.0

Prefixes all files with given FILE_EXTENSION in DIRECTORY with the FILE_PREFIX. If the prefix already exists on the file the prefix is skipped.

To install:

Then to install prefix_file run:

deno_installer prefix_file https://gist.githubusercontent.com/johnsonjo4531/3a18bc4539be33a2cfd965f93e12cdc6/raw/2c515c413940b93df599c5e2646136a13e4f9f4c/prefix_file.ts -A

Then to install unprefix_file run:

deno_installer unprefix_file https://gist.githubusercontent.com/johnsonjo4531/3a18bc4539be33a2cfd965f93e12cdc6/raw/2c515c413940b93df599c5e2646136a13e4f9f4c/unprefix_file.ts -A

Running:

Running with permission warnings

Think of this as an interactive mode. This may be helpful if you want to see every file the script is reading and writing. The read access of a directory is so that the program can know the filenames in the directories (and therefore it may attempt to rewrite). The read and write access of the file means the program will attempt to rename that file.

prefix_file DIRECTORY FILE_PREFIX FILE_EXTENSION

or:

unprefix_file DIRECTORY FILE_PREFIX FILE_EXTENSION

Running without permissions warning:

This will be helpful if you want to ignore all permissions warnings. Notice the addition of the -A flag this tells Deno (the typescript runtime) to allow all possible permissions:

prefix_file -A DIRECTORY FILE_PREFIX FILE_EXTENSION

or:

unprefix_file -A DIRECTORY FILE_PREFIX FILE_EXTENSION

License

(All files in this Gist are MIT LICENSED)

Copyright 2019 johnsonjo4531
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import * as path from "https://deno.land/std@v0.12.0/fs/path.ts";
import { parse } from "https://deno.land/std@v0.12.0/flags/mod.ts";
(async () => {
const parsedArgs = parse(Deno.args);
const FILE_PREFIX = parsedArgs._[2];
const DIRECTORY = parsedArgs._[1];
const FILE_EXTENSION = parsedArgs._[3];
if (
parsedArgs.h ||
parsedArgs.help ||
!FILE_PREFIX ||
!DIRECTORY ||
!FILE_EXTENSION
) {
console.error("Invalid syntax try again! Refer to usage below.\n\n");
console.log(`USAGE: deno prefix_file.ts DIRECTORY FILE_PREFIX FILE_EXTENSION
Prefixes all files with given FILE_EXTENSION in DIRECTORY with the FILE_PREFIX. If the
prefix already exists on the file the prefix is skipped.
`);
return;
}
if (!FILE_EXTENSION.startsWith(".")) {
throw new Error("FILE_EXTENSION must start with a dot `.`");
return;
}
const promises = [];
for (const file of await Deno.readDir(DIRECTORY)) {
if (path.extname(file.name) === FILE_EXTENSION) {
if (file.name.startsWith(FILE_PREFIX)) {
continue;
} else {
promises.push(
Deno.rename(
path.resolve(DIRECTORY, file.name),
path.resolve(DIRECTORY, `${FILE_PREFIX}${file.name}`)
)
);
}
}
}
await Promise.all(promises);
console.log("completed successfully");
})();;
import * as path from "https://deno.land/std@v0.12.0/fs/path.ts";
import { parse } from "https://deno.land/std@v0.12.0/flags/mod.ts";
(async () => {
const parsedArgs = parse(Deno.args);
const FILE_PREFIX = parsedArgs._[2];
const DIRECTORY = parsedArgs._[1];
const FILE_EXTENSION = parsedArgs._[3];
if (
parsedArgs.h ||
parsedArgs.help ||
!FILE_PREFIX ||
!DIRECTORY ||
!FILE_EXTENSION
) {
console.error("Invalid syntax try again! Refer to usage below.\n\n");
console.log(`USAGE: deno unprefix_file.ts DIRECTORY FILE_PREFIX FILE_EXTENSION
Prefixes all files with given FILE_EXTENSION in DIRECTORY with the FILE_PREFIX. If the
prefix already exists on the file the prefix is skipped.
`);
return;
}
if (!FILE_EXTENSION.startsWith(".")) {
throw new Error("FILE_EXTENSION must start with a dot `.`");
return;
}
const promises = [];
for (const file of await Deno.readDir(DIRECTORY)) {
if (path.extname(file.name) === FILE_EXTENSION) {
if (file.name.startsWith(FILE_PREFIX)) {
promises.push(
Deno.rename(
path.resolve(DIRECTORY, file.name),
path.resolve(DIRECTORY, file.name.replace(FILE_PREFIX, ""))
)
);
}
}
}
await Promise.all(promises);
console.log("completed successfully");
})();;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment