Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Renaming an Invalid Folder

I have an application that creates invalid Directories... e.g. c:\Program Files\somedirectory. - the period is a part of the directory name.

Now, I have tried everything that I know about renaming a file in delphi.

  • RenameFile(file1,file2)
  • MoveFile(File1,File2)
  • etc...

I have tried getting the windows short path ExtractShortPathName but this just returns an empty string. (to call in commandPrompt: "RENAME ShortOldName NewName")

Now I know I can manually do it in cmd but I am not about to spend the time to go through all my files and do it ;)

Here is a bit of my code:

xshortname := ExtractFileName(ExtractShortPathName(ns + oldName));
  xNewName := newName;

  cmdTxt :=  PWideChar('/C cd ' + ns);
  cmdTxt2 := PWideChar(cmdTxt + '&&' + ' RENAME ' + xshortname + ' ' + #34 + xNewName + #34);
  ShellExecute(0, nil, 'cmd.exe', cmdTxt2, nil, SW_SHOWNORMAL);

I know shellExecute is not the best method to be using here. I am just trying to get the stinker to work. I will worry about optimizing and proper Delphi coding later.

So my question is: Does anyone know of a function that will allow me to rename an invalid directory?

like image 339
Cgdynamic Avatar asked Jun 26 '12 03:06

Cgdynamic


1 Answers

You can use the standard Windows API function MoveFile() if you pass the name of the folder in a special way:

MoveFile('\\?\D:\invalid_dir.', '\\?\D:\invalid_dir.fixed');

instead of

MoveFile('D:\invalid_dir.', 'D:\invalid_dir.fixed');

More about it can be found on MSDN under the "Naming Files, Paths, and Namespaces" topic. Note that it specifically advises against putting trailing dots in file names.

like image 76
mghie Avatar answered Sep 28 '22 01:09

mghie