4

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

riad
  • 361
  • 1
  • 9
  • 19

3 Answers3

4

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);

Community
  • 1
  • 1
Jørn E. Angeltveit
  • 3,029
  • 3
  • 22
  • 53
  • I tried the fileExists function, it says "the source doesnt exist', but the files are there. When I tried source := 'c:\test.jpg', it works, but when I get it from the clientdataset, it fails – riad Jan 31 '11 at 09:14
  • Try `ShowMessage('-->' + Source + '<--');` Maybe the `FileN` field is defined as a `FixedChar` string field? If so, you may get a lot of trailing spaces in the source? Setting `FixedChar` to `False` or adding `Source := Trim(Source);` will do... – Jørn E. Angeltveit Jan 31 '11 at 09:20
  • Ok I tried trim(source), now I got another error it says : "the specified path was not found", and fixedchar is false – riad Jan 31 '11 at 09:42
  • It works, you were right, the problem was in the spaces at the end of source variable, trim(source) solved the problem – riad Jan 31 '11 at 09:56
2

Could you log the values of FileName and Dest to see exactly what is being passed to CopyFile?

Also, it looks like you are not using Source but rather FileName, which doesn't appear to be defined anywhere in the code fragment you posted. Did you mean to use Source?

John Pickup
  • 5,015
  • 2
  • 22
  • 15
  • Sorry my mistake, it's "CopyFile(PChar(Source), PChar(Dest), False)", the files in the dataset are simples paths of images, like 'c:\test1.jpg, c:\test2.jpg ... – riad Jan 31 '11 at 08:45
  • 2
    CopyFile can only really fail if either the source file doesn't exist (or you don't have the rights to read it) or the destination directory doesn't exist (or you don't have rights to write to it). Try logging to actual values you are passing and executing a `copy` command from the command prompt – John Pickup Jan 31 '11 at 09:01
1

You will get this error if you have a : as a part of path in Source. You may have one of course like in c:\ but c:\Test:Folder\Text.txt will give you the error The filename, directory name, or volume label syntax is incorrect.

Edit 1 Another invalid character is ?. I do not know if you use Unicode in Delphi or if your source of data is Unicode but sometimes unknown Unicode characters gets translated to ?.

Edit 2 Spaces before the drive letter in source will also give you the same error.

Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281