1. change all files names to lower case.
- Posted by sergelli Aug 29, 2014
- 1421 views
Friends, why the code below does not work?
The intention is to change all files names to lower case.
But nothing is renamed. How should I do this?
ff= dir(dd) if atom(ff) then ff= message_box("Folder " & dd &"\nNot found","","") return end if for i=3 to length(ff) do ll=dd&lower(ff[i][1]) mm=moveFile(dd&ff[i][1],ll) if mm=w32False then xx= message_box("The file \n" & dd & ff[i][1]&"\n has not been renamed to \n"&ll,"","") end if end for
Thanks in advance
2. Re: change all files names to lower case.
- Posted by PeteE Aug 29, 2014
- 1407 views
I'm not at a windows box at the moment, so this is just a guess:
MoveFile fails if the destination file already exists, which might be the what's happening since you're only changing the case of the filename and Windows is filename case-insensitive. Try using MoveFileEx with the flag MOVEFILE_REPLACE_EXISTING?
And if that doesn't work, try renaming it to temporary different name, then rename again to the lowercased name.
3. Re: change all files names to lower case.
- Posted by sergelli Aug 29, 2014
- 1434 views
I did not understand. Sorry...
MoveFileEx does not exist on or in Eu4 and Eu3
How to use this function?
4. Re: change all files names to lower case.
- Posted by evanmars Aug 29, 2014
- 1402 views
How about something like this?
include std/filesys.e include std/text.e if not rename_file(dd, lower(dd),1) then puts(1,"Couldn't rename "&dd&"\n") end if
5. Re: change all files names to lower case.
- Posted by sergelli Aug 29, 2014
- 1401 views
So, it worked.
I renamed twice.
The first time with a changed name
Later renamed to the correct name in lowercase
ff= dir(dd) if atom(ff) then ff= message_box("Folder " & dd &"\nNot found","","") return end if for i=3 to length(ff) do ll=dd&lower(ff[i][1]) mm=moveFile(dd&ff[i][1],ll & "1") -- I added something in the name, only to record with different name mm=moveFile(ll & "1",ll) -- Now, recorded with the correct name if mm=w32False then xx= message_box("The file \n" & dd & ff[i][1]&"\n has not been renamed to \n"&ll,"","") end if end for
But this seems a codig poorly done.
Does anyone have a better idea?
6. Re: change all files names to lower case.
- Posted by petelomax Aug 29, 2014
- 1369 views
So, it worked. Does anyone have a better idea?
The only thing I'd change would be to test for when the double rename is necessary, then again the code posted seems to rename things that are already lowercase. Personally, I'd write a shim (completely untested):
procedure rename(sequence d, sequence oldname, sequence newname) if not equal(newname,oldname) then if equal(lower(oldname),lower(newname)) then moveFile(d&oldname,d&oldname&'1') oldname&='1' end if moveFile(d&oldname,d&newname) end if end procedure
Pete
7. Re: change all files names to lower case.
- Posted by sergelli Aug 30, 2014
- 1304 views
It's a good idea to avoid renaming the names that are already in lowercase.
But I believe there was more direct method, which avoided rename twice.
Thanks to all for the help.