TheLastGimbus / GooglePhotosTakeoutHelper

Script that organizes the Google Takeout archive into one big chronological folder
https://aur.archlinux.org/packages/gpth-bin
Apache License 2.0
3.48k stars 176 forks source link

PathNotFoundException caused by leading or trailing spaces in file/directory names #212

Closed a-chen closed 9 months ago

a-chen commented 1 year ago

Hi, ran into an issue caused by a trailing space somehow showing up at the end of one of my directories in the example below. The exception caused the program to exit.

PathNotFoundException: Cannot rename file to 'output\2021.06.20 wedding shower \2021-06-20 14_42_48-Zoom Meeting.png', path = 'input\Takeout\Google Photos\2021.06.20 wedding shower \2021-06-20 14_42_48-Zoom Meeting.png' (OS Error: The system cannot find the path specified.
, errno = 3)
#0      _checkForErrorResponse (dart:io/common.dart:42)
#1      _File.rename.<anonymous closure> (dart:io/file_impl.dart:318)
<asynchronous suspension>
#2      moveFiles.moveFile (package:gpth/moving.dart:132)
<asynchronous suspension>
#3      moveFiles (package:gpth/moving.dart:153)
<asynchronous suspension>
#4      main.<anonymous closure> (file:///d:/a/googlephotostakeouthelper/googlephotostakeouthelper/bin/gpth.dart:365)
<asynchronous suspension>

Being lazy, I asked ChatGPT where to make the change in moving.dart

"I encountered an error that seems to be caused by a space at the end of the folder name in the below error. I think the solution would be to strip leading and trailing empty characters of file and folder names. Where should this change be made?"

and it recommended the following changes. Looked mostly correct to me, so am reporting this issue. Hope this helps! Greatly appreciate your work

ChatGPT response

The change should be made where the file and folder paths are being constructed, specifically where the folder's name is defined and where the file is being moved or copied to.

Here is where you can make the changes:

Line 83-90: This is where the folder path is constructed. You can use Dart's trim method to remove leading and trailing white spaces from the file and folder names. Replace the existing lines with the following code:

final folder = Directory(
  p.join(
    output.path,
    (file.key ?? 'ALL_PHOTOS').trim(), // album or all
    divideToDates
        ? date == null
            ? 'date-unknown'.trim()
            : p.join(
                '${date.year}'.trim(),
                date.month.toString().padLeft(2, '0').trim(),
              )
        : '',
  ),
);

Line 107-110: This is where the file is being moved or copied. Again, you should trim the filename here:

final freeFile = findNotExistingName(
    File(p.join(folder.path, p.basename(file.value.path).trim())));

After these changes, your program should work correctly with folder or file names that have leading or trailing spaces.

a-chen commented 1 year ago

Seems to be a Windows thing where Windows recognizes "someDirectory " to be the exact same as "someDirectory" https://stackoverflow.com/questions/74936903/why-do-some-os-functions-ignore-trailing-spaces-on-windows

TheLastGimbus commented 1 year ago

Oh dear jezus ;_;

i will try to handle this in some time - maybe a week or smth

big thanks for figuring this out by the way...

TheLastGimbus commented 1 year ago

Hi man! Looked into this finally, and here is my try: https://github.com/TheLastGimbus/GooglePhotosTakeoutHelper/commit/7e7216d9ef7723bc31b237c1c3b3f0b72e75f77b

here is the nightly exe: https://github.com/TheLastGimbus/GooglePhotosTakeoutHelper/actions/runs/5271626192

let me know if it works now :+1: