Skip to content

Instantly share code, notes, and snippets.

Created November 11, 2020 17:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erisa/b8ad165162862a0ea84768f9db5c4a5c to your computer and use it in GitHub Desktop.
Save Erisa/b8ad165162862a0ea84768f9db5c4a5c to your computer and use it in GitHub Desktop.
Hard Link Generator
#!/bin/env -S crystal build
# Hard Link Generator
# Generates hard links for all files in a given folder,
# while also finding and replacing strings in the filenames.
# The folder is created for you, but existing files (Running it twice?) will cause errors.
# Usage:
# Hard Link Generator - (c) Erisa A 2020.
# -h, --help Show help
# -s SOURCE_DIR, --src SOURCE_DIR The directory containing source files.
# -d DEST_DIR, --dest DEST_DIR The directory where output hard links should be dropped.
# -f SEARCHTEXT, --find SEARCHTEXT Text to match in source filenames.
# -r REPLACETEXT, --replace REPLACETEXT Text to replace in source filenames
# Example:
# $ ./hlg --src 'test series' --dest 'emby/test' --find 'test pattern ' --replace 'test S0E'
# [debug] files found: ["test pattern 01.mkv", "test pattern 02.mkv", "test pattern 03.mkv", "test pattern 04.mkv", "test pattern 05.mkv", "test pattern 06.mkv", "test pattern 07.mkv", "test pattern 09.mkv"]
# 'test series/test pattern 01.mkv' => 'emby/test/test S0E01.mkv'
# 'test series/test pattern 02.mkv' => 'emby/test/test S0E02.mkv'
# 'test series/test pattern 03.mkv' => 'emby/test/test S0E03.mkv'
# 'test series/test pattern 04.mkv' => 'emby/test/test S0E04.mkv'
# 'test series/test pattern 05.mkv' => 'emby/test/test S0E05.mkv'
# 'test series/test pattern 06.mkv' => 'emby/test/test S0E06.mkv'
# 'test series/test pattern 07.mkv' => 'emby/test/test S0E07.mkv'
# 'test series/test pattern 09.mkv' => 'emby/test/test S0E09.mkv'
require "file_utils"
require "option_parser"
dest_folder = ""
src_folder = ""
find = ""
replace = ""
option_parser = OptionParser.parse do |parser|
parser.banner = "Hard Link Generator - (c) Erisa A 2020."
parser.on "-h", "--help", "Show help" do
puts parser
parser.on "-s SOURCE_DIR", "--src SOURCE_DIR", "The directory containing source files." do |src_input|
src_folder = src_input
parser.on "-d DEST_DIR", "--dest DEST_DIR", "The directory where output hard links should be dropped." do |dest_input|
dest_folder = dest_input
parser.on "-f SEARCHTEXT", "--find SEARCHTEXT", "Text to match in source filenames." do |find_input|
find = find_input
parser.on "-r REPLACETEXT", "--replace REPLACETEXT", "Text to replace in source filenames." do |replace_input|
replace = replace_input
if dest_folder == "" || src_folder == ""
puts "I need at least input and output folders."
puts option_parser
dir =
files = [] of String
while file =
files << file unless file == "." || file == ".."
files = files.sort
puts "[debug] files found: #{files}"
files.each {|x|
new_x = x.gsub(find, replace)
puts "'#{src_folder}/#{x}' => '#{dest_folder}/#{new_x}'"
FileUtils.ln("#{src_folder}/#{x}", "#{dest_folder}/#{new_x}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment