Error:the filename directory name or volume label syntax is incorrect, in Delphi 7 CopyFile Function

riad picture riad · Jan 31, 2011 · Viewed 27.2k times · Source

I want to copy files from one folder to another using CopyFile function. The source files paths are stored in a ClientDataSet called "itemsDB". The code is :

Var Source, Dest : String;
Begin
  itemsDB.First;
  While Not itemsDB.EOF do
  Begin
    Source := itemsDB.FieldValues['FileN'];
    Dest := 'C:\NewDir\'+ExtractFileName(Source);
    if Not CopyFile(PChar(Source), PChar(Dest), False) then
          Showmessage(SysErrorMessage(getlasterror()));
     itemsDB.Next;
  end; 
 end

When I execute the code, I get the error message "the filename directory name or volume label syntax is incorrect". I verfied all the files paths in the DataSet, they're correct. In my example, my clientdataset contains two JPG images "c:\test1.jpg" and "c:\test2.jpg" When I tried source := 'c:\test1.jpg', it works perfectly, but when I get it from the clientdataset, it fails.

Thanks in advance

Answer

Jørn E. Angeltveit picture Jørn E. Angeltveit · Jan 31, 2011

Updated answer...

(As recommended...)

After some discussion in the comments field, the error was discovered to be invalid trailing space characters in the Source string.

If the FileN field is defined as a FixedChar string field, the Source will include these trailing spaces.

Set FixedChar to False in the object inspector, or remove the trailing space characters with Source := Trim(Source);