Skip to content

Instantly share code, notes, and snippets.

@ithinkandicode
Last active August 9, 2020 15:41
Show Gist options
  • Save ithinkandicode/9f3326b99ef6b5247dad96d6b79f16e7 to your computer and use it in GitHub Desktop.
Save ithinkandicode/9f3326b99ef6b5247dad96d6b79f16e7 to your computer and use it in GitHub Desktop.
Humble Bundle Renamer - jQuery
/**
* Humble Bundle Renamer
*
* Renames files downloaded from a Humble Bundle downloads page:
*
* - Run the script in the developer tool's JS console
* - Copy the generated text, which appears in a box on the page
* - Paste this into a text file, within the same folder as your downloaded files
* - Save the file, then rename it to "renamer.bat"
* - Run the batch script
*
* Why this is useful:
*
* Files are saved by their server names (eg, "an_interesting_book.pdf"),
* not their human-readable names ("An Interesting Book.pdf").
* This script gives you the contents of a batch file which renames these
* downloaded files to their human-readable equivalents.
*
* Original Reddit post:
* https://www.reddit.com/r/humblebundles/comments/7nepm5/bundle_file_renamer_judge_dredd_comics/
*/
// Create an array to store our batch file lines
var output = [];
// Build the array
$('.js-all-downloads-holder .whitebox-redux .row').each(function(){
// Get name
var name = $(this).find('.gameinfo .title a:first-child').text().trim();
// Replace illegal characters (you can change the "" in the second parameter to whatever you like, as with the first line)
name = name.replace(":", " -");
name = name.replace("?", "");
name = name.replace("*", "");
name = name.replace("|", "");
// Get filenames & add them to the output array
$(this).find('.downloads .download .a').each(function() {
// Get single link URL
var url = $(this).attr('href');
// Remove the query string from the URL
var urlQueryIndex = url.indexOf('?');
var urlSliced = url.slice(0, urlQueryIndex);
// Remove the domain path
var filename = urlSliced.replace("https://dl.humble.com/", "");
// Get the file extension (".pdf", etc)
var extensionIndex = urlSliced.lastIndexOf('.');
var extension = url.slice(extensionIndex, urlSliced.length);
// Batch file renaming convention is "rename [old filename] [new filename]"
var oldFilename = filename;
var newFilename = name + extension;
var batLine = 'rename ' + oldFilename + ' "' + newFilename + '"';
output.push(batLine);
});
});
// Create an empty textarea, and usage instructions
$('.js-all-downloads-holder .whitebox-redux').prepend('<div style="margin-bottom: 20px;font-size: 18px;line-height: 25px;"><strong>Batch Renamer Instructions:</strong><ol><li>Copy and paste the text below into an empty text file named "renamer.txt"</li><li>Rename this file to "renamer.bat" (without quotes)</li><li>Put the file into a folder with all of your downloaded files (be sure to make backups first, just in case!)</li><li>Double-click the .bat file, and your files will be renamed!</li></ol></div><textarea class="batch-file-output" style="width: 100%; min-height: 500px;margin-bottom: 20px;"></textarea>');
// Output the array to the textarea
$.each(output, function(index, value) {
console.warn(value);
$('.batch-file-output').append(value + "\n");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment