-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_rename.js
111 lines (92 loc) · 2.88 KB
/
batch_rename.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const pokemon = require('pokemon');
const fs = require('fs');
const path = require('path');
// Replace these with the appropriate folder names
const source = 'animated-by-name/';
const dest = 'animated-by-nationaldex/';
const capitalize = (string) => {
return string.charAt(0).toUpperCase() + string.substr(1).toLowerCase();
}
// For the 16 Pokemon that are nammed differently
const fixMapping = (originalName) => {
const mappingFix = {
'Nidoran-f': 'Nidoran♀',
'Nidoran': 'Nidoran♂',
'Farfetchd': 'Farfetch’d',
'Mrmime': 'Mr. Mime',
'Hooh': 'Ho-Oh',
'Mimejr': 'Mime Jr.',
'Porygon-z': 'Porygon-Z',
'Zekrom': 'Zekrom ',
'Typenull': 'Type: Null',
'Jangmoo': 'Jangmo-o',
'Hakamoo': 'Hakamo-o',
'Kommoo': 'Kommo-o',
'Tapukoko': 'Tapu Koko',
'Tapulele': 'Tapu Lele',
'Tapubulu': 'Tapu Bulu',
'Tapufini': 'Tapu Fini'
}
return mappingFix[originalName] ? mappingFix[originalName] : originalName;
}
// Rename the Pokemon Sprites from Name to National Dex Number
fs.readdir( source, ( err, files ) => {
if( err ) {
console.error( "Could not list the directory.", err );
process.exit( 1 );
}
files.forEach( ( file, index ) => {
let sourcePath = path.join( source, file );
fs.stat( sourcePath, ( error, stat ) => {
if ( error ) {
console.error( "Error stating file.", error );
return;
}
const fileExtension = path.extname(sourcePath);
const pokemonName = path.basename(sourcePath, fileExtension);
let pokemonNumber = 0;
try {
pokemonNumber = pokemon.getId(fixMapping(capitalize(pokemonName)));
} catch (err) {
// console.error(err);
} finally {
if (pokemonNumber === 0) {
console.log( "Skipping", pokemonName )
// Don't rename or move the Pokemon if no ID is found
} else {
let destPath = path.join( dest, pokemonNumber.toString() + fileExtension );
fs.rename( sourcePath, destPath, ( error ) => {
if ( error ) {
console.error( "File moving error.", error );
}
else {
console.log( "Moved file '%s' to '%s'.", sourcePath, destPath );
}
});
}
}
});
});
// Find missing Pokemon (Displays on second run of script after initial batch rename)
fs.readdir( dest, ( err, files ) => {
if( err ) {
console.error( "Could not list the directory.", err );
process.exit( 1 );
}
let allMovedFiles = [];
files.forEach( ( file, index ) => {
let finalDestPath = path.join( source, file );
const destExtension = path.extname(finalDestPath);
allMovedFiles.push(parseInt(path.basename(finalDestPath, destExtension)))
});
allMovedFiles.sort((a, b) => a - b)
let currentPokemon = 0;
for(let nationalDexNum = 1; nationalDexNum <= allMovedFiles.length; nationalDexNum++) {
if (allMovedFiles[currentPokemon] !== nationalDexNum) {
console.log("Missing", nationalDexNum);
currentPokemon--;
}
currentPokemon++;
}
});
});