OT: when you can not detete that long file from the command line
From
T@21:1/5 to
All on Sun Apr 21 20:13:52 2024
Hi All,
When you are trying to delete a a file
that has a very long file & path name ,
from the cmd line, powershell, your unlink
command within your compiler, and it just
wont delete. And you get the following
the returned error message of
"File not found"
And no you did not misspell it it.
But you can delete it with Windows
Explorer, here is what is going on.
Windows has two maximum file & path names
character lengths.
The default is called "MAX_PATH" and it is
260 characters long. Del and friends
makes an API (Application Program Interface)
call to DeleteFileA (UTF8) or DeleteFileW
(UTF16). The call lops off the extra
characters above MAX_PATH, so you
are trying to delete a different file name,
which is why the File Not Found error.
The solution is to use the other maximum,
non-default path limit of 32,767 characters.
To do this, prepend \\?\ to your path/file name.
Windows Explorer does this automatically
for you which is why it works. You can also
prepend this to shorter files without issue
(it is probably shower though.)
Some examples from the Raku (Perl6) module
I wrote to handle this. It returns OK if
the file got deleted or the error message
if not:
The Q[...] means a direct quote.("" does not work
so when used from the command line with Raku.)
Current directory path:
echo abc > eraseme.txt && raku -I. -e "use NativeDelete
:ApiDeleteFile; say ApiDeleteFile( Q[eraseme.txt] )" && type eraseme.txt
OK
The system cannot find the file specified.
Looking for the wrong file:
echo abc > eraseme.txt && raku -I. -e "use NativeDelete
:ApiDeleteFile; say ApiDeleteFile( Q[eraseme2.txt] )" && type eraseme.txt
The system cannot find the file specified.
abc
Long path from hell:
raku -I. -e "use NativeDelete :ApiDeleteFile; say ApiDeleteFile( Q[\\?\D:\MyDocsBackup\backup2\Mozilla 2024-04-15 21;14;31 (Full)\Firefox\Profiles\pj0elosu.default-release\storage\default\https+++505991220932649.webpush.freshchat.com^partitionKey=%28https%2Cbid13.com%29\cache\morgue\114\{7cccd5e9-a7aa-4349-a2e9-569baf007272}.final]
);"
OK
HTH,
-T
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)