I created a folder through the mac finder and named it février (french keyboard). I run a node.js script to return all the folders and files that are in this one. To get the name of the folders/files from the finder I used:
const files = fs.readdirSync(dir);
files.forEach((file) => {
const path = `${dir}/${file}`;
const stat = fs.statSync(path);
if (stat && stat.isDirectory()) {
results = results.concat(readLocalDir(path));
} else {
results.push(path);
}
});
It gives me an array with the full path ['février/image.png', ...].
Then I check with the dropbox api, if this file is already uploaded to my dropbox folder. I use the full path to check it with this:
return dropbox
.filesListFolder({ path: `/${dir}`, recursive: true })
.then(res => getFilesListContinue(res))
...
async function getFilesListContinue(res) {
if (res.has_more) {
const files = await dropbox.filesListFolderContinue({ cursor: res.cursor });
return getFilesListContinue({
has_more: files.has_more,
entries: flatten([res.entries, files.entries]),
cursor: files.cursor,
});
}
return res;
}
Even though when I'm doing this, it returns that the file is not there, however, the folder and file are there, but the folder name has a different encoding.
I then checked the encoding of each folder name (the one on my finder, and the one on my dropbox). I used detect-character-encoding and this is what I got:
finder: février { encoding: 'ascii' }
dropbox: février { encoding: 'windows-1252' }
If you try to select the two février word through an editor, you will see you can't select the same occurrence as the first one, because they are not the same février =/= février.
Is there any simple way to convert my local ASCII(?) string to a windows-1252 encoded string to be able to compare them against?
As proposed in the comments, the solution is more simple that the problem can be complex. I just had to normalize the path return by readdirSync function
const files = fs.readdirSync(dir);
files.forEach((file) => {
const path = `${dir}/${file.normalize('NFC')}`;
...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With