Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename already downloaded files to "path-restrict": "ascii" and continue downloading where it left off? #6985

Closed
barkoder opened this issue Feb 13, 2025 · 8 comments

Comments

@barkoder
Copy link

barkoder commented Feb 13, 2025

I downloaded a lot but only discovered the "path-restrict": "ascii" option much later.

Is there a way to tell gallery-dl to rename all files in a given directory to "path-restrict": "ascii" (including .part files) and then continue the download where it left off?

Thanks!

@mikf
Copy link
Owner

mikf commented Feb 13, 2025

While there is a --rename option, it does not allow to switch between different path-restrict settings.

I think you'd be best off using a third-party tool to rename your files by replacing any characters not in 0-9A-Za-z_. with underscores _.

@barkoder
Copy link
Author

I used this in bash.

for file in * ; do mv "$file" $(echo "$file" | sed -e 's/[^A-Za-z0-9.]/_/g') ; done

But there are 3 more underscores in the filename than what you get with gallery-dl's "path-restrict": "ascii"

It's the emoji. They're being substituted with 4 underscores, instead of just 1 underscore with gallery-dl's "path-restrict": "ascii"

Please help me remove those 3 additional underscores . Thanks.

@mikf
Copy link
Owner

mikf commented Feb 15, 2025

Replacing

echo "$file" | sed -e 's/[^A-Za-z0-9.]/_/g'

with

python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' "$file"

to let Python do the replacement should help.

edit: You might want to quote the $(...) part:

for file in * ; do mv -- "$file" "$(python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' "$file")" ; done

@barkoder
Copy link
Author

barkoder commented Feb 15, 2025

Create this file in test directory - 💞.txt

Using your command,

for file in * ; do mv -- "$file" "$(python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' "$file")" ; done

New filename - [0m[0K_.txt[0K

If I just do

$ python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' "$file"
_.txt

It looks like _.txt but there's more being printed.
Because when I wc -c that,

$ python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' "$file" | wc -c
17

17 characters.

But _.txt is only supposed to be 5 characters.

$ echo -n _.txt | wc -c
5

@mikf
Copy link
Owner

mikf commented Feb 15, 2025

Works in my machine ™️

$ touch '💞.txt'
$ ls
💞.txt
$ for file in * ; do mv -- "$file" "$(python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' "$file")" ; done
$ ls
_.txt
$ ls | wc -c
6  # 5 characters + newline
$ python -c 'import sys, re; print(re.sub(r"[^0-9A-Za-z_.]", "_", sys.argv[1]), end="")' '💞.txt' | wc -c
5

@mikf
Copy link
Owner

mikf commented Feb 15, 2025

[0m

This looks like the start of an ANSI escape sequence, but I might be wrong.

@mikf mikf added the os:unix label Feb 15, 2025
@barkoder
Copy link
Author

Thanks @mikf it works! Had to update bash.

@mikf mikf closed this as completed Feb 17, 2025
@Hrxn
Copy link
Contributor

Hrxn commented Feb 18, 2025

[0m

This looks like the start of an ANSI escape sequence, but I might be wrong.

Yup, ANSI escape sequence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants