Error: The file name directory name or volume label syntax is incorrect in the Delphi 7 CopyFile function

I want to copy files from one folder to another using the CopyFile function. Source file paths are stored in a ClientDataSet called "itemsDB". Code:

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 "invalid file name directory name or volume label syntax". I returned all the file paths to the DataSet, they are 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 fine, but when I get its from clientdataset, it fails.

Thank you in advance

+4
source share
3 answers

Updated answer ...

(As recommended ...)

After some discussion in the comment field, an error was detected as an invalid length for the path characters in the Source line.

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

Set FixedChar - False in the object inspector or delete the path symbols with a space Source := Trim(Source);

+3
source

Could you write the values โ€‹โ€‹of FileName and Dest to see exactly what CopyFile is transmitting?

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

+2
source

You will get this error if you have : as part of the path to Source . You may have one , of course, as 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 donโ€™t know if you use Unicode in Delphi or if your data source is Unicode, but sometimes unknown Unicode characters go to ? .

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

+1
source

Source: https://habr.com/ru/post/1337737/


All Articles